-
Notifications
You must be signed in to change notification settings - Fork 606
feat(hooks): Add invocation state #1550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…Event, BeforeModelCallEvent, and AfterModelCallEvent This change extends the invocation_state attribute (already present in tool-related hook events) to the four core hook events as scoped in issue strands-agents#914: - BeforeInvocationEvent - AfterInvocationEvent - BeforeModelCallEvent - AfterModelCallEvent Changes: - Added invocation_state: dict[str, Any] attribute to the 4 target event classes in hooks/events.py - Updated event creation in agent.py to pass invocation_state to Before/AfterInvocationEvent - Updated event creation in event_loop.py to pass invocation_state to Before/AfterModelCallEvent - Updated all affected tests to use invocation_state parameter - Added new tests to verify invocation_state is properly accessible in the events This enables better multi-agent coordination by allowing hooks to access shared context throughout the agent invocation lifecycle, including request tracking, dynamic configuration, and context-aware processing. Addresses: strands-agents#914
Combined invocation_state feature with messages attribute from main: - BeforeInvocationEvent now has both invocation_state and messages attributes - AfterInvocationEvent, BeforeModelCallEvent, AfterModelCallEvent have invocation_state - Updated tests to verify both features work together
| stacklevel=2, | ||
| ) | ||
| await self.hooks.invoke_callbacks_async(BeforeInvocationEvent(agent=self)) | ||
| await self.hooks.invoke_callbacks_async(BeforeInvocationEvent(agent=self, invocation_state={})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is old structured input. It's deprecated and it doesn't allow to pass invocation state, but we pass empty dict for completeness
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…Event, BeforeModelCallEvent, and AfterModelCallEvent
- Add invocation_state attribute with default_factory=dict for backwards compatibility
- Pass invocation_state through in agent.py and event_loop.py
- Add tests for invocation_state accessibility in all four events
- Update existing tests to use ANY matcher for invocation_state comparisons
Note: The deprecated structured_output_async method passes invocation_state={} since
it predates this feature and doesn't accept invocation_state as a parameter.
29c1a87 to
c71d2b9
Compare
|
Pull Request and Push Action / check-api (pull_request)F is calling out a breaking change. Under most circumstances I'd agree, but in this case Hook objects are constructed by the framework and "distributed" to implementations defined by customers. So afaict this should not be truly breaking |
Description
This PR extends
invocation_stateto four core hook events that previously lacked it:BeforeInvocationEventAfterInvocationEventBeforeModelCallEventAfterModelCallEventThe
invocation_statedictionary was already available in tool-related events (BeforeToolCallEvent,AfterToolCallEvent), but not accessible during the broader invocation and model call phases. This made it difficult to implement cross-cutting concerns like request tracking, multi-agent coordination, and context-aware logging throughout the entire request lifecycle.Events intentionally without
invocation_stateAgentInitializedEvent- Fires during agent construction, before any invocation existsMessageAddedEvent- Low-level notification where the message itself is self-containedNote on deprecated
structured_output_asyncThe deprecated
structured_output_asyncmethod passesinvocation_state={}since it predates this feature and doesn't acceptinvocation_stateas a parameter. Users should migrate toagent("prompt", structured_output_model=Model)which properly supportsinvocation_state.Related Issues
N/A
Documentation PR
N/A
Type of Change
New feature
Testing
Added unit tests verifying
invocation_stateis accessible in all four eventsUpdated existing hook tests to include
invocation_stateassertionsAll 50+ related tests pass
I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.