Skip to content

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

Open
alexander-alderman-webb wants to merge 18 commits into
webb/fix-run_single_turn_streamed-patchfrom
webb/use-agent-hooks-2
Open

ref(openai-agents): Use first class agent hooks when available#7029
alexander-alderman-webb wants to merge 18 commits into
webb/fix-run_single_turn_streamed-patchfrom
webb/use-agent-hooks-2

Conversation

@alexander-alderman-webb

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

Copy link
Copy Markdown
Contributor

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

Codecov Results 📊

97214 passed | ⏭️ 6239 skipped | Total: 103453 | Pass Rate: 93.97% | Execution Time: 358m 25s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +550
Passed Tests 📈 +502
Failed Tests
Skipped Tests 📈 +48

All tests are passing successfully.

✅ Patch coverage is 89.32%. Project has 2494 uncovered lines.
✅ Project coverage is 90%. Comparing base (base) to head (head).

Files with missing lines (3)
File Patch % Lines
sentry_sdk/integrations/openai_agents/patches/runner.py 83.02% ⚠️ 9 Missing and 5 partials
sentry_sdk/integrations/openai_agents/patches/agent_run.py 95.56% ⚠️ 2 Missing and 9 partials
sentry_sdk/integrations/openai_agents/spans/invoke_agent.py 100.00% ⚠️ 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.90%    90.00%     +0.1%
==========================================
  Files          193       193         —
  Lines        24822     24936      +114
  Branches      8924      8974       +50
==========================================
+ Hits         22315     22442      +127
- Misses        2507      2494       -13
- Partials      1417      1423        +6

Generated by Codecov Action

@alexander-alderman-webb
alexander-alderman-webb changed the base branch from webb/use-agent-hooks to webb/fix-run_single_turn_streamed-patch August 4, 2026 13:09
@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
Comment thread sentry_sdk/integrations/openai_agents/patches/runner.py
Comment thread sentry_sdk/integrations/openai_agents/patches/runner.py
Comment thread sentry_sdk/integrations/openai_agents/patches/runner.py
Comment thread sentry_sdk/integrations/openai_agents/patches/agent_run.py Outdated
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as draft August 4, 2026 15:33
Comment thread sentry_sdk/integrations/openai_agents/patches/agent_run.py
Comment thread sentry_sdk/integrations/openai_agents/patches/agent_run.py
Comment thread sentry_sdk/integrations/openai_agents/spans/invoke_agent.py
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review August 5, 2026 14:03
Comment thread sentry_sdk/integrations/openai_agents/patches/agent_run.py
Comment thread sentry_sdk/integrations/openai_agents/patches/runner.py Outdated

@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 8343310. Configure here.

Comment thread sentry_sdk/integrations/openai_agents/patches/agent_run.py
Comment thread sentry_sdk/integrations/openai_agents/patches/runner.py Outdated
Comment thread sentry_sdk/integrations/openai_agents/patches/runner.py
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

1 participant