generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 621
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
Merged
mkmeral
merged 4 commits into
strands-agents:main
from
mkmeral:feat/invocation-state-hooks
Jan 28, 2026
Merged
feat(hooks): Add invocation state #1550
mkmeral
merged 4 commits into
strands-agents:main
from
mkmeral:feat/invocation-state-hooks
Jan 28, 2026
Conversation
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
…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
mkmeral
commented
Jan 23, 2026
pgrayy
previously approved these changes
Jan 23, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
dbschmigelski
requested changes
Jan 23, 2026
…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
pgrayy
approved these changes
Jan 23, 2026
dbschmigelski
approved these changes
Jan 23, 2026
Member
|
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 |
Unshure
approved these changes
Jan 26, 2026
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.
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.