Add response wrappers for OpenAI Responses API streams.#4280
Merged
aabmass merged 17 commits intoopen-telemetry:mainfrom Mar 10, 2026
Merged
Add response wrappers for OpenAI Responses API streams.#4280aabmass merged 17 commits intoopen-telemetry:mainfrom
aabmass merged 17 commits intoopen-telemetry:mainfrom
Conversation
…rWrapper to include references to the OpenAI SDK source. This improves clarity on the functionality and origin of the wrappers.
…nt processing and error handling. Introduced conditional imports for response events and error types, ensuring compatibility with missing modules. Updated event processing logic to utilize a consolidated response events tuple.
…om/vasantteja/opentelemetry-python-contrib into openai-v2-response-stream-wrappers
xrmx
reviewed
Mar 3, 2026
Contributor
xrmx
left a comment
There was a problem hiding this comment.
I guess you are waiting adding test because you are unsure of the interface?
aabmass
reviewed
Mar 3, 2026
aabmass
approved these changes
Mar 3, 2026
Member
MikeGoldsmith
left a comment
There was a problem hiding this comment.
This looks really good, thanks @vasantteja. I agree with @xrmx, we definitely need tests but happy with how it's looking so far.
… processing. Introduced context managers for safe instrumentation, improved import handling for response events, and streamlined response attribute extraction logic.
…dling and stream finalization behavior.
Member
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Looks good to me if @aabmass's outstanding queries are resolved 👍🏻
… code cleanliness.
…cks for response events. Removed unnecessary conditional checks for event types, enhancing code clarity and maintainability.
…om/vasantteja/opentelemetry-python-contrib into openai-v2-response-stream-wrappers
…mproved error handling and context management. Introduced ExitStack for better resource management and added NotImplementedError for unimplemented parse methods. Updated TODO comments for future refactoring with wrapt.ObjectProxy.
sightseeker
added a commit
to sightseeker/opentelemetry-python-contrib
that referenced
this pull request
Mar 11, 2026
…ry#4280) * Add response wrappers for OpenAI Responses API streams. * Enhance docstrings for ResponseStreamWrapper and ResponseStreamManagerWrapper to include references to the OpenAI SDK source. This improves clarity on the functionality and origin of the wrappers. * Add wrappers for OpenAI Responses API streams and response stream managers in CHANGELOG.md * Refactor response handling in OpenAI response wrappers to improve event processing and error handling. Introduced conditional imports for response events and error types, ensuring compatibility with missing modules. Updated event processing logic to utilize a consolidated response events tuple. * Refactor OpenAI response wrappers to enhance error handling and event processing. Introduced context managers for safe instrumentation, improved import handling for response events, and streamlined response attribute extraction logic. * Add unit tests for ResponseStreamManagerWrapper to validate error handling and stream finalization behavior. * Remove unnecessary blank line in test_response_wrappers.py to improve code cleanliness. * Refactor event handling in ResponseStreamWrapper to simplify type checks for response events. Removed unnecessary conditional checks for event types, enhancing code clarity and maintainability. * Enhance ResponseStreamWrapper and ResponseStreamManagerWrapper with improved error handling and context management. Introduced ExitStack for better resource management and added NotImplementedError for unimplemented parse methods. Updated TODO comments for future refactoring with wrapt.ObjectProxy.
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.
Description
This change adds OpenAI Responses API wrapper classes for both the realized response stream and the response stream manager in the
openai_v2instrumentation package. This is part of this pr. I moved this component into a different pr to make review easy and get feedback. The new wrappers are intended to support instrumentation around bothresponses.create(..., stream=True)andresponses.stream()while preserving the OpenAI SDK object model.The implementation keeps the wrapper types aligned with the SDK's concrete Responses types, so the stream wrapper targets
ResponseStream[TextFormatT]and the manager wrapper targetsResponseStreamManager[TextFormatT]. This preserves access to Responses-specific APIs such asget_final_response(),response, and context manager lifecycle hooks while allowing telemetry to be attached consistently across both entrypoints.Motivation: the OpenAI Responses API exposes two streaming entrypoints with different return types, and we need wrappers for both so instrumentation can be applied without changing user-facing behavior. No additional external dependencies are required for this change beyond the existing OpenAI SDK support already used by the
openai_v2instrumentation.Fixes #3436 partly
Type of change
How Has This Been Tested?
Ran the unit test suite of openai.
Suggested verification:
Import
opentelemetry.instrumentation.openai_v2.response_wrappersRun the OpenAI v2 Responses tests
Test A
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.