Skip to content

fix(openai-agents): set gen_ai.operation.name to "responses" for OpenAIResponsesModel#6603

Closed
singlaamitesh wants to merge 2 commits into
getsentry:masterfrom
singlaamitesh:fix/openai-agents-operation-name-6417
Closed

fix(openai-agents): set gen_ai.operation.name to "responses" for OpenAIResponsesModel#6603
singlaamitesh wants to merge 2 commits into
getsentry:masterfrom
singlaamitesh:fix/openai-agents-operation-name-6417

Conversation

@singlaamitesh

Copy link
Copy Markdown

Summary

Fixes #6417.

The ai_client_span function hardcoded gen_ai.operation.name = "chat" for every call, even when openai-agents was using OpenAIResponsesModel, which calls the OpenAI Responses API (not Chat Completions).

Before:

# TODO-anton: implement other types of operations. Now "chat" is hardcoded.
SPANDATA.GEN_AI_OPERATION_NAME: "chat",  # ← always "chat"
name=f"chat {model_name}",               # ← always "chat ..."

After: the model class is detected in _get_model (where the model object is available) and passed to ai_client_span:

  • OpenAIResponsesModel"responses" (consistent with the standalone openai integration)
  • Any other model class (e.g. OpenAIChatCompletionsModel) → "chat"

The sentry.op remains OP.GEN_AI_CHAT for both (same as the standalone openai integration does for Responses API spans).

How it works

_get_model already has the full model object. A cheap isinstance check determines the operation name, which is captured in the closure and forwarded to both wrapped_get_response and wrapped_stream_response via the new operation parameter on ai_client_span.

Test plan

  • All 144 existing openai-agents tests pass with updated assertions ("chat""responses")
  • Bug reproduced: without fix OpenAIResponsesModel always produced chat gpt-4o; with fix it produces responses gpt-4o
  • ruff check + ruff format — clean

…AIResponsesModel

The ai_client_span function hardcoded gen_ai.operation.name = "chat" for
every call, even when openai-agents was using OpenAIResponsesModel which
calls the Responses API (not the Chat Completions API).

Fix: detect the model class in _get_model (where the model object is
available) and pass the correct operation name through to ai_client_span:
- OpenAIResponsesModel -> "responses" (consistent with the openai integration)
- All other models (e.g. OpenAIChatCompletionsModel) -> "chat"

The span name and gen_ai.operation.name now both reflect the actual API
being used. The sentry.op remains OP.GEN_AI_CHAT for both (same as the
standalone openai integration does for Responses API calls).

Also remove the two TODO comments that tracked this gap.

Fixes getsentry#6417.
@singlaamitesh singlaamitesh requested a review from a team as a code owner June 18, 2026 05:27
Copilot AI review requested due to automatic review settings June 18, 2026 05:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes incorrect gen_ai.operation.name labeling in the openai-agents integration when the underlying model uses the OpenAI Responses API, aligning span semantics with the standalone openai integration.

Changes:

  • Add an operation parameter to ai_client_span and use it for span naming plus gen_ai.operation.name.
  • Detect OpenAIResponsesModel in _get_model and forward "responses" (otherwise default to "chat").
  • Update openai-agents integration tests to assert "responses"-prefixed span names and operation attributes/data.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
sentry_sdk/integrations/openai_agents/spans/ai_client.py Makes ai_client_span operation-aware so spans can be labeled as "responses" vs "chat".
sentry_sdk/integrations/openai_agents/patches/models.py Detects OpenAIResponsesModel and passes the correct operation string into span creation.
tests/integrations/openai_agents/test_openai_agents.py Updates assertions to validate the corrected "responses" operation labeling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +91 to +99
# Determine the gen_ai operation name from the model class.
# OpenAIResponsesModel uses the Responses API ("responses"); all other
# model classes (e.g. OpenAIChatCompletionsModel) use Chat Completions ("chat").
try:
from agents.models.openai_responses import OpenAIResponsesModel

_operation = "responses" if isinstance(model, OpenAIResponsesModel) else "chat"
except ImportError:
_operation = "chat"
… for operation detection

Avoids importing an internal agents class inside the function body.
type(model).__name__ lookup is more resilient to package restructuring
and makes the full set of supported model types explicit in one dict.
@alexander-alderman-webb

Copy link
Copy Markdown
Contributor

No string matching on classes please, that's brittle.
Feel free to discuss the approach before PRing on the issue @singlaamitesh!

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.

Set gen_ai.operation.name correctly in openai-agents

3 participants