Share stream manager wrappers between instrumentations - #390
Open
lmolkova wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
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). Callingstream_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.
lmolkova
force-pushed
the
genai-util-stream-helpers
branch
from
August 14, 2026 21:43
0ba4f32 to
346d3b4
Compare
lmolkova
commented
Aug 14, 2026
| assert factory_calls == [True] | ||
|
|
||
|
|
||
| def test_sync_manager_enter_fails_invocation_when_manager_raises(): |
lmolkova
marked this pull request as ready for review
August 14, 2026 21:51
Pull request dashboard statusWaiting on reviewers · refreshed 2026-08-14 22:13 UTC Review the latest changes. Status above doesn't look right?
|
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
force-pushed
the
genai-util-stream-helpers
branch
from
August 14, 2026 21:57
9f9b01f to
2b6d1e0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 intoopentelemetry.util.genai.streamasSyncStreamManagerWrapper/AsyncStreamManagerWrapperandfinalize_on_close/finalize_on_aclose, and onboards both packages.Also fixes a span leak for streams exposing
acloseinstead ofclose— Google's asyncgenerate_content_streamemitted no span when closed before being drained.Follow-ups: the duplicated
with_raw_responseproxy moves to the util next, then generator-backed stream support for #386.