add OpenAI responses support#3901
Conversation
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
…version) Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
….resources.chat.completions Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
…onversation_support updated responses and conversation support
|
|
Just a thought, the patch.py is getting bigger and bigger...anyone think we should split it up ? I assume we will keep adding things in the future. so...just wondering... |
|
Agree with the previous comment, responses itself could probably be a separate file. Also didn't look too deeply into it, but I think there is a bit of code here that can be moved into helper functions (to prevent duplicate code, decrease clutter, etc). |
There is a style check in one of the test suites that enforces a 1000 line limit for any file otherwise a set of changes will fail to score a 10/10 and the test will get marked fail. I believe a refactor will be necessary to pass CI for these changes. |
| response = await async_openai_client.responses.create( | ||
| input=input_value, model=llm_model_value |
There was a problem hiding this comment.
| response = await async_openai_client.responses.create( | |
| input=input_value, model=llm_model_value | |
| response = await async_openai_client.responses.create( | |
| input=input_value, | |
| model=llm_model_value, | |
| stream_options={"include_usage": True} |
This will make sure that token usage gets returned, so that we can test that the instrument captures it. That said, I do see usage in your cassettes, so this is more of a nit.
|
Hi, is there any progress on this? |
|
I no longer have the capacity to work on this, I'm sorry. Can we mark this is stale so someone else can pick it up? It has been stale for a very long time. |
|
Hi @iamemilio, I don't mind picking it up, I was just wondering if there has been any communication from the original author, @M-Hietala? |
|
None that I am aware of, no :( |
|
Closing in favor of #4166 |
Summary
This PR adds instrumentation support for the OpenAI Responses API (structured outputs) to the
opentelemetry-instrumentation-openai-v2library, following the same monkeypatching pattern used for chat completions.Background
The OpenAI SDK introduced the Responses API (
client.responses.create) for structured outputs in version 1.66.0. This API was not previously instrumented, meaning calls to it would not generate telemetry data (spans, logs, or metrics).Changes
This PR instruments both synchronous and asynchronous versions of the Responses API:
Implementation Details
Version Checking:
_is_responses_api_supported()function to detect if OpenAI SDK >= 1.66.0packaging.versionfor reliable version comparisonNew wrapper functions in
patch.py:responses_create()- Wraps synchronousResponses.createmethodasync_responses_create()- Wraps asynchronousAsyncResponses.createmethod_set_responses_attributes()- Sets span attributes for responses_record_responses_metrics()- Records metrics for responses API callsInstrumentation hooks in
__init__.py:wrap_function_wrappercalls foropenai.resources.responses.responses.Responses.createwrap_function_wrappercalls foropenai.resources.responses.responses.AsyncResponses.createunwrapcalls in_uninstrument()methodTelemetry Captured
The instrumentation captures (when responses API is available):
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true)Tests
Added comprehensive test coverage with version-aware skipping:
test_responses.py- Tests for synchronous responses API with/without content capture (skipped if OpenAI < 1.66.0)test_async_responses.py- Tests for asynchronous responses API with/without content capture (skipped if OpenAI < 1.66.0)Documentation
Updated documentation to include responses API examples:
README.rst- Added usage example showing both chat completions and responses API__init__.py- Added responses API exampleBug Fixes
openai.types.chatinstead ofopenai.resources.chat.completionsTesting
Verified that:
Compatibility
Backward Compatibility
This implementation maintains full backward compatibility. Users with OpenAI SDK versions < 1.66.0 will continue to have chat completions instrumented while responses API instrumentation is gracefully skipped.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.