Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/strands/event_loop/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ async def process_stream(
elif "redactContent" in chunk:
handle_redact_content(chunk["redactContent"], state)

# Override stop_reason when the model returns "end_turn" but the response
# 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.

if stop_reason == "end_turn":
content = state["message"].get("content", [])
if any("toolUse" in item for item in content):
logger.debug("stop_reason override: end_turn -> tool_use (response contains toolUse blocks)")
stop_reason = "tool_use"

yield ModelStopReason(stop_reason=stop_reason, message=state["message"], usage=usage, metrics=metrics)


Expand Down