Skip to content

fix(openai-agents): Stop destructively wrapping tools - #6961

Open
alexander-alderman-webb wants to merge 7 commits into
webb/openai-agents-agent-tool-definitionsfrom
webb/openai-agents-tool-overwrite
Open

fix(openai-agents): Stop destructively wrapping tools#6961
alexander-alderman-webb wants to merge 7 commits into
webb/openai-agents-agent-tool-definitionsfrom
webb/openai-agents-tool-overwrite

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Description

Overwrite the on_invoke_tool property instead of wrapping the entire tool.

Otherwise, the follow-up PR has the test error

FAILED tests/integrations/openai_agents/test_openai_agents.py::test_tool_definitions[True-True] - agents.exceptions.UserError: ToolSearchTool() requires at least one searchable Responses surface: a tool_namespace(...) function tool, a deferred-loading function tool (`function_tool(..., defer_loading=True)`), or a deferred-loading hosted MCP server (`HostedMCPTool(tool_config={..., 'defer_loading': True})`).

as defer_loading is not forwarded in the wrapper.

Issues

Reminders

@alexander-alderman-webb
alexander-alderman-webb changed the base branch from webb/openai-agents-tool-definitions to webb/openai-agents-agent-tool-definitions July 30, 2026 14:26
@github-actions

github-actions Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

99634 passed | ⏭️ 6757 skipped | Total: 106391 | Pass Rate: 93.65% | Execution Time: 365m 48s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -160
Passed Tests 📉 -135
Failed Tests 📉 -1
Skipped Tests 📉 -24

➖ Removed Tests (1)

View removed tests
  • test_continuous_profiler_auto_start_and_stop_sampled_span_streaming[non-experiment-thread]
    • File: tests.profiler.test_continuous_profiler

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 2506 uncovered lines.
✅ Project coverage is 89.88%. Comparing base (base) to head (head).

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       +15

Generated by Codecov Action

Comment thread sentry_sdk/integrations/openai_agents/patches/tools.py
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review July 31, 2026 08:55
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner July 31, 2026 08:55
Comment on lines +62 to +63
tool.on_invoke_tool._sentry_wrapped = True
tool.on_invoke_tool = create_wrapped_invoke(tool, original_on_invoke)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 27d9ea6. Configure here.

@sentrivana sentrivana left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm, but check out the bot comments if you haven't yet in case there's something to them

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.

2 participants