Skip to content

Conversation

Copy link

Copilot AI commented Oct 14, 2025

Summary

This PR adds instrumentation support for the OpenAI Responses API (structured outputs) to the opentelemetry-instrumentation-openai-v2 library, 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:

from openai import OpenAI
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor

OpenAIInstrumentor().instrument()

client = OpenAI()

# Now automatically instrumented!
response = client.responses.create(
    model="gpt-4o-mini",
    input="Write a short poem on open telemetry.",
)

Implementation Details

Version Checking:

  • Added _is_responses_api_supported() function to detect if OpenAI SDK >= 1.66.0
  • Instrumentation only wraps responses API when supported version is detected
  • Chat completions instrumentation is always enabled (no version requirement)
  • Uses packaging.version for reliable version comparison

New wrapper functions in patch.py:

  • responses_create() - Wraps synchronous Responses.create method
  • async_responses_create() - Wraps asynchronous AsyncResponses.create method
  • _set_responses_attributes() - Sets span attributes for responses
  • _record_responses_metrics() - Records metrics for responses API calls

Instrumentation hooks in __init__.py:

  • Added conditional wrap_function_wrapper calls for openai.resources.responses.responses.Responses.create
  • Added conditional wrap_function_wrapper calls for openai.resources.responses.responses.AsyncResponses.create
  • Added corresponding conditional unwrap calls in _uninstrument() method

Telemetry Captured

The instrumentation captures (when responses API is available):

  • Spans with attributes including operation name, model, response ID, service tier, and token usage
  • Log events for input/output messages (when OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true)
  • Metrics for operation duration and token usage (input/output tokens)

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
  • Module docstring in __init__.py - Added responses API example

Bug Fixes

  • Fixed ChatCompletion imports to use openai.types.chat instead of openai.resources.chat.completions

Testing

Verified that:

  • All methods are correctly wrapped after instrumentation
  • All methods are correctly unwrapped after uninstrumentation
  • Spans capture correct attributes (model, tokens, service tier)
  • Logs capture input/output based on content capture setting
  • Metrics are recorded for duration and token usage
  • Implementation follows existing code patterns and style
  • Version checking correctly detects supported/unsupported OpenAI versions (1.66.0 threshold)
  • Tests are automatically skipped when OpenAI version doesn't support responses API
  • ChatCompletion imports are correct and use the proper type location

Compatibility

  • OpenAI SDK: >= 1.26.0 (minimum version), >= 1.66.0 (for responses API support)
  • Python: >= 3.9
  • OpenTelemetry API: ~= 1.37

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

Add support to the openai v2 instrumentation library for the openai responses API. Use the same pattern (monkeypatching) as is done for the chat completions.


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

Copilot AI and others added 3 commits October 14, 2025 23:59
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>
Copilot AI changed the title [WIP] Add support for openai responses API in instrumentation library Add support for OpenAI Responses API instrumentation Oct 15, 2025
Copilot AI requested a review from johanste October 15, 2025 00:07
Copilot finished work on behalf of johanste October 15, 2025 00:07
@johanste
Copy link
Owner

@copilot, only try to instrument responses if the version of the openai client library version installed supports responses. And only run the responses test if the openai client library version supports it

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Copy link
Author

Copilot AI commented Oct 15, 2025

@copilot, only try to instrument responses if the version of the openai client library version installed supports responses. And only run the responses test if the openai client library version suppor...

Added version checking in commit 6b5f542. The instrumentation now:

  • Only instruments responses API if openai.__version__ >= 2.3.0
  • Skips responses tests if OpenAI version doesn't support it (using pytestmark)
  • Uses packaging.version for reliable version comparison

Copilot finished work on behalf of johanste October 15, 2025 00:55
@johanste
Copy link
Owner

@copilot, doesn't openai version 1.66 and later support responses? Use the lowest version you can for responses support.

…version)

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Copy link
Author

Copilot AI commented Oct 15, 2025

@copilot, doesn't openai version 1.66 and later support responses? Use the lowest version you can for responses support.

Updated to use OpenAI >= 1.66.0 in commit e9c4a2f. The version check now uses the lowest supported version (1.66.0) where the responses API was first introduced.

Copilot finished work on behalf of johanste October 15, 2025 01:09
@johanste
Copy link
Owner

@copilot, fix the imports of ChatCompletion. It should be from openai.types.chat, not from openai.resources.chat.completions.

….resources.chat.completions

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Copy link
Author

Copilot AI commented Oct 15, 2025

@copilot, fix the imports of ChatCompletion. It should be from openai.types.chat, not from openai.resources.chat.completions.

Fixed in commit f8a2e1c. Changed imports from openai.resources.chat.completions to openai.types.chat in both test_chat_completions.py and test_async_chat_completions.py.

Copilot finished work on behalf of johanste October 15, 2025 01:37
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.

3 participants