Skip to content

bug: ToolCallResult auto-coerces structured tool outputs to str(), losing type information #7867

Description

@tcconnally

Bug Description

When a tool function returns a structured type (dict, list, Pydantic model, dataclass), AutoGen's ToolCallResult silently coerces it to str() via Python's default string conversion. This causes type information loss that propagates through the agent's message history and prevents downstream agents from consuming structured data.

Steps to Reproduce

from autogen_agentchat.messages import ToolCallResult, ToolCallSummaryMessage

# Tool returns structured data
def my_tool() -> dict:
    return {"status": "success", "data": {"count": 42, "items": ["a", "b", "c"]}}

# Result is coerced to str
result = ToolCallResult(
    content=[ToolCallResult(...)],  # simplified
    ...
)
# content[0].content becomes "{'status': 'success', 'data': {'count': 42, 'items': ['a', 'b', 'c']}}"
# Downstream agents must parse this string back to access the data

Expected Behavior

ToolCallResult should preserve the original Python type (or a serializable representation like JSON) so downstream agents and message processors can consume structured tool outputs without string parsing.

Actual Behavior

All non-string return values become their str() representation. Dicts become Python-repr strings (not valid JSON), lists become repr strings, and complex objects become their repr.

Impact

  • Multi-agent pipelines where one agent's tool output feeds another's input break
  • Tools returning JSON don't produce valid JSON in the message history
  • Type information from Pydantic/dataclass returns is completely lost
  • Agents must write fragile parsing code to recover structured data from repr strings

Proposed Fix

Preserve the original type when possible, or at minimum serialize to valid JSON rather than Python repr. Consider adding a content_type field to distinguish between string/structured results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions