Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

zastrowm
Copy link
Member

Description

This is step step 2/N of implementing typed-events:

Related PRs:

  1. feat: Implement typed events internally #745

Migrate "callback" nested properties and explicitly passing invocation_state to behaviors on the TypedEvent:

  • TypedEvent.is_callback_event for determining if an event should be yielded and or invoked in the callback
  • TypedEvent.prepare for taking in invocation_state (and optionally merging it in)
    • This is only for backwards compatibility; certain events need to be merged in with invocation_state, and now we can move away from passing invocation_state throughout the system

Customers still only get dictionaries, as we decided that this will remain an implementation detail for the time being, but this makes the events typed all the way up until just before we yield events back to the caller

As part of this refactor, we also now strongly typed ToolResultEvent and ToolStreamEvent - neither of these are currently yielded up to the caller - that will be unlocked/opened in a future PR as part of #543

Observable changes: There should be none outside of the Agent; internally there are a couple places where we now sorely return TypedEvents

Related Issues

#242

Documentation PR

Will be once we publish typed events

Type of Change

Other (please describe): Internal refactor

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.

Move away from "callback" nested properties in the dict and explicitly passing invocation_state migrating to behaviors on the TypedEvent:

 - TypedEvent.is_callback_event for determining if an event should be yielded and or invoked in the callback
 - TypedEvent.prepare for taking in invocation_state

Customers still only get dictionaries, as we decided that this will remain an implementation detail for the time being, but this makes the events typed all the way up until *just* before we yield events back to the caller
@zastrowm zastrowm marked this pull request as ready for review August 27, 2025 13:48
@zastrowm zastrowm merged commit 6dadbce into strands-agents:main Aug 28, 2025
11 of 12 checks passed
dbschmigelski pushed a commit to dbavro19/sdk-python that referenced this pull request Aug 28, 2025
…s#755)

Move away from "callback" nested properties in the dict and explicitly passing invocation_state migrating to behaviors on the TypedEvent:

 - TypedEvent.is_callback_event for determining if an event should be yielded and or invoked in the callback
 - TypedEvent.prepare for taking in invocation_state

Customers still only get dictionaries, as we decided that this will remain an implementation detail for the time being, but this makes the events typed all the way up until *just* before we yield events back to the caller

---------

Co-authored-by: Mackenzie Zastrow <[email protected]>
@zastrowm zastrowm mentioned this pull request Aug 28, 2025
7 tasks
@zastrowm zastrowm deleted the typed_events_rev2 branch August 29, 2025 20:24
zastrowm added a commit to zastrowm/sdk-python that referenced this pull request Aug 29, 2025
Our current implementation of AgentTool.stream() has a problem that we don't differentiate between intermediate streaming events and the final ToolResult events.  Our only contract is that the last event *must be* be the tool result that is passed to the LLM.  Our switch to Typed Events (strands-agents#755) pushes us in the right direction but for backwards compatibility we can't update the signature of `AgentTool.stream()` (nor have we exposed externally TypedEvents yet). That means that if we implemented tool-streaming today, then callers would see non-generator functions yielding both a `ToolStreamEvent` and `ToolResultEvent` even though they're not actually streaming responses.

To avoid the odd behavior noted above, we'll special-case SDK-defined functions by allowing them to emit `ToolStreamEvent` and `ToolResultEvent` types directly (bypassing our normal wrapping), since they have the knowledge of when tools are actually generators or not.

There's no observable difference in behavior to callers (this is all internal behavior), but this means that when we switch the flip for Tool Streaming, non-generator tools will **not** emit ToolStreamEvents - at least for AgentTool implementations that are in the SDK.
Ratish1 pushed a commit to Ratish1/sdk-python that referenced this pull request Aug 30, 2025
…s#755)

Move away from "callback" nested properties in the dict and explicitly passing invocation_state migrating to behaviors on the TypedEvent:

 - TypedEvent.is_callback_event for determining if an event should be yielded and or invoked in the callback
 - TypedEvent.prepare for taking in invocation_state

Customers still only get dictionaries, as we decided that this will remain an implementation detail for the time being, but this makes the events typed all the way up until *just* before we yield events back to the caller

---------

Co-authored-by: Mackenzie Zastrow <[email protected]>
zastrowm added a commit that referenced this pull request Sep 3, 2025
Our current implementation of AgentTool.stream() has a problem that we don't differentiate between intermediate streaming events and the final ToolResult events.  Our only contract is that the last event *must be* be the tool result that is passed to the LLM.  Our switch to Typed Events (#755) pushes us in the right direction but for backwards compatibility we can't update the signature of `AgentTool.stream()` (nor have we exposed externally TypedEvents yet). That means that if we implemented tool-streaming today, then callers would see non-generator functions yielding both a `ToolStreamEvent` and `ToolResultEvent` even though they're not actually streaming responses.

To avoid the odd behavior noted above, we'll special-case SDK-defined functions by allowing them to emit `ToolStreamEvent` and `ToolResultEvent` types directly (bypassing our normal wrapping), since they have the knowledge of when tools are actually generators or not.

There's no observable difference in behavior to callers (this is all internal behavior), but this means that when we switch the flip for Tool Streaming, non-generator tools will **not** emit ToolStreamEvents - at least for AgentTool implementations that are in the SDK.

Co-authored-by: Mackenzie Zastrow <[email protected]>
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.

2 participants