Skip to content

Commit 102e0fc

Browse files
afarntrogdbschmigelski
authored andcommitted
fix(agent): Return structured output JSON when AgentResult has no text (#1290)
* fix(agent): Return structured output JSON when AgentResult has no text When AgentResult has no text content but structured_output is present, __str__() now returns the JSON serialization of the structured output instead of an empty string. This fixes output propagation in multi-agent graphs where structured output was being lost. Changes: - Modified AgentResult.__str__() to fall back to structured_output.model_dump_json() - Added unit test test__str__empty_message_with_structured_output to verify fix - All existing tests pass, maintaining backward compatibility #1118
1 parent cfa06c7 commit 102e0fc

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/strands/agent/agent_result.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def __str__(self) -> str:
3838
"""Get the agent's last message as a string.
3939
4040
This method extracts and concatenates all text content from the final message, ignoring any non-text content
41-
like images or structured data.
41+
like images or structured data. If there's no text content but structured output is present, it serializes
42+
the structured output instead.
4243
4344
Returns:
4445
The agent's last message as a string.
@@ -49,6 +50,10 @@ def __str__(self) -> str:
4950
for item in content_array:
5051
if isinstance(item, dict) and "text" in item:
5152
result += item.get("text", "") + "\n"
53+
54+
if not result and self.structured_output:
55+
result = self.structured_output.model_dump_json()
56+
5257
return result
5358

5459
@classmethod

tests/strands/agent/test_agent_result.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,27 @@ def test__str__with_structured_output(mock_metrics, simple_message: Message):
201201
assert message_string == "Hello world!\n"
202202
assert "test" not in message_string
203203
assert "42" not in message_string
204+
205+
206+
def test__str__empty_message_with_structured_output(mock_metrics, empty_message: Message):
207+
"""Test that str() returns structured output JSON when message has no text content."""
208+
structured_output = StructuredOutputModel(name="example", value=123, optional_field="optional")
209+
210+
result = AgentResult(
211+
stop_reason="end_turn",
212+
message=empty_message,
213+
metrics=mock_metrics,
214+
state={},
215+
structured_output=structured_output,
216+
)
217+
218+
# When message has no text content, str() should return structured output as JSON
219+
message_string = str(result)
220+
221+
# Verify it's the same as the structured output's JSON representation
222+
assert message_string == structured_output.model_dump_json()
223+
224+
# Verify it contains the expected data
225+
assert "example" in message_string
226+
assert "123" in message_string
227+
assert "optional" in message_string

0 commit comments

Comments
 (0)