fix: override end_turn stop reason when streaming response contains toolUse blocks#1827
Open
atian8179 wants to merge 2 commits 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
src/strands/event_loop/streaming.py
Outdated
| # 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.
Author
There was a problem hiding this comment.
Thanks for the detailed review! Pushed updates addressing all points:
- Removed ticket reference from code comments
- Moved the override into the
messageStophandler so the event is yielded with the correct stop reason - Changed from
logger.debugtologger.warning - Removed the duplicate override logic in
bedrock.py— now handled centrally instreaming.py - Will add tests — working on it
…, remove bedrock override - Move stop_reason override before yield so ModelStreamChunkEvent has correct stop reason - Change log level from debug to warning - Remove duplicate override logic in bedrock.py (now handled centrally) - Remove ticket reference from code comments (belongs in PR)
pgrayy
reviewed
Mar 8, 2026
| content = state["message"].get("content", []) | ||
| if any("toolUse" in item for item in content): | ||
| logger.warning( | ||
| "stop_reason override: end_turn -> tool_use (response contains toolUse blocks)" |
pgrayy
reviewed
Mar 8, 2026
| callback(chunk) | ||
| # The stop_reason override for end_turn -> tool_use is now | ||
| # handled centrally in streaming.py so no model-specific | ||
| # fixup is needed here. |
Member
There was a problem hiding this comment.
I wouldn't worry about adding this comment here.
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