From a8535ddd4bf9c44ef38675d031ca903b34d1bbb3 Mon Sep 17 00:00:00 2001 From: vaibhav-patel Date: Sat, 20 Jun 2026 13:51:55 +0530 Subject: [PATCH] Serialize dict and list tool results as JSON 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 #7867. --- .../autogen-core/src/autogen_core/tools/_base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/python/packages/autogen-core/src/autogen_core/tools/_base.py b/python/packages/autogen-core/src/autogen_core/tools/_base.py index d2ea76e21da1..7385f48b7fc7 100644 --- a/python/packages/autogen-core/src/autogen_core/tools/_base.py +++ b/python/packages/autogen-core/src/autogen_core/tools/_base.py @@ -171,6 +171,16 @@ def return_value_as_string(self, value: Any) -> str: return json.dumps(dumped) return str(dumped) + if isinstance(value, (dict, list)): + # Serialize structured return values as valid JSON instead of their + # Python repr (e.g. ``str``) so downstream consumers receive + # double-quoted JSON rather than single-quoted repr strings. Fall + # back to ``str`` for values that are not JSON serializable. + try: + return json.dumps(value) + except (TypeError, ValueError): + return str(value) + return str(value) @abstractmethod