feat(agent): add stream_final_turn_only parameter to stream_async#2104
Open
zhifanl wants to merge 1 commit intostrands-agents:mainfrom
Open
feat(agent): add stream_final_turn_only parameter to stream_async#2104zhifanl wants to merge 1 commit intostrands-agents:mainfrom
zhifanl wants to merge 1 commit intostrands-agents:mainfrom
Conversation
Add a stream_final_turn_only parameter to Agent.stream_async that buffers intermediate turn text events and only yields text from the final model turn. Non-text events (lifecycle, tool use, reasoning, citations) pass through unchanged. Closes strands-agents#2055
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
When using
stream_asyncwith tool-using agents, text events from every model turn are yielded to the caller — including intermediate reasoning before tool calls. For production chat UIs and SSE endpoints, this is noise. The only workaround today requires consumers to implement fragile buffering logic that depends on SDK internals likestart_event_loop, rawmessageStopevents, and theend_turn→tool_useoverride.This adds a first-class SDK option to stream only the final answer, eliminating the need for consumer-side buffering.
Resolves: #2055
Public API Changes
Agent.stream_asyncaccepts a newstream_final_turn_onlykeyword argument:When
stream_final_turn_only=True, intermediate turn text events are buffered internally and discarded when the turn ends with tool use. Text from the final turn (wherestop_reason == "end_turn") is flushed to both the caller and callback handler. Non-text events (lifecycle, tool use, reasoning, citations, model stream chunks) pass through unchanged regardless of this setting.Default is
False— fully backward compatible, no behavior change unless opted in.Use Cases
Related Issues
#2055
Type of Change
New feature
Testing
hatch run prepareAll test passed
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.