feat(hooks): accept callable hook callbacks in Agent constructor #1992
Open
agent-of-mkmeral wants to merge 2 commits intostrands-agents:mainfrom
Open
feat(hooks): accept callable hook callbacks in Agent constructor #1992agent-of-mkmeral wants to merge 2 commits intostrands-agents:mainfrom
agent-of-mkmeral wants to merge 2 commits intostrands-agents:mainfrom
Conversation
…s parameter
Previously, the hooks parameter in Agent.__init__ only accepted
HookProvider instances. This change allows passing plain callable
hook callbacks (functions with typed event parameters) directly,
matching the flexibility of Agent.add_hook().
The hooks param now accepts a mixed list of HookProvider instances
and/or callable hook callbacks:
def on_start(event: BeforeInvocationEvent) -> None:
print('Starting!')
agent = Agent(hooks=[on_start, MyHookProvider()])
This provides a more convenient API for simple hook use cases where
creating a full HookProvider class is unnecessary.
Changes:
- Updated hooks param type: list[HookProvider | HookCallback] | None
- Added isinstance check to dispatch HookProviders vs callables
- Added ValueError for invalid hook types
- Added comprehensive tests (12 test cases)
Contributor
|
/strands review |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
|
@agent-of-mkmeral did you not run |
|
Assessment: ✅ Approve This PR addresses a legitimate usability gap by enabling the Review Summary
Well done on the clear PR description with before/after examples! |
- Remove unused variable assignments (F841 lint errors) - Apply auto-formatting from hatch run format
Contributor
Author
|
Fixed! Ran Commit: Issues found & fixed
Verification
|
mkmeral
approved these changes
Mar 27, 2026
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.
Motivation
Currently, the
hooksparameter inAgent.__init__only acceptsHookProviderinstances:However,
Agent.add_hook()(added in #1706) already supports plain callables:This inconsistency is confusing. If you can register a callable via
add_hook(), you should also be able to pass it via the constructor.Changes
src/strands/agent/agent.py:hooksparam type fromlist[HookProvider]tolist[HookProvider | HookCallback]HookProvider→add_hook(), callable →add_callback(None, hook)ValueErrorfor invalid hook types with clear error messageAfter This Change
Callables must have typed event parameters (same requirement as
agent.add_hook()). Lambdas without type hints will raiseValueErrorwith a helpful message.Tests
Added 12 new test cases in
tests/strands/agent/test_agent_hooks_callable.py:All 24 existing hook tests + 61 hook registry tests continue to pass.
Related
add_hookconvenience method)cc @mkmeral