Skip to content

feat(multiagent): Add __str__ support for MultiAgentResult and NodeResult#1998

Open
dosvk wants to merge 2 commits intostrands-agents:mainfrom
dosvk:feat/multiagent-result-str
Open

feat(multiagent): Add __str__ support for MultiAgentResult and NodeResult#1998
dosvk wants to merge 2 commits intostrands-agents:mainfrom
dosvk:feat/multiagent-result-str

Conversation

@dosvk
Copy link
Copy Markdown

@dosvk dosvk commented Mar 27, 2026

Add __str__ methods to MultiAgentResult and NodeResult for human-readable
string output, consistent with the existing AgentResult.__str__ behavior.

NodeResult.__str__ delegates to its inner result — AgentResult, nested
MultiAgentResult, or Exception.

MultiAgentResult.__str__ follows the same priority order as AgentResult.__str__:

  1. Interrupts (if present) → stringified list of interrupt dicts
  2. Node results → each node's text output, prefixed with node name

This addresses the review feedback from #1568 regarding matching the priority
order from AgentResult.__str__.

Related Issues

Closes #1561

Type of Change

New feature

Testing

  • Added 5 unit tests covering all code paths: AgentResult delegation,
    Exception delegation, node iteration, interrupt priority, and empty results
  • 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.

…sult

Add human-readable string representations to MultiAgentResult and
NodeResult, consistent with the existing AgentResult.__str__ behavior.

NodeResult.__str__ delegates to its inner result (AgentResult,
MultiAgentResult, or Exception). MultiAgentResult.__str__ prioritizes
interrupts, then outputs each node's text prefixed with its name.

This makes print(result) produce clean output instead of the default
dataclass repr.

Closes strands-agents#1561
@mkmeral
Copy link
Copy Markdown
Contributor

mkmeral commented Mar 28, 2026

/strands review

@github-actions
Copy link
Copy Markdown

Assessment: Approve

Clean implementation that follows the established AgentResult.__str__ pattern. The priority order for interrupts is consistent with the existing behavior and the delegation pattern in NodeResult is simple and effective.

Review Details
  • Code Quality: Well-structured with proper type annotations and Google-style docstrings
  • API Consistency: Correctly mirrors AgentResult.__str__ priority order (interrupts → content)
  • Testing: 5 tests cover the primary code paths; consider adding tests for multiple nodes and nested MultiAgentResult for completeness

Suggestion for improved test coverage:

Consider adding tests for:

  1. Multiple nodes - tests the for loop and newline joining in MultiAgentResult.__str__
  2. Nested MultiAgentResult - tests the recursive case mentioned in the docstring

Example tests:

def test_multi_agent_result_str_multiple_nodes():
    """Test MultiAgentResult.__str__ with multiple nodes."""
    ar1 = AgentResult(
        message={"role": "assistant", "content": [{"text": "Response 1"}]},
        stop_reason="end_turn", state={}, metrics={},
    )
    ar2 = AgentResult(
        message={"role": "assistant", "content": [{"text": "Response 2"}]},
        stop_reason="end_turn", state={}, metrics={},
    )
    result = MultiAgentResult(
        status=Status.COMPLETED,
        results={"node1": NodeResult(result=ar1), "node2": NodeResult(result=ar2)},
    )
    output = str(result)
    assert "node1: Response 1" in output
    assert "node2: Response 2" in output
    assert "\n" in output


def test_node_result_str_with_nested_multiagent():
    """Test NodeResult.__str__ with nested MultiAgentResult."""
    inner_ar = AgentResult(
        message={"role": "assistant", "content": [{"text": "Nested response"}]},
        stop_reason="end_turn", state={}, metrics={},
    )
    inner_mar = MultiAgentResult(
        status=Status.COMPLETED,
        results={"inner_node": NodeResult(result=inner_ar)},
    )
    outer_node = NodeResult(result=inner_mar)
    assert "inner_node: Nested response" in str(outer_node)

…esult

Add two additional tests addressing review feedback:
- Multiple nodes: verifies for loop and newline joining
- Nested MultiAgentResult: verifies recursive delegation in NodeResult
@github-actions github-actions bot added size/m and removed size/s labels Mar 28, 2026
@dosvk dosvk requested a deployment to manual-approval March 28, 2026 02:32 — with GitHub Actions Waiting
@dosvk dosvk requested a deployment to manual-approval March 28, 2026 02:32 — with GitHub Actions Waiting
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.

[FEATURE] __str__ support for MultiAgentResult

2 participants