From 5e69a04bcec9a54816c699251d0ba24315f4c433 Mon Sep 17 00:00:00 2001 From: jiangnan <1394485448@qq.com> Date: Mon, 16 Mar 2026 18:42:47 +0800 Subject: [PATCH] fix: add to_json to Content for serialization compatibility Content has to_dict() but lacks to_json(), causing TypeError when Azure Functions serializes Message objects containing Content instances. Add to_json() matching the pattern used by SerializationMixin. Fixes #4719 Signed-off-by: JiangNan <1394485448@qq.com> --- python/packages/core/agent_framework/_types.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/python/packages/core/agent_framework/_types.py b/python/packages/core/agent_framework/_types.py index a4e3a57330..4d1b52d14b 100644 --- a/python/packages/core/agent_framework/_types.py +++ b/python/packages/core/agent_framework/_types.py @@ -1298,6 +1298,22 @@ def to_dict(self, *, exclude_none: bool = True, exclude: set[str] | None = None) return result + def to_json(self, *, exclude_none: bool = True, exclude: set[str] | None = None, **kwargs: Any) -> str: + """Serialize the content to a JSON string. + + This is a convenience wrapper around ``to_dict()`` that returns a JSON + string via ``json.dumps()``. + + Keyword Args: + exclude_none: Whether to exclude None values. Defaults to True. + exclude: Field names to exclude from serialization. + **kwargs: Additional keyword arguments passed to ``json.dumps()``. + + Returns: + JSON string representation of the content. + """ + return json.dumps(self.to_dict(exclude_none=exclude_none, exclude=exclude), **kwargs) + def __eq__(self, other: object) -> bool: """Check if two Content instances are equal by comparing their dict representations.""" if not isinstance(other, Content):