Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/strands/event_loop/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/strands/event_loop/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ 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"}]},
],
[
{"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"}]},
],
Expand Down
Loading