Skip to content

Conversation

@afarntrog
Copy link
Contributor

@afarntrog afarntrog commented Dec 4, 2025

Description

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 the following two issues:

  1. When running print(agent_result) on an AgentResult while using structured output - the user will see the Agent’s message that invokes the tool with the toolUse. This is demonstrated with the following code:
from pydantic import BaseModel, Field
from strands import Agent


class Foo(BaseModel):
    foo: Annotated[str, Field(min_length=1, max_length=10)]
    bar: Annotated[str, Field(min_length=1, max_length=10)]


agent = Agent(
    system_prompt="You are a helpful assistant that converts data into Foo.",
    structured_output_model=Foo,
)
res = first_agent("Hello")

Output

print(res)
>>> Hello! I need to provide you with a structured output using the Foo tool. Let me create a simple example for you.

New Output:

print(res)
{"foo":"hello","bar":"world"}
  1. Output propagation in multi-agent graphs where structured output was being lost:
    In graph.py we are stringifying the result and adding it to the node_input before this change, the result would be empty. Now it'll contain the structured output json result.
        for dep_id, node_result in dependency_results.items():
            node_input.append(ContentBlock(text=f"\nFrom {dep_id}:"))
            # Get all agent results from this node (flattened if nested)
            agent_results = node_result.get_agent_results()
            for result in agent_results:
                agent_name = getattr(result, "agent_name", "Agent")
                result_text = str(result)
                node_input.append(ContentBlock(text=f"  - {agent_name}: {result_text}"))

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

Related Issues

#1118

Documentation PR

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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

strands-agents#1118
@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

zastrowm
zastrowm previously approved these changes Dec 5, 2025
Copy link
Member

@zastrowm zastrowm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness (and my curiosity) can you add the new output of your example code to the PR description

@afarntrog afarntrog merged commit 911a1c7 into strands-agents:main Dec 5, 2025
15 of 16 checks passed
dbschmigelski pushed a commit to dbschmigelski/sdk-python that referenced this pull request Dec 9, 2025
strands-agents#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

strands-agents#1118
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants