Skip to content

fix: serialize structured (dict/list) tool return values as JSON#7932

Open
HumphreySun98 wants to merge 1 commit into
microsoft:mainfrom
HumphreySun98:fix/tool-result-json-serialization
Open

fix: serialize structured (dict/list) tool return values as JSON#7932
HumphreySun98 wants to merge 1 commit into
microsoft:mainfrom
HumphreySun98:fix/tool-result-json-serialization

Conversation

@HumphreySun98

Copy link
Copy Markdown

Why are these changes needed?

BaseTool.return_value_as_string JSON-encodes Pydantic model return values, but
dict and list return values fall through to str(value), producing a Python
repr rather than valid JSON:

def my_tool() -> dict:
    return {"status": "success", "items": ["a", "b"]}

# return_value_as_string(...) currently yields:
#   {'status': 'success', 'items': ['a', 'b']}   <- single quotes, not valid JSON

As a result, tools returning structured data do not produce valid JSON in the
message history, and downstream agents/consumers must parse Python-repr strings
to recover the data (per #7867).

This PR serializes dict and list return values with json.dumps (falling
back to str() for values that are not JSON-serializable):

#   {"status": "success", "items": ["a", "b"]}   <- valid JSON

This is intentionally the narrow, compatibility-preserving slice discussed in the
issue: the public content: str text projection is kept unchanged (no
message-schema change, so provider adapters, serializers, summary formatting,
termination conditions, and user code that expects text are unaffected). Scalars
and other types keep their existing str() behavior; only dict/list (the
structured types the issue is about) now emit valid JSON. Pydantic returns were
already JSON-encoded and are unchanged.

Related issue number

Refs #7867 (addresses the "structured outputs are not valid JSON" part; does not
change content from str to a structured type).

Checks

  • I've included any doc changes needed for https://microsoft.github.io/autogen/ (none required for this change).
  • I've added tests (if relevant) corresponding to the changes introduced in this PR.
  • I've made sure all auto checks have passed (ran ruff, mypy, and pytest tests/test_tools.py locally).

…n repr

`BaseTool.return_value_as_string` JSON-encodes Pydantic model return values but
fell through to `str(value)` for `dict` and `list` returns, producing a Python
repr (e.g. `{'status': 'success'}` with single quotes) rather than valid JSON.
As a result, tools returning structured data did not produce valid JSON in the
message history, and downstream agents/consumers had to parse repr strings to
recover the data.

Serialize `dict` and `list` return values with `json.dumps` (falling back to
`str()` for values that are not JSON-serializable). This keeps the public
`content: str` text projection unchanged — no message-schema change — while
making structured outputs valid JSON. Scalars and other types are unaffected.

Refs microsoft#7867

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant