Skip to content

Implement OpenAI Responses API instrumentation and examples#4166

Closed
vasantteja wants to merge 29 commits intoopen-telemetry:mainfrom
vasantteja:feat/instrument-openai-responses
Closed

Implement OpenAI Responses API instrumentation and examples#4166
vasantteja wants to merge 29 commits intoopen-telemetry:mainfrom
vasantteja:feat/instrument-openai-responses

Conversation

@vasantteja
Copy link
Copy Markdown
Contributor

@vasantteja vasantteja commented Feb 5, 2026

Description

This PR adds OpenAI Responses API instrumentation (sync Responses.create) to opentelemetry-instrumentation-openai-v2, using TelemetryHandler. It also adds tests for Responses API create behavior.

Fixes #3436 partly

Type of change

Please delete options that are not relevant.

  • 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?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Responses API tests with VCR recordings
source .tox/py312-test-instrumentation-openai-v2-latest/bin/activate
pytest instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_responses.py --vcr-record=all -v

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

- Added instrumentation for the OpenAI Responses API, including tracing for `Responses.create` and `Responses.stream` methods.
- Introduced example scripts demonstrating the usage of the Responses API with OpenTelemetry.
- Created a `.env` file for configuration, including API keys and OpenTelemetry settings.
- Updated README files to include instructions for running examples and configuring the environment.
- Added unit tests for the new Responses API functionality, ensuring proper tracing and metrics collection.

This update enhances the observability of OpenAI API interactions within the OpenTelemetry framework.
- Updated the OpenAIInstrumentor to conditionally wrap and unwrap the Responses API methods based on the installed OpenAI package version (>=1.66.0).
- Added version checks in the test suite to skip tests if the Responses API is not available, ensuring compatibility with earlier versions of the OpenAI library.
- Improved error handling for missing API methods to prevent runtime exceptions.
- Added pylint disable comments to suppress warnings for specific lines in the Responses API example and patch files.
- Updated the `responses_create` and `responses_stream` methods with links to relevant OpenAI documentation for better reference.
- Improved code formatting for readability by adjusting line breaks and indentation in the patch file.
- Reformatted code in the patch file to enhance readability by adjusting line breaks and indentation.
- Ensured consistent style for model retrieval and span name updates in the ResponseStreamWrapper class.
- Minor adjustments to import statements for clarity and organization.
- Updated the OpenAI package version in requirements.txt to 1.66.0 for compatibility.
- Refactored span name retrieval in the patch.py file to directly format the span name using operation and model attributes, removing the redundant _get_span_name function.
- Improved code clarity and consistency in the responses_create and responses_stream methods.
…omments

- Added a comment in the `responses_stream` method to clarify the purpose of avoiding duplicate span creation.
- Updated span name retrieval to use a default value of 'unknown' for the model attribute if not present, improving robustness.
- Refactored the `_record_metrics` function to directly access the request model from attributes, enhancing clarity and consistency.
- Added a new wrapper function `responses_retrieve` to trace the `retrieve` method of the `Responses` class, enhancing observability.
- Updated the `OpenAIInstrumentor` to include the new tracing functionality for the `retrieve` method.
- Enhanced test coverage for the `retrieve` method, including new test cases for both standard and streaming responses.
- Added new YAML cassettes to support the updated tests for the `retrieve` functionality.
- Added a TODO comment in the patch.py file to consider migrating Responses instrumentation to TelemetryHandler once content capture and streaming hooks are available.
- Included a reference link to the OpenAI responses.py file for context on the `retrieve` method.
- Introduced a new module `patch_responses.py` to handle tracing for the `Responses` class methods, including `create`, `stream`, and `retrieve`.
- Updated the `__init__.py` file to import the new responses patching functions.
- Enhanced test coverage with new YAML cassettes for various response scenarios, including standard and streaming responses.
- Removed outdated response tracing logic from `patch.py` to streamline the instrumentation process.
…strumentation

- Moved the `_record_metrics` function to the `utils.py` file for better organization and accessibility.
- Updated the `patch.py` file to import the `_record_metrics` function from `utils.py`, streamlining the code structure.
- Enhanced the `responses_retrieve` method to simplify span attribute checks and improve readability.
- Added new test cases and YAML cassettes to cover various response scenarios, including streaming and standard responses.
@JWinermaSplunk
Copy link
Copy Markdown

This is a relatively large PR, is there any way it could be split up?

@vasantteja
Copy link
Copy Markdown
Contributor Author

@JWinermaSplunk Sure I removed the async stuff to make it small. I will remove stream and retrieve to make it more small. FYI I am rewriting this with TelemetryHandler so that we can start using shared utils. I am assuming that might make the pr little bulky so can I remove the examples?

…ry support

- Added `opentelemetry-util-genai` as a dependency for improved telemetry handling.
- Refactored response handling in `patch_responses.py` to utilize `TelemetryHandler` for tracing.
- Updated `responses_create` and `responses_retrieve` methods to integrate new telemetry features.
- Simplified imports and removed unused code in `__init__.py`.
- Added extensive test cases and YAML cassettes for various response scenarios, including streaming and standard responses.
- Adjusted requirements files to include the new utility package for testing.
…try features

- Updated `responses_create` and `responses_retrieve` methods to streamline content capture logic.
- Introduced helper functions for extracting input and output messages, and system instructions.
- Enhanced telemetry support by integrating content capture based on experimental mode settings.
- Added new test cases to validate content capture functionality in various scenarios, including streaming and standard responses.
- Created YAML cassettes for testing response handling and content capture behavior.
- Simplified imports and removed unused code in `__init__.py` and `patch_responses.py`.
invocation,
result,
"service_tier",
GenAIAttributes.GEN_AI_OPENAI_RESPONSE_SERVICE_TIER,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@lmolkova
Copy link
Copy Markdown
Member

@vasantteja it seems we have some intersection with this PR - #3715 (switching to new semconv for chat completion), would you mind reviewing it?

We'll probably have some merge conflicts, but not terrible ones

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.

Generally looks pretty good - left some suggestions and would like to reduce size of this PR if possible.


This is another 3000+ lines change PR across many files - PRs of this size are hard to review with confidence. I know this is experimental and wanting to move fast, but huge PRs makes it challenging.

In this case, it's mostly VCR cassettes but I think we could break up into the following parts:

  1. util-genai only — eg should_capture_content() plus tests
  2. Responses API core (non-streaming)
  3. Responses API streaming

As pointed out by @lmolkova - this PR is touching a lot of files #3715 does too. eg both modify utils.py, __init__.py, and patch.py.

Maybe we should we focus on one first, then rebase the other one to minimise conflicts?


Other things I noticed:

  • pyproject.toml has openai >= 1.26.0 but Responses API requires openai >= 1.66.0
  • can we separate some of the changes into other files to avoid having to disable pylint's too many lines? eg something like patch_chat.py

@tammy-baylis-swi tammy-baylis-swi moved this to Reviewed PRs that need fixes in Python PR digest Feb 26, 2026
@vasantteja vasantteja closed this Mar 6, 2026
@github-project-automation github-project-automation Bot moved this from Reviewed PRs that need fixes to Done in Python PR digest Mar 6, 2026
@vasantteja vasantteja reopened this Mar 6, 2026
@MikeGoldsmith
Copy link
Copy Markdown
Member

@vasantteja looks like you may have some lint errors. You can run the precommit to clean them up.

@tammy-baylis-swi tammy-baylis-swi moved this from Done to Approved PRs that need fixes in Python PR digest Mar 6, 2026
@vasantteja vasantteja closed this Mar 16, 2026
@github-project-automation github-project-automation Bot moved this from Approved PRs that need fixes to Done in Python PR digest Mar 16, 2026
@vasantteja vasantteja reopened this Mar 16, 2026
@vasantteja vasantteja closed this Mar 16, 2026
@vasantteja vasantteja reopened this Mar 16, 2026
@vasantteja vasantteja marked this pull request as draft March 16, 2026 21:08
@MikeGoldsmith
Copy link
Copy Markdown
Member

@vasantteja please can you merge main into this branch and resolve conflicts, then run precommit as there are lint errors.

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