Skip to content

Add response wrappers for OpenAI Responses API streams.#4280

Merged
aabmass merged 17 commits intoopen-telemetry:mainfrom
vasantteja:openai-v2-response-stream-wrappers
Mar 10, 2026
Merged

Add response wrappers for OpenAI Responses API streams.#4280
aabmass merged 17 commits intoopen-telemetry:mainfrom
vasantteja:openai-v2-response-stream-wrappers

Conversation

@vasantteja
Copy link
Copy Markdown
Contributor

@vasantteja vasantteja commented Mar 3, 2026

Description

This change adds OpenAI Responses API wrapper classes for both the realized response stream and the response stream manager in the openai_v2 instrumentation 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 both responses.create(..., stream=True) and responses.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 targets ResponseStreamManager[TextFormatT]. This preserves access to Responses-specific APIs such as get_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_v2 instrumentation.

Fixes #3436 partly

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Ran the unit test suite of openai.

Suggested verification:

  • Import opentelemetry.instrumentation.openai_v2.response_wrappers

  • Run the OpenAI v2 Responses tests

  • Test A

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

…rWrapper to include references to the OpenAI SDK source. This improves clarity on the functionality and origin of the wrappers.
@vasantteja vasantteja requested a review from a team as a code owner March 3, 2026 12:51
…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.
Copy link
Copy Markdown
Contributor

@xrmx xrmx left a comment

Choose a reason for hiding this comment

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

I guess you are waiting adding test because you are unsure of the interface?

@tammy-baylis-swi tammy-baylis-swi moved this to Reviewed PRs that need fixes in Python PR digest Mar 3, 2026
Copy link
Copy Markdown
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

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

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.
Copy link
Copy Markdown
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

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

Looks good to me if @aabmass's outstanding queries are resolved 👍🏻

@aabmass aabmass merged commit 4df92e2 into open-telemetry:main Mar 10, 2026
784 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed PRs that need fixes to Done in Python PR digest Mar 10, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Instrument OpenAI Responses API

6 participants