Skip to content

Implement OpenAI Responses API instrumentation and examples#4166

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

Implement OpenAI Responses API instrumentation and examples#4166
vasantteja wants to merge 6 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 and Responses.stream) to opentelemetry-instrumentation-openai-v2, including streaming span lifecycle handling, token metrics,
and request/response attributes. It also adds tests for Responses API behavior and updates docs/examples to show Responses usage.

Motivation: the Responses API is now a primary OpenAI interface; instrumentation should cover it just like chat completions and embeddings.

Dependencies: OpenAI SDK with Responses support (e.g., openai>=1.57.3) and jiter for streaming.

Fixes # (3436)

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.
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.

6 participants