Skip to content

Expose input messages to BeforeInvocationEvent hook#1474

Merged
Unshure merged 5 commits intostrands-agents:mainfrom
Unshure:agent-tasks/8
Jan 15, 2026
Merged

Expose input messages to BeforeInvocationEvent hook#1474
Unshure merged 5 commits intostrands-agents:mainfrom
Unshure:agent-tasks/8

Conversation

@Unshure
Copy link
Member

@Unshure Unshure commented Jan 13, 2026

Description

Add the message parameter to the BeforeInvokeHook to provide access to the agent invocation inputs. Today the BeforeInvokeHook only provides a reference to agent, but this is before the initial message was added to the messages array, so this information is essentially inaccessible at this point. By providing access to messages in this hook, preprocessing can occur before the agent is ever actually invoked (like guardrails or filters).

Related Issues

#1006

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • 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.

Add messages attribute to BeforeInvocationEvent to enable input-side
guardrails for PII detection, content moderation, and prompt attack
prevention. Hooks can now inspect and modify messages before they are
added to the agent's conversation history.

- Add writable messages attribute to BeforeInvocationEvent (None default)
- Pass messages parameter from _run_loop() to BeforeInvocationEvent
- Add unit tests for new messages attribute and writability
- Add integration tests for message modification use case
- Update docs/HOOKS.md with input guardrails documentation

Resolves #8
- Remove detailed Input Guardrails section from docs/HOOKS.md
- Simplify BeforeInvocationEvent docstring per review
- Remove backward compatibility note from messages attribute
- Remove no-op test for messages initialization
@codecov
Copy link

codecov bot commented Jan 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Unshure Unshure changed the title Agent tasks/8 Expose input messages to BeforeInvocationEvent hook Jan 13, 2026
Use concise equality comparison for BeforeInvocationEvent assertions
instead of verbose instance checks and property assertions.
JackYPCOnline
JackYPCOnline previously approved these changes Jan 14, 2026
@Unshure Unshure merged commit dfe3ec7 into strands-agents:main Jan 15, 2026
16 of 17 checks passed
manoj-selvakumar5 pushed a commit to manoj-selvakumar5/strands-sdk-python that referenced this pull request Feb 5, 2026
…1474)

* feat(hooks): expose input messages to BeforeInvocationEvent

Add messages attribute to BeforeInvocationEvent to enable input-side
guardrails for PII detection, content moderation, and prompt attack
prevention. Hooks can now inspect and modify messages before they are
added to the agent's conversation history.

- Add writable messages attribute to BeforeInvocationEvent (None default)
- Pass messages parameter from _run_loop() to BeforeInvocationEvent
- Add unit tests for new messages attribute and writability
- Add integration tests for message modification use case
- Update docs/HOOKS.md with input guardrails documentation

Resolves strands-agents#8

* refactor: address review feedback

- Remove detailed Input Guardrails section from docs/HOOKS.md
- Simplify BeforeInvocationEvent docstring per review
- Remove backward compatibility note from messages attribute
- Remove no-op test for messages initialization

* refactor: simplify test assertions per review

Use concise equality comparison for BeforeInvocationEvent assertions
instead of verbose instance checks and property assertions.

* Use overwritten messages array for the agent

* Fix mypy issue

---------

Co-authored-by: Strands Agent <217235299+strands-agent@users.noreply.github.com>
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.

6 participants