From e7c29b27987c7c2774ce866b1d01a9c0135c261c Mon Sep 17 00:00:00 2001 From: Vidit Patankar Date: Sun, 31 May 2026 14:39:03 +0530 Subject: [PATCH] Python: preserve image/nested content in FunctionResultContent from_function_call_content_and_result selected res from the first item of a ChatMessageContent (text / image data_uri / nested result) but then unconditionally ran res = str(result), discarding all three branches. Image and nested-FunctionResultContent payloads were silently lost (empty string). Move res = str(result) into an else branch so it is only the fallback for unrecognized item types. Adds a regression test. --- .../contents/function_result_content.py | 3 ++- .../contents/test_function_result_content.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/python/semantic_kernel/contents/function_result_content.py b/python/semantic_kernel/contents/function_result_content.py index b881371a398f..41d4a53ed910 100644 --- a/python/semantic_kernel/contents/function_result_content.py +++ b/python/semantic_kernel/contents/function_result_content.py @@ -144,7 +144,8 @@ def from_function_call_content_and_result( res = result.items[0].data_uri elif isinstance(result.items[0], FunctionResultContent): res = result.items[0].result - res = str(result) + else: + res = str(result) else: res = result return cls( diff --git a/python/tests/unit/contents/test_function_result_content.py b/python/tests/unit/contents/test_function_result_content.py index 2e83dc9737a9..850bcc51c5c2 100644 --- a/python/tests/unit/contents/test_function_result_content.py +++ b/python/tests/unit/contents/test_function_result_content.py @@ -116,6 +116,25 @@ def test_from_fcc_and_result(result: any): assert frc.metadata == {"test": "test", "test2": "test2"} +def test_from_fcc_and_result_preserves_image_and_nested_content(): + # When the result is a ChatMessageContent whose first item is an + # ImageContent or a nested FunctionResultContent, that item's data must be + # surfaced - not overwritten by str(result). + fcc = FunctionCallContent(id="test", name="test-function", arguments="{}") + + img = ImageContent(data=b"\x89PNG\r\n", mime_type="image/png", data_format="base64") + frc_img = FunctionResultContent.from_function_call_content_and_result( + fcc, ChatMessageContent(role="tool", items=[img]) + ) + assert frc_img.result == img.data_uri + + nested = FunctionResultContent(id="n", name="p-f", result={"k": "v"}) + frc_nested = FunctionResultContent.from_function_call_content_and_result( + fcc, ChatMessageContent(role="tool", items=[nested]) + ) + assert frc_nested.result == {"k": "v"} + + def test_to_cmc(): frc = FunctionResultContent(id="test", name="test-function", result="test-result") cmc = frc.to_chat_message_content()