Skip to content

Commit b1df148

Browse files
authored
fix: append blank text content if assistant content is empty (#677)
1 parent 980a988 commit b1df148

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
@@ -273,7 +275,6 @@ async def process_stream(chunks: AsyncIterable[StreamEvent]) -> AsyncGenerator[d
273275

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

315316
messages = remove_blank_messages_content_text(messages)
316-
317317
chunks = model.stream(messages, tool_specs if tool_specs else None, system_prompt)
318318

319319
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)