fix: don't emit ToolStream events for non generator functions #773
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
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 aToolStreamEvent
andToolResultEvent
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
andToolResultEvent
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.
Note that we have tests verifying the e2e events emitted, including synchronous, asynchronous, and async generator tools:
sdk-python/tests/strands/agent/hooks/test_agent_events.py
Lines 15 to 30 in cb4b7fb
Related Issues
#543, #242
Documentation PR
Type of Change
Bug fix/tool streaming prep
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
hatch run prepare
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.