ref(openai-agents): Use first class agent hooks when available - #7029
ref(openai-agents): Use first class agent hooks when available#7029alexander-alderman-webb wants to merge 39 commits into
Conversation
Codecov Results 📊✅ 99162 passed | ⏭️ 6479 skipped | Total: 105641 | Pass Rate: 93.87% | Execution Time: 357m 23s 📊 Comparison with Base Branch
➖ Removed Tests (1)View removed tests
All tests are passing successfully. ✅ Patch coverage is 89.32%. Project has 2505 uncovered lines. Files with missing lines (3)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 89.96% 89.99% +0.03%
==========================================
Files 193 193 —
Lines 24955 25016 +61
Branches 9004 9030 +26
==========================================
+ Hits 22448 22511 +63
- Misses 2507 2505 -2
- Partials 1437 1437 —Generated by Codecov Action |
| if use_run_hooks: | ||
| run_hooks = kwargs.get("hooks") | ||
| if run_hooks is not None: |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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"?
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 327a4d8. Configure here.

Description
Add
on_agent_start(),on_agent_end()andon_handoff()hooks.Unlike tool hooks, the Invoke Agent span is stored on the
RunHooksinstance.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 theRunHooksinstance 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 theon_agent_start()andon_agent_end()hooks.Issues
Closes #6989
Reminders
uv run ruff.feat:,fix:,ref:,meta:)