Skip to content

fix: override end_turn stop reason when streaming response contains toolUse blocks#1827

Open
atian8179 wants to merge 1 commit intostrands-agents:mainfrom
atian8179:fix/streaming-tool-use-stop-reason-override
Open

fix: override end_turn stop reason when streaming response contains toolUse blocks#1827
atian8179 wants to merge 1 commit intostrands-agents:mainfrom
atian8179:fix/streaming-tool-use-stop-reason-override

Conversation

@atian8179
Copy link

Problem

Some models (e.g., Sonnet 4.x on Bedrock) return end_turn as the stop reason in messageStop 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 entirely.

This results in the agent returning prematurely with unexecuted tool calls.

Root Cause

In stream_messages_async() (streaming.py), the stop_reason from handle_message_stop() is yielded as-is. Unlike the non-streaming path in event_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 toolUse blocks. If stop_reason is end_turn but toolUse blocks are present, override to tool_use.

Fixes #1810

…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
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few call outs:

  1. I wouldn't worry about mentioning the ticket here in comments. That is more for the PR itself.
  2. We should try to fix this before the yield ModelStreamChunkEvent above. This way the event is yielded with the correct stop reason.
  3. This I would say warrants a warn instead of a debug.
  4. 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.
  5. We should add tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Streaming has_tool_use tracking fails to trigger end_turn → tool_use stop reason override in certain edge cases

2 participants