fix(openai-agents): Stop destructively wrapping tools - #6961
fix(openai-agents): Stop destructively wrapping tools#6961alexander-alderman-webb wants to merge 7 commits into
Conversation
Codecov Results 📊✅ 99634 passed | ⏭️ 6757 skipped | Total: 106391 | Pass Rate: 93.65% | Execution Time: 365m 48s 📊 Comparison with Base Branch
➖ Removed Tests (1)View removed tests
All tests are passing successfully. ✅ Patch coverage is 100.00%. Project has 2506 uncovered lines. Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 89.88% 89.88% —%
==========================================
Files 193 193 —
Lines 24686 24753 +67
Branches 8814 8860 +46
==========================================
+ Hits 22188 22247 +59
- Misses 2498 2506 +8
- Partials 1400 1415 +15Generated by Codecov Action |
…enai-agents-tool-overwrite
This reverts commit 4efb6fb.
…enai-agents-tool-overwrite
| tool.on_invoke_tool._sentry_wrapped = True | ||
| tool.on_invoke_tool = create_wrapped_invoke(tool, original_on_invoke) |
There was a problem hiding this comment.
Bug: The _sentry_wrapped flag is set on the original function, not the wrapper. This causes the idempotency check to fail on subsequent calls, leading to tools being wrapped multiple times.
Severity: HIGH
Suggested Fix
The _sentry_wrapped flag should be set on the new wrapper function after it is created, not on the original function before it is replaced. The correct sequence is to create the wrapper, set the flag on it, and then assign it to tool.on_invoke_tool.
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/patches/tools.py#L62-L63
Potential issue: The idempotency check for wrapping `FunctionTool` instances is flawed.
The code sets the `_sentry_wrapped` flag on the original `tool.on_invoke_tool` function
before replacing it with a new wrapper function. In subsequent calls within a multi-turn
agent run, the check inspects the new wrapper function, which does not have the
`_sentry_wrapped` attribute. This causes the check to fail, leading to the tool being
wrapped repeatedly. This results in deeply nested spans and incorrect telemetry for any
agent that reuses tools across multiple turns.
Did we get this right? 👍 / 👎 to inform future reviews.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 27d9ea6. Configure here.
| wrapped_tools.append(wrapped_tool) | ||
| tool.on_invoke_tool._sentry_wrapped = True | ||
| tool.on_invoke_tool = create_wrapped_invoke(tool, original_on_invoke) | ||
| wrapped_tools.append(tool) |
There was a problem hiding this comment.
Stale agent on shared tools
Medium Severity
Permanently replacing on_invoke_tool and skipping already-wrapped tools freezes the agent captured by the invoke wrapper. When the same FunctionTool is reused by another agent (shared tools, clones, or handoffs), later execute_tool spans keep the first agent's name, model settings, and conversation id.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 27d9ea6. Configure here.
sentrivana
left a comment
There was a problem hiding this comment.
lgtm, but check out the bot comments if you haven't yet in case there's something to them


Description
Overwrite the
on_invoke_toolproperty instead of wrapping the entire tool.Otherwise, the follow-up PR has the test error
as
defer_loadingis not forwarded in the wrapper.Issues
Reminders
uv run ruff.feat:,fix:,ref:,meta:)