feat: add before_run and after_run hook points to Agent - #11985
Merged
Conversation
before_run fires once per run after state initialization and before the first LLM call. after_run fires once per run after the step loop ends, before the Agent builds its return value, regardless of whether the run stopped on an exit condition or max_agent_steps (unlike on_exit). Fixes deepset-ai/haystack-private#455 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Proposed Changes:
Adds
before_runandafter_runhook points to theAgent:before_run: runs exactly once per run, after the state is initialized and before first LLM call. Use it to rewrite the initial messages or seed state without re-running on every step likebefore_llmdoes. Removes the need for theif state.get("brief"): returnguard in the agent-packScopeHook.after_run: runs exactly once per run, after the step loop has ended and before the Agent builds its return value — regardless of whether the run stopped on an exit condition or becausemax_agent_stepswas reached (unlikeon_exit). State mutations are reflected in the returnedmessages/last_messageandstate_schemaoutputs — so aWriteHook-style report writer can no longer be skipped when the step budget is exhausted. Settingcontinue_runhere has no effect.Implementation details:
HookPointLiteral andBEFORE_RUN/AFTER_RUNconstants extended inhaystack/hooks/protocol.pyand exported fromhaystack.hooks. Validation, serialization,allowed_hook_points, and warm-up/close lifecycle all pick the new points up generically.Agent.runandAgent.run_async, inside the tracing span (soafter_runmutations are captured in the output content tag). No new reserved state keys.hooks.mdxandagent.mdxindocs-website, plus thehooksparameter docstring inAgent.__init__.How did you test it?
test/components/agents/test_agent_hooks.pyNotes for the reviewer
after_runinvocation is placed before_public_outputs(...)so hooks can shape the returned result like the WriteHook in agent-pack.on_exitsemantics are unchanged; its doc bullet now cross-referencesafter_runfor logic that must run however the run ends.ScopeHook/WriteHookcan drop their guards and switchallowed_hook_pointsto the new points once this ships.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.🤖 Generated with Claude Code