Implement OpenAI Responses API instrumentation and examples#4166
Implement OpenAI Responses API instrumentation and examples#4166vasantteja wants to merge 24 commits intoopen-telemetry:mainfrom
Conversation
- 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.
|
This is a relatively large PR, is there any way it could be split up? |
|
@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`.
...umentation-genai/opentelemetry-instrumentation-openai-v2/examples/responses/requirements.txt
Outdated
Show resolved
Hide resolved
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/responses/main.py
Outdated
Show resolved
Hide resolved
...ntelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/__init__.py
Show resolved
Hide resolved
...try-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch_responses.py
Show resolved
Hide resolved
...try-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch_responses.py
Outdated
Show resolved
Hide resolved
...try-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch_responses.py
Outdated
Show resolved
Hide resolved
…prove optional feature handling.
|
@wrisa Can you please take another look at this PR? I removed a bunch of stuff(examples and Responses.retrieve instrumentation). |
…te-setting functions.
…onse wrapper functionalities.
There was a problem hiding this comment.
Can we make it a hard dependency?
Also we don't have any coverage testing in this repo today, where no cover coming from?
There was a problem hiding this comment.
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.
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.
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
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.