Skip to content

Conversation

@mkmeral
Copy link
Contributor

@mkmeral mkmeral commented Jan 23, 2026

Description

This PR extends invocation_state to four core hook events that previously lacked it:

  • BeforeInvocationEvent
  • AfterInvocationEvent
  • BeforeModelCallEvent
  • AfterModelCallEvent

The invocation_state dictionary 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_state

  • AgentInitializedEvent - Fires during agent construction, before any invocation exists
  • MessageAddedEvent - Low-level notification where the message itself is self-contained

Note on deprecated structured_output_async

The deprecated structured_output_async method passes invocation_state={} since it predates this feature and doesn't accept invocation_state as a parameter. Users should migrate to agent("prompt", structured_output_model=Model) which properly supports invocation_state.

Related Issues

N/A

Documentation PR

N/A

Type of Change

New feature

Testing

  • Added unit tests verifying invocation_state is accessible in all four events

  • Updated existing hook tests to include invocation_state assertions

  • All 50+ related tests pass

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Containerized Agent added 2 commits January 7, 2026 16:48
…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={}))
Copy link
Contributor Author

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

pgrayy
pgrayy previously approved these changes Jan 23, 2026
@codecov
Copy link

codecov bot commented Jan 23, 2026

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.
@mkmeral mkmeral force-pushed the feat/invocation-state-hooks branch from 29c1a87 to c71d2b9 Compare January 23, 2026 17:35
@github-actions github-actions bot added size/m and removed size/m labels Jan 23, 2026
@dbschmigelski
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants