Skip to content

Serialize dict and list tool results as JSON in return_value_as_string#7873

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

Serialize dict and list tool results as JSON in return_value_as_string#7873
vaibhav-patel wants to merge 1 commit into
microsoft:mainfrom
vaibhav-patel:fix/7867-tool-result-json-serialization

Conversation

@vaibhav-patel

Copy link
Copy Markdown

Why

Fixes #7867.

BaseTool.return_value_as_string already serializes Pydantic BaseModel return
values to JSON, but plain dict and list return values fell through to
str(), producing single-quoted Python repr strings instead of valid JSON.
For a tool returning {"status": "success"}, the message history got
"{'status': 'success'}" — not valid JSON — so downstream agents and message
processors had to parse repr strings to recover structured data.

What

  • Serialize dict and list return values with json.dumps, mirroring the
    existing BaseModel branch.
  • Fall back to str() for values that are not JSON serializable, so behavior is
    preserved for those cases.
  • Strings and other scalar return types are unchanged.

No public API change: return_value_as_string still returns str; this only
changes the textual form of dict/list results from Python repr to JSON.

Verification

  • python -m py_compile on the edited file.
  • Manual checks: dicts/lists now emit json.loads-parseable JSON; strings and
    ints unchanged; a dict holding a non-serializable object falls back to
    str() without raising.

BaseTool.return_value_as_string already serializes Pydantic BaseModel
return values to JSON, but plain dict and list return values fell through
to str(), producing single-quoted Python repr strings rather than valid
JSON. Downstream agents and message processors then had to parse repr
strings to recover the data, and tools returning JSON-shaped data did not
produce valid JSON in the message history.

Serialize dict and list return values with json.dumps so they become
valid JSON, falling back to str() for values that are not JSON
serializable so existing behavior is preserved for those cases. Strings
and other scalar return types are unchanged.

Fixes microsoft#7867.
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.

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

1 participant