Skip to content

Conversation

zastrowm
Copy link
Owner

@zastrowm zastrowm commented Aug 29, 2025

Description

Enable decorated tools to be an async generator, enabling streaming of tool events back to to the caller. Assuming strands-agents#773 is merged, the changes needed are fairly minor, as currently we're just swallowing tool stream events.

Implements strands-agents#543

Api Bar-raising

The tool event being emitted (as a dictionary) needs to go through bar-raising.

Proposed new Event

(event type is purely our internal name).

ToolStreamEvent:

  • tool_stream_tool_use: ToolUse - the tool use that emitted the event
  • tool_stream_data: Any - the data that was streamed/yielded from the tool

Our existing events are as follows:

Event Type Dictionary Properties
InitEventLoopEvent init_event_loop: bool
StartEvent start: bool
StartEventLoopEvent start_event_loop: bool
ModelStreamChunkEvent event: StreamEvent
ToolUseStreamEvent delta: ContentBlockDelta
current_tool_use: dict[str, Any]
TextStreamEvent data: str,
delta: ContentBlockDelta
ReasoningTextStreamEvent reasoningText: str | None
delta: ContentBlockDelta
reasoning: bool
ReasoningSignatureStreamEvent reasoning_signature: str | None,
delta: ContentBlockDelta,
reasoning: bool
EventLoopThrottleEvent event_loop_throttled_delay: int
ModelMessageEvent message: Message
ToolResultMessageEvent message: Any
ForceStopEvent force_stop: bool,
force_stop_reason: str
AgentResultEvent result: AgentResult

Related Issues

strands-agents#543

Documentation PR

Type of Change

Bug fix
New feature
Breaking change
Documentation update
Other (please describe):

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.

JackYPCOnline and others added 5 commits August 30, 2025 05:15
* fix: fix loading tools with same tool name

* simplify if condition

---------

Co-authored-by: Jack Yuan <[email protected]>
…s-agents#773)

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.

Co-authored-by: Mackenzie Zastrow <[email protected]>
Enable decorated tools to be an async generator, enabling streaming of tool events back to to the caller.
@zastrowm zastrowm force-pushed the enable_tool_streaming branch from 65f93f3 to b9be087 Compare September 3, 2025 18:37
@zastrowm zastrowm closed this Sep 3, 2025
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