fix: override end_turn stop reason when streaming response contains toolUse blocks#1827
Open
atian8179 wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…oolUse Some models (e.g., Sonnet 4.x on Bedrock) return end_turn as the stop reason even when the response contains toolUse content blocks. In streaming mode, the stop_reason from messageStop is used directly without checking for toolUse blocks, causing the event loop to skip tool execution. Add a post-stream check: if stop_reason is end_turn but the assembled message contains toolUse blocks, override to tool_use. This matches the existing non-streaming behavior in event_loop.py. Fixes strands-agents#1810
pgrayy
reviewed
Mar 6, 2026
| # contains toolUse blocks. Some models (e.g., Sonnet 4.x) can return | ||
| # end_turn as stop_reason even when tool calls are present, which prevents | ||
| # the event loop from processing those tool calls. | ||
| # See: https://github.com/strands-agents/sdk-python/issues/1810 |
Member
There was a problem hiding this comment.
Few call outs:
- I wouldn't worry about mentioning the ticket here in comments. That is more for the PR itself.
- We should try to fix this before the
yield ModelStreamChunkEventabove. This way the event is yielded with the correct stop reason. - This I would say warrants a warn instead of a debug.
- With this change, we should be able to remove the override logic in https://github.com/strands-agents/sdk-python/blob/main/src/strands/models/bedrock.py#L826.
- We should add tests.
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.
Problem
Some models (e.g., Sonnet 4.x on Bedrock) return
end_turnas the stop reason inmessageStopeven when the response containstoolUsecontent blocks. In streaming mode, thestop_reasonfrommessageStopis used directly without checking fortoolUseblocks, causing the event loop to skip tool execution entirely.This results in the agent returning prematurely with unexecuted tool calls.
Root Cause
In
stream_messages_async()(streaming.py), thestop_reasonfromhandle_message_stop()is yielded as-is. Unlike the non-streaming path inevent_loop.py(which checks_has_tool_use_in_latest_message), the streaming path has no corresponding override.Fix
After the streaming loop completes, check if the assembled message content contains
toolUseblocks. Ifstop_reasonisend_turnbuttoolUseblocks are present, override totool_use.Fixes #1810