diff --git a/src/strands/event_loop/streaming.py b/src/strands/event_loop/streaming.py index 74cadaf9e..1833b31bf 100644 --- a/src/strands/event_loop/streaming.py +++ b/src/strands/event_loop/streaming.py @@ -40,10 +40,12 @@ def remove_blank_messages_content_text(messages: Messages) -> Messages: # only modify assistant messages if "role" in message and message["role"] != "assistant": continue - if "content" in message: content = message["content"] has_tool_use = any("toolUse" in item for item in content) + if len(content) == 0: + content.append({"text": "[blank text]"}) + continue if has_tool_use: # Remove blank 'text' items for assistant messages @@ -272,7 +274,6 @@ async def process_stream(chunks: AsyncIterable[StreamEvent]) -> AsyncGenerator[d async for chunk in chunks: yield {"callback": {"event": chunk}} - if "messageStart" in chunk: state["message"] = handle_message_start(chunk["messageStart"], state["message"]) elif "contentBlockStart" in chunk: @@ -312,7 +313,6 @@ async def stream_messages( logger.debug("model=<%s> | streaming messages", model) messages = remove_blank_messages_content_text(messages) - chunks = model.stream(messages, tool_specs if tool_specs else None, system_prompt) async for event in process_stream(chunks): diff --git a/tests/strands/event_loop/test_streaming.py b/tests/strands/event_loop/test_streaming.py index 921fd91de..d630b353e 100644 --- a/tests/strands/event_loop/test_streaming.py +++ b/tests/strands/event_loop/test_streaming.py @@ -26,6 +26,7 @@ def moto_autouse(moto_env, moto_mock_aws): {"role": "assistant", "content": [{"text": "a"}, {"text": " \n"}, {"toolUse": {}}]}, {"role": "assistant", "content": [{"text": ""}, {"toolUse": {}}]}, {"role": "assistant", "content": [{"text": "a"}, {"text": " \n"}]}, + {"role": "assistant", "content": []}, {"role": "assistant"}, {"role": "user", "content": [{"text": " \n"}]}, ], @@ -33,6 +34,7 @@ def moto_autouse(moto_env, moto_mock_aws): {"role": "assistant", "content": [{"text": "a"}, {"toolUse": {}}]}, {"role": "assistant", "content": [{"toolUse": {}}]}, {"role": "assistant", "content": [{"text": "a"}, {"text": "[blank text]"}]}, + {"role": "assistant", "content": [{"text": "[blank text]"}]}, {"role": "assistant"}, {"role": "user", "content": [{"text": " \n"}]}, ],