Skip to content

Share stream manager wrappers between instrumentations - #390

Open
lmolkova wants to merge 1 commit into
open-telemetry:mainfrom
lmolkova:genai-util-stream-helpers
Open

Share stream manager wrappers between instrumentations#390
lmolkova wants to merge 1 commit into
open-telemetry:mainfrom
lmolkova:genai-util-stream-helpers

Conversation

@lmolkova

Copy link
Copy Markdown
Member

The openai and anthropic instrumentations each had their own copy of the stream-manager lifecycle, a close-finalizing proxy for stream.response, and the stream/iterator rebind. This moves them into opentelemetry.util.genai.stream as SyncStreamManagerWrapper / AsyncStreamManagerWrapper and finalize_on_close / finalize_on_aclose, and onboards both packages.

Also fixes a span leak for streams exposing aclose instead of close — Google's async generate_content_stream emitted no span when closed before being drained.

Follow-ups: the duplicated with_raw_response proxy moves to the util next, then generator-backed stream support for #386.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Centralizes stream lifecycle handling in opentelemetry-util-genai and adopts it across OpenAI and Anthropic instrumentations.

Changes:

  • Adds shared stream-manager and close-finalization wrappers.
  • Migrates OpenAI and Anthropic implementations.
  • Supports successful finalization of aclose-only Google streams.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py Adds shared wrappers and aclose support.
util/opentelemetry-util-genai/tests/test_stream.py Tests shared lifecycle behavior.
util/opentelemetry-util-genai/.changelog/390.added Records new public helpers.
util/opentelemetry-util-genai/.changelog/390.changed Records aclose finalization.
instrumentation/opentelemetry-instrumentation-google-genai/tests/generate_content/test_async_streaming.py Tests early async stream closure.
instrumentation/opentelemetry-instrumentation-google-genai/.changelog/390.fixed Documents the span leak fix.
instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/response_wrappers.py Adopts shared wrappers.
instrumentation/opentelemetry-instrumentation-genai-openai/tests/test_response_wrappers.py Updates OpenAI wrapper tests.
instrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/wrappers.py Adopts shared wrappers.
instrumentation/opentelemetry-instrumentation-genai-anthropic/tests/test_async_wrappers.py Updates Anthropic wrapper tests.
Suppressed comments (1)

util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py:536

  • After manager.__aexit__ succeeds, the SDK manager has already awaited the stream's close (as the OpenAI and Anthropic async managers do). Calling stream_wrapper.__aexit__ then closes it a second time, which changes the SDK lifecycle and may fail for non-idempotent streams. Finalize telemetry directly here instead.
            if suppressed:
                # See SyncStreamManagerWrapper.__exit__.
                await stream_wrapper.__aexit__(None, None, None)
            else:
                await stream_wrapper.__aexit__(exc_type, exc_val, exc_tb)

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py Outdated
@lmolkova
lmolkova force-pushed the genai-util-stream-helpers branch from 0ba4f32 to 346d3b4 Compare August 14, 2026 21:43
assert factory_calls == [True]


def test_sync_manager_enter_fails_invocation_when_manager_raises():

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to utils

@lmolkova
lmolkova marked this pull request as ready for review August 14, 2026 21:51
@lmolkova
lmolkova requested a review from a team as a code owner August 14, 2026 21:51
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 14, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-08-14 22:13 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

Move the stream-manager lifecycle, the close-finalizing response proxy, and
the stream rebind helper into opentelemetry.util.genai.stream, and onboard the
anthropic and openai instrumentations onto them. Also finalize telemetry for
streams that expose aclose instead of close.

Assisted-by: Claude Opus 5
@lmolkova
lmolkova force-pushed the genai-util-stream-helpers branch from 9f9b01f to 2b6d1e0 Compare August 14, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants