-
Notifications
You must be signed in to change notification settings - Fork 509
fix: Duplicate LLM calls for the Agents SDK #984
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
Conversation
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.
Pull Request Overview
This pull request fixes duplicate LLM calls for the Agents SDK by adding special handling for "ResponseSpanData" and integrating custom wrappers into the OpenAI instrumentation. Key changes include:
- Adding context propagation for ResponseSpanData in exporter.py.
- Wrapping the Responses API calls in instrumentor.py with custom wrappers to leverage the Agents SDK trace context.
- Unwrapping the custom wrappers when uninstrumenting, with enhanced debug logging in both files.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| agentops/instrumentation/openai_agents/exporter.py | Adds special handling for ResponseSpanData to propagate trace context |
| agentops/instrumentation/openai/instrumentor.py | Introduces custom wrappers for both synchronous and asynchronous responses and handles unwrapping for the custom instrumentation |
Comments suppressed due to low confidence (1)
agentops/instrumentation/openai_agents/exporter.py:319
- The variable 'span_id' is used without being defined. Please ensure 'span_id' is properly assigned before this line or update the reference if a different variable should be used.
ctx = context_api.set_value("openai_agents.span_id", span_id, ctx)
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Pull Request Overview
This PR fixes duplicate LLM calls by setting the context in the OpenAI instrumentor using information from the OpenAI Agents SDK. Key changes include adding tests to verify context propagation in custom wrappers, propagating trace context within the exporter for ResponseSpanData, and updating the instrumentor to use custom wrappers for responses.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/unit/instrumentation/openai_core/test_custom_wrappers.py | Added unit tests to ensure the custom responses wrappers correctly set OpenAI Agents SDK context. |
| agentops/instrumentation/openai_agents/exporter.py | Introduced trace context propagation for ResponseSpanData to prevent duplicate spans and calls. |
| agentops/instrumentation/openai/instrumentor.py | Updated the instrumentor to wrap and unwrap responses using custom wrappers with added logging. |
Comments suppressed due to low confidence (1)
agentops/instrumentation/openai_agents/exporter.py:319
- The variable 'span_id' is used here but not defined in this scope. Ensure 'span_id' is properly retrieved or assigned before being used.
ctx = context_api.set_value("openai_agents.span_id", span_id, ctx)
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.
Pull Request Overview
This PR fixes duplicate LLM calls in the Agents SDK by updating how the OpenAI responses instrumentation is applied. Key changes include:
- Switching from the standard wrap/unwrap to using custom wrappers via wrap_function_wrapper.
- Updating and extending tests for both synchronous and asynchronous response instrumentation.
- Adding special handling in the OpenAI Agents exporter to propagate trace context from response spans.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/unit/instrumentation/openai_core/test_instrumentor.py | Updated tests to verify the use of custom wrappers with wrap_function_wrapper |
| tests/unit/instrumentation/openai_core/test_custom_wrappers.py | Added tests to ensure custom wrappers correctly handle context and span attributes |
| agentops/instrumentation/openai_agents/exporter.py | Added logic for processing ResponseSpanData and propagating trace context |
| agentops/instrumentation/openai/instrumentor.py | Updated _instrument and _uninstrument to use custom wrappers and provide fallback logging |
Comments suppressed due to low confidence (1)
agentops/instrumentation/openai_agents/exporter.py:319
- The variable 'span_id' is used without being defined. Consider retrieving the span identifier from the span (similarly to how 'trace_id' and 'parent_id' are obtained) to ensure proper context propagation.
ctx = context_api.set_value("openai_agents.span_id", span_id, ctx)
|
@the-praxs The original ticket also mentions tool calls. Please either do that in this branch or make a new ticket :) As for the review-- this is fairly complicated. @Dwij1704 has some more insights on the right method here. |
Will do it here |
|
@the-praxs Another major issue here is that responses don't seem to be properly instrumented. Run any example OpenAI Agents and you'll see. Agents uses the Responses API, and for whatever reason, we don't catch prompts. Here's an example JSON we get in the AgentOps dashboard. Notice there's no prompt (user or system), only a completion. |
I ran all the notebooks and checked the spans completely. I had both the prompts and completions in the LLM calls. I cannot reproduce this issue but let me see if there's something wonky. |
|
Cannot reproduce the issue you mentioned. Here are the trace IDs for each of the notebooks and they have the data as intended -
|
|
Closing this since #987 makes a core change. |

📥 Pull Request
📘 Description
Sets the context in the OpenAI instrumentor from the OpenAI Agents SDK instrumentor.
Closes #974
🧪 Testing
Tested with the customer service agent example and the demo repo.