Serialize dict and list tool results as JSON in return_value_as_string#7873
Open
vaibhav-patel wants to merge 1 commit into
Open
Serialize dict and list tool results as JSON in return_value_as_string#7873vaibhav-patel wants to merge 1 commit into
vaibhav-patel wants to merge 1 commit into
Conversation
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.
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
Fixes #7867.
BaseTool.return_value_as_stringalready serializes PydanticBaseModelreturnvalues to JSON, but plain
dictandlistreturn values fell through tostr(), producing single-quoted Pythonreprstrings instead of valid JSON.For a tool returning
{"status": "success"}, the message history got"{'status': 'success'}"— not valid JSON — so downstream agents and messageprocessors had to parse
reprstrings to recover structured data.What
dictandlistreturn values withjson.dumps, mirroring theexisting
BaseModelbranch.str()for values that are not JSON serializable, so behavior ispreserved for those cases.
No public API change:
return_value_as_stringstill returnsstr; this onlychanges the textual form of
dict/listresults from Pythonreprto JSON.Verification
python -m py_compileon the edited file.json.loads-parseable JSON; strings andints unchanged; a
dictholding a non-serializable object falls back tostr()without raising.