From b84a2c6b3cec3fd7bcfbd8aaa5fd1cdbe5c673c7 Mon Sep 17 00:00:00 2001 From: Rolando Bosch Date: Thu, 13 Aug 2026 12:20:39 -0400 Subject: [PATCH] fix: merge streaming text metadata --- .../contents/streaming_text_content.py | 2 +- .../contents/test_streaming_text_content.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 python/tests/unit/contents/test_streaming_text_content.py diff --git a/python/semantic_kernel/contents/streaming_text_content.py b/python/semantic_kernel/contents/streaming_text_content.py index 66440be6ae6b..ab58ca140b37 100644 --- a/python/semantic_kernel/contents/streaming_text_content.py +++ b/python/semantic_kernel/contents/streaming_text_content.py @@ -49,7 +49,7 @@ def __add__(self, other: TextContent) -> "StreamingTextContent": choice_index=self.choice_index, inner_content=self._merge_inner_contents(other.inner_content), ai_model_id=self.ai_model_id, - metadata=self.metadata, + metadata=self.metadata | other.metadata, text=(self.text or "") + (other.text or ""), encoding=self.encoding, ) diff --git a/python/tests/unit/contents/test_streaming_text_content.py b/python/tests/unit/contents/test_streaming_text_content.py new file mode 100644 index 000000000000..e766a8d0c2ce --- /dev/null +++ b/python/tests/unit/contents/test_streaming_text_content.py @@ -0,0 +1,26 @@ +# Copyright (c) Microsoft. All rights reserved. + +from semantic_kernel.contents.streaming_text_content import StreamingTextContent + + +def test_stc_add_combines_metadata(): + chunk1 = StreamingTextContent(choice_index=0, text="Hello, ", metadata={"id": "cmpl-1"}) + chunk2 = StreamingTextContent(choice_index=0, text="world!", metadata={"usage": {"prompt_tokens": 5}}) + + combined = chunk1 + chunk2 + + assert combined.text == "Hello, world!" + assert combined.metadata == {"id": "cmpl-1", "usage": {"prompt_tokens": 5}} + + # Make sure the original metadata is preserved + assert chunk1.metadata == {"id": "cmpl-1"} + assert chunk2.metadata == {"usage": {"prompt_tokens": 5}} + + +def test_stc_add_metadata_conflicting_keys_other_wins(): + chunk1 = StreamingTextContent(choice_index=0, text="Hello, ", metadata={"id": "cmpl-1", "logprobs": None}) + chunk2 = StreamingTextContent(choice_index=0, text="world!", metadata={"logprobs": {"tokens": ["world!"]}}) + + combined = chunk1 + chunk2 + + assert combined.metadata == {"id": "cmpl-1", "logprobs": {"tokens": ["world!"]}}