From 3ad20b4652bcbe9cb7b6ac1e13b303175c7f9e43 Mon Sep 17 00:00:00 2001 From: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com> Date: Wed, 23 Sep 2026 18:52:20 +0530 Subject: [PATCH] Python: fix(core): handle None text safely in Message.text property Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com> --- python/packages/core/agent_framework/_types.py | 4 +++- python/packages/core/tests/core/test_types.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/python/packages/core/agent_framework/_types.py b/python/packages/core/agent_framework/_types.py index 17dba475e3e..6d8b097f3ac 100644 --- a/python/packages/core/agent_framework/_types.py +++ b/python/packages/core/agent_framework/_types.py @@ -1932,7 +1932,9 @@ def text(self) -> str: Remarks: This property concatenates the text of all TextContent objects in Content. """ - return " ".join(content.text for content in self.contents if content.type == "text") # type: ignore[misc] + return " ".join( + content.text for content in self.contents if content.type == "text" and content.text is not None + ) AgentRunInputs = str | Content | Message | Sequence[str | Content | Message] diff --git a/python/packages/core/tests/core/test_types.py b/python/packages/core/tests/core/test_types.py index a6797b30449..7e3b2274b36 100644 --- a/python/packages/core/tests/core/test_types.py +++ b/python/packages/core/tests/core/test_types.py @@ -2562,6 +2562,23 @@ def test_chat_message_complex_content_serialization(): assert reconstructed.contents[2].type == "function_result" +def test_message_text_handles_none_text_contents() -> None: + """Message.text should filter out text content whose text is None without raising TypeError.""" + msg = Message( + role="assistant", + contents=[ + Content("text", text=None), + Content.from_text("Hello"), + Content("text", text=None), + Content.from_text("world"), + ], + ) + assert msg.text == "Hello world" + + empty_msg = Message(role="user", contents=[Content("text", text=None)]) + assert empty_msg.text == "" + + def test_message_roundtrip_preserves_compaction_annotation_dict() -> None: message = Message( role="assistant",