-
Notifications
You must be signed in to change notification settings - Fork 580
feat(integration): add gen_ai.conversation.id if available
#5307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat(integration): add gen_ai.conversation.id if available
#5307
Conversation
…_ai.conversation.id`
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Span Streaming
Other
Bug Fixes 🐛Span Streaming
Other
Internal Changes 🔧Fastmcp
Mcp
Other
🤖 This preview updates automatically when you update the PR. |
…nts-conversation-id
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 8.12s All tests are passing successfully. ❌ Patch coverage is 8.33%. Project has 13608 uncovered lines. Files with missing lines (180)
Generated by Codecov Action |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor Bugbot has reviewed your changes and found 3 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| if agent: | ||
| conv_id = getattr(agent, "_sentry_conversation_id", None) | ||
| if conv_id: | ||
| span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Callers don't pass agent to update_ai_client_span
Medium Severity
The update_ai_client_span function was modified to accept a new agent parameter (defaulting to None) for setting conversation_id on AI client spans. However, the callers in models.py at lines 128 and 179 were not updated to pass the agent argument. Since agent defaults to None, the conversation_id condition if agent: is never true, and gen_ai.conversation.id is never set on AI client spans.
| # Add conversation ID from agent | ||
| conv_id = getattr(agent, "_sentry_conversation_id", None) | ||
| if conv_id: | ||
| span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated pattern for setting conversation ID on spans
Low Severity
The same 3-line pattern for extracting and setting _sentry_conversation_id from an agent onto a span is repeated 4 times across execute_tool.py, handoff.py, invoke_agent.py, and ai_client.py. This pattern should be extracted to a utility function in utils.py (e.g., _set_conversation_id(span, agent)), following the existing pattern of _set_agent_data, _set_usage_data, etc.
| span: "sentry_sdk.tracing.Span", | ||
| response: "Any", | ||
| response_model: "Optional[str]" = None, | ||
| agent: "Optional[Agent]" = None, | ||
| ) -> None: | ||
| """Update AI client span with response data (works for streaming and non-streaming).""" | ||
| if hasattr(response, "usage") and response.usage: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The agent parameter is not passed to update_ai_client_span in models.py for streaming and non-streaming responses, preventing conversation_id from being set.
Severity: MEDIUM
Suggested Fix
Pass the agent variable to the update_ai_client_span function calls on lines 132 and 178 of sentry_sdk/integrations/openai_agents/models.py. For example, change update_ai_client_span(span, result, response_model) to update_ai_client_span(span, result, response_model, agent=agent).
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: sentry_sdk/integrations/openai_agents/spans/ai_client.py#L46-L52
Potential issue: The function `update_ai_client_span` was updated to accept an `agent`
parameter to set the `conversation_id` on AI client spans. However, the calls to this
function in `sentry_sdk/integrations/openai_agents/models.py` on lines 132
(non-streaming) and 178 (streaming) were not updated to pass this new parameter. While
the `agent` object is available in the scope of both calls, its omission means the
`conversation_id` will not be correctly propagated to the AI client spans. This issue is
particularly evident in the streaming code path, which lacks test coverage for this
specific data point.


Description
Closes https://linear.app/getsentry/issue/TET-1721/openai-agents-add-gen-aiconversationid