Skip to content

ref(openai-agents): Use first class agent hooks when available - #7029

Closed
alexander-alderman-webb wants to merge 40 commits into
masterfrom
webb/use-agent-hooks-2
Closed

alexander-alderman-webb wants to merge 40 commits into
masterfrom
webb/use-agent-hooks-2

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Add on_agent_start(), on_agent_end() and on_handoff() hooks.

Unlike tool hooks, the Invoke Agent span is stored on the RunHooks instance.

If the hooks are active, start and finish Invoke Agent spans in the hooks in successful executions. If an exception bubbles up, on_agent_end() does not run. Modify monkey-patches so that the span stored on the RunHooks instance is finished if the hooks are present.

Make the signatures of invoke_agent_span(), update_invoke_agent_span() and _maybe_start_agent_span() more minimal so that they can be re-used in the on_agent_start() and on_agent_end() hooks.

Issues

Closes #6989

Reminders

@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139228 passed | ⏭️ 7857 skipped | Total: 147085 | Pass Rate: 94.66% | Execution Time: 482m 7s

📊 Comparison with Base Branch

Metric Change
Total Tests —
Passed Tests 📈 +1
Failed Tests 📉 -1
Skipped Tests —

➖ Removed Tests (1)

View removed tests
  • test_profile_captured
    • File: tests.profiler.test_transaction_profiler

All tests are passing successfully.

✅ Patch coverage is 88.57% (93 of 105 changed executable lines covered; target 80%).
Project statement coverage is 90.26%.

Changed files with executable lines (4)
File Patch coverage Changed executable lines
sentry_sdk/integrations/openai_agents/patches/runner.py 83.02% 44/53 covered; missed: 190, 191, 192, 288, 293, 294, 335, 341, 342; partial branches: 75, 92, 287, 295, 332
sentry_sdk/integrations/openai_agents/patches/agent_run.py 95.56% 43/45 covered; missed: 131, 241; partial branches: 126, 140, 142, 154, 225, 236, 250, 252, 264
sentry_sdk/integrations/openai_agents/spans/invoke_agent.py 80.00% 4/5 covered; missed: 131; partial branches: 77, 118, 130
sentry_sdk/integrations/openai_agents/init.py 100.00% 2/2 covered

Generated by Coverage Action

@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review August 4, 2026 15:09
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner August 4, 2026 15:09
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as draft August 4, 2026 15:33
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review August 5, 2026 14:03
Comment on lines +136 to +138
if use_run_hooks:
run_hooks = kwargs.get("hooks")
if run_hooks is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To confirm my understanding of what's happening here - are run hooks something that are passed in by users (which is why they could potentially be None)?

and `on_tool_end()`
Responsible for creating Execute Tool and Invoke Agent spans. Both
spans can also be closed by hooks, and Execute Tool spans are exclusively
closed in the hook.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When you say "the hook" here - is there are specific hook that you're referring to? Or do you mean this as just a generalized "run hook"?

Base automatically changed from webb/fix-run_single_turn_streamed-patch to master August 6, 2026 07:39

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

Stale Bugbot comment from a previous run.

@alexander-alderman-webb
alexander-alderman-webb force-pushed the webb/use-agent-hooks-2 branch 2 times, most recently from bbc2001 to 67b4d2b Compare November 17, 2026 09:36

@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 2 potential issues.

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 2066975. Configure here.

context: "AgentHookContext[TContext]",
agent: "Agent[TContext]",
) -> "None":
self._sentry_invoke_agent_span = invoke_agent_span(agent, context.turn_input)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shared hooks clobber concurrent agent spans

Medium Severity

The invoke-agent span is kept as a single attribute on the shared _SentryRunHooks instance created when user hooks are patched. Concurrent Runner.run calls that reuse the same RunHooks overwrite _sentry_invoke_agent_span, so one run can close the other run's span or leave a span unfinished. The previous design stored the span on the per-run context_wrapper.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2066975. Configure here.

) -> "SingleStepResult":
return await _run_single_turn(
run_loop.run_single_turn, *args, **kwargs
run_loop.run_single_turn, use_run_hooks, *args, **kwargs

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agent hooks gated on wrong version

Medium Severity

use_run_hooks turns on at openai-agents 0.3.2, but on_agent_start reads context.turn_input, which exists only on AgentHookContext from 0.6.5. On 0.3.2–0.6.4 that access fails inside capture_internal_exceptions, the monkey-patch span path is skipped, and invoke-agent spans are silently dropped.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2066975. Configure here.

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.

Use RunHooks.on_agent_start and RunHooks.on_agent_end

2 participants