Skip to content

Conversation

@athewsey
Copy link
Contributor

@athewsey athewsey commented Oct 8, 2025

Description

Add the incoming messages to BeforeInvocationEvent, so that this hook can be used for implementing guardrails that check (and perhaps redact) user inputs before kicking off the event loop or raising MessageAddedEvents.

Related Issues

#1006

Documentation PR

None yet - Should be some small updates required if we go ahead. I can work on this if reviewers are OK with the change in principle?

Type of Change

New feature

Testing

Updated existing unit tests to validate the messages are passed through correctly (both in structured output and normal agent invocation).

⚠️ For now I have not added any test to explicitly validate that if a BeforeInvocationEvent hook edits the input messages in place, these will be reflected in the rest of the invocation. Do we want to commit to this?

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • (⚠️ See note above) 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.

@athewsey athewsey changed the title feat: expose messages to BeforeInvocationEvent Expose input messages to BeforeInvocationEvent Oct 8, 2025
Add incoming messages to BeforeInvocationEvent so this hook can be
used for guardrails aiming to check (and perhaps redact) user inputs
before kicking off the event loop.
Copy link
Contributor

@mkmeral mkmeral left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, and I'm sorry for the delayed response.

The feature and PR makes a lot of sense, I just had 2 small comments.

self.hooks.invoke_callbacks(BeforeInvocationEvent(agent=self))
if not self.messages and not prompt:
raise ValueError("No conversation history or prompt provided")
temp_messages: Messages = self.messages + self._convert_prompt_to_messages(prompt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just send the new messages? I think that would allow devs to differentiate between new messages (what we pass), and agent.messages (what already exists in history).

with self.tracer.tracer.start_as_current_span(
"execute_structured_output", kind=trace_api.SpanKind.CLIENT
"execute_structured_output",
attributes={
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to update the span?

@Unshure
Copy link
Member

Unshure commented Jan 9, 2026

Apologies for the delay on this issue, but we have made some significant changes since this was submitted. We need to rebase this pr in order to fit with the current state of the codebase.

@cagataycali
Copy link
Member

Hey @athewsey! 👋

I noticed this PR has merge conflicts. The feat/agent-interface PR (#1126) was recently merged, which likely caused the conflict.

Since this is a valuable feature (tagged as ready for contribution and paper cut), I'd be happy to help rebase this if you'd like - just let me know!

The feature makes a lot of sense for implementing early guardrails on user input before it gets added to memory. I saw @mkmeral's feedback from Jan 8th - looks like the PR just needs:

  1. Rebase onto latest main
  2. Address the two small comments from the review

Would love to see this land! 🚀


Comment from strands-coder autonomous agent 🦆

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants