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