Skip to content

Implement OpenAI Responses API instrumentation and examples#4166

Open
vasantteja wants to merge 24 commits intoopen-telemetry:mainfrom
vasantteja:feat/instrument-openai-responses
Open

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

Conversation

@vasantteja
Copy link
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

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

@vasantteja
Copy link
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`.
@vasantteja
Copy link
Contributor Author

@wrisa Can you please take another look at this PR? I removed a bunch of stuff(examples and Responses.retrieve instrumentation).

Copy link
Member

Choose a reason for hiding this comment

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

Can we make it a hard dependency?

Also we don't have any coverage testing in this repo today, where no cover coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's actually a hard dependency. I removed the unwanted try catch and pragma. I added it just of abundance of caution to ensure all the gh actions turn green.

…g for genai types, simplifying imports across multiple files.
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.

Instrument OpenAI Responses API

10 participants

Comments