From 6ddc7030b4c6c08f62ab079618793d21b0bb9bf2 Mon Sep 17 00:00:00 2001 From: Reginald Alfret Date: Wed, 23 Sep 2026 20:51:39 +0530 Subject: [PATCH] fix(python): preserve empty tool results in chat history (fixes #14359) Signed-off-by: Reginald Alfret --- python/semantic_kernel/contents/chat_history.py | 2 +- python/tests/unit/contents/test_chat_history.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/python/semantic_kernel/contents/chat_history.py b/python/semantic_kernel/contents/chat_history.py index a773c232c7bc..2be030a4c39e 100644 --- a/python/semantic_kernel/contents/chat_history.py +++ b/python/semantic_kernel/contents/chat_history.py @@ -219,7 +219,7 @@ def _prepare_for_add( """Prepare a message to be added to the history.""" kwargs["role"] = role - if role == AuthorRole.TOOL and content and not items: + if role == AuthorRole.TOOL and content is not None and not items: tool_call_id = kwargs.pop("tool_call_id", None) function_name = kwargs.pop("function_name", "unknown") function_result_content = FunctionResultContent( diff --git a/python/tests/unit/contents/test_chat_history.py b/python/tests/unit/contents/test_chat_history.py index 3c1b92945296..0de8b74ebad1 100644 --- a/python/tests/unit/contents/test_chat_history.py +++ b/python/tests/unit/contents/test_chat_history.py @@ -133,6 +133,20 @@ def test_add_tool_message_to_dict_succeeds(chat_history: ChatHistory): assert result["tool_call_id"] == "call_123" +def test_add_tool_message_empty_content_to_dict_succeeds(chat_history: ChatHistory): + chat_history.add_tool_message("", tool_call_id="call_123") + msg = chat_history.messages[-1] + assert msg.role == AuthorRole.TOOL + assert len(msg.items) == 1 + assert isinstance(msg.items[0], FunctionResultContent) + assert msg.items[0].result == "" + assert msg.items[0].id == "call_123" + result = msg.to_dict() + assert result["content"] == "" + assert result["role"] == AuthorRole.TOOL + assert result["tool_call_id"] == "call_123" + + def test_add_tool_message_list(chat_history: ChatHistory): content = [FunctionResultContent(id="test", result="Tool message")] chat_history.add_tool_message(content)