fix: serialize structured (dict/list) tool return values as JSON#7932
Open
HumphreySun98 wants to merge 1 commit into
Open
fix: serialize structured (dict/list) tool return values as JSON#7932HumphreySun98 wants to merge 1 commit into
HumphreySun98 wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why are these changes needed?
BaseTool.return_value_as_stringJSON-encodes Pydantic model return values, butdictandlistreturn values fall through tostr(value), producing a Pythonrepr rather than 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
dictandlistreturn values withjson.dumps(fallingback to
str()for values that are not JSON-serializable):# {"status": "success", "items": ["a", "b"]} <- valid JSONThis is intentionally the narrow, compatibility-preserving slice discussed in the
issue: the public
content: strtext projection is kept unchanged (nomessage-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; onlydict/list(thestructured 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
contentfromstrto a structured type).Checks
ruff,mypy, andpytest tests/test_tools.pylocally).