[BUG] SageMakerAIModel streaming reports end_turn when an endpoint returns a tool call with finish_reason stop
Checks
- [x] I have updated to the lastest minor and patch version of Strands
- [x] I have checked the documentation and this is not expected behavior
- [x] I have searched ./issues and there are no duplicates of my issue
SDK Language
Python
Strands Version
main @ 0b68b8aa5
Steps to Reproduce
With stream: True, the streaming branch of SageMakerAIModel.stream passes finish_reason straight to message_stop. When an endpoint returns a tool call with finish_reason: "stop", the stop reason maps to end_turn.
import asyncio, json
from unittest import mock
from strands.models.sagemaker import SageMakerAIModel
tool_call = {"index": 0, "id": "t1", "type": "function",
"function": {"name": "get_weather", "arguments": '{"location": "Paris"}'}}
chunks = [
{"choices": [{"index": 0, "delta": {"tool_calls": [tool_call]}, "finish_reason": None}]},
{"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]},
]
async def main():
with mock.patch("boto3.Session") as session:
model = SageMakerAIModel(
endpoint_config={"endpoint_name": "e", "region_name": "us-east-1"},
payload_config={"max_tokens": 10, "stream": True},
)
client = session.return_value.client.return_value
client.invoke_endpoint_with_response_stream.return_value = {
"Body": [{"PayloadPart": {"Bytes": f"data: {json.dumps(c)}\n\n".encode()}} for c in chunks]
}
async for event in model.stream([{"role": "user", "content": [{"text": "weather?"}]}]):
if "messageStop" in event:
print(event)
asyncio.run(main())
Expected Behavior
{'messageStop': {'stopReason': 'tool_use'}}, the same as the non-streaming branch returns for this response.
Actual Behavior
{'messageStop': {'stopReason': 'end_turn'}}
Additional Context
The non-streaming branch already reports tool_use for stop next to a tool call. In #5619, @liramon2 confirmed that is the right behavior for their endpoints.
Possible Solution
In the streaming branch, report tool_calls when tool calls were generated and finish_reason is stop or empty. Keep length and content_filter as they are.
Related Issues
#5619
Hi @yonib05, I'd like to take this one. Plan: in the streaming branch, report tool_calls when tool calls were generated and the endpoint ended with stop or no finish reason, keeping length and content_filter as they are (same rule as your suggestion on #5619). Tests cover each finish reason plus a plain text stop. I have it ready locally and will open the PR shortly.
go for it @VISHNU0906. one pointer: for the length case don't copy the non-streaming override at strands-py/src/strands/models/sagemaker.py:619, it sets tool_calls whenever tool calls exist (see #5619).