Skip to content

Commit eedd865

Browse files
committed
fix: append blank text content if assistant content is empty
1 parent 606f657 commit eedd865

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/strands/event_loop/streaming.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ def remove_blank_messages_content_text(messages: Messages) -> Messages:
4040
# only modify assistant messages
4141
if "role" in message and message["role"] != "assistant":
4242
continue
43-
4443
if "content" in message:
4544
content = message["content"]
4645
has_tool_use = any("toolUse" in item for item in content)
46+
if len(content) == 0:
47+
content.append({"text": "[blank text]"})
48+
continue
4749

4850
if has_tool_use:
4951
# Remove blank 'text' items for assistant messages
@@ -272,7 +274,6 @@ async def process_stream(chunks: AsyncIterable[StreamEvent]) -> AsyncGenerator[d
272274

273275
async for chunk in chunks:
274276
yield {"callback": {"event": chunk}}
275-
276277
if "messageStart" in chunk:
277278
state["message"] = handle_message_start(chunk["messageStart"], state["message"])
278279
elif "contentBlockStart" in chunk:
@@ -312,7 +313,6 @@ async def stream_messages(
312313
logger.debug("model=<%s> | streaming messages", model)
313314

314315
messages = remove_blank_messages_content_text(messages)
315-
316316
chunks = model.stream(messages, tool_specs if tool_specs else None, system_prompt)
317317

318318
async for event in process_stream(chunks):

tests/strands/event_loop/test_streaming.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ def moto_autouse(moto_env, moto_mock_aws):
2626
{"role": "assistant", "content": [{"text": "a"}, {"text": " \n"}, {"toolUse": {}}]},
2727
{"role": "assistant", "content": [{"text": ""}, {"toolUse": {}}]},
2828
{"role": "assistant", "content": [{"text": "a"}, {"text": " \n"}]},
29+
{"role": "assistant", "content": []},
2930
{"role": "assistant"},
3031
{"role": "user", "content": [{"text": " \n"}]},
3132
],
3233
[
3334
{"role": "assistant", "content": [{"text": "a"}, {"toolUse": {}}]},
3435
{"role": "assistant", "content": [{"toolUse": {}}]},
3536
{"role": "assistant", "content": [{"text": "a"}, {"text": "[blank text]"}]},
37+
{"role": "assistant", "content": [{"text": "[blank text]"}]},
3638
{"role": "assistant"},
3739
{"role": "user", "content": [{"text": " \n"}]},
3840
],

0 commit comments

Comments
 (0)