fix(openai-agents): set gen_ai.operation.name to "responses" for OpenAIResponsesModel#6603
Closed
singlaamitesh wants to merge 2 commits into
Closed
Conversation
…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.
There was a problem hiding this comment.
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
operationparameter toai_client_spanand use it for span naming plusgen_ai.operation.name. - Detect
OpenAIResponsesModelin_get_modeland forward"responses"(otherwise default to"chat"). - Update
openai-agentsintegration 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.
Contributor
|
No string matching on classes please, that's brittle. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #6417.
The
ai_client_spanfunction hardcodedgen_ai.operation.name = "chat"for every call, even when openai-agents was usingOpenAIResponsesModel, which calls the OpenAI Responses API (not Chat Completions).Before:
After: the model class is detected in
_get_model(where the model object is available) and passed toai_client_span:OpenAIResponsesModel→"responses"(consistent with the standaloneopenaiintegration)OpenAIChatCompletionsModel) →"chat"The
sentry.opremainsOP.GEN_AI_CHATfor both (same as the standalone openai integration does for Responses API spans).How it works
_get_modelalready has the full model object. A cheapisinstancecheck determines the operation name, which is captured in the closure and forwarded to bothwrapped_get_responseandwrapped_stream_responsevia the newoperationparameter onai_client_span.Test plan
"chat"→"responses")OpenAIResponsesModelalways producedchat gpt-4o; with fix it producesresponses gpt-4oruff check+ruff format— clean