Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
26 changes: 26 additions & 0 deletions python/tests/unit/contents/test_streaming_text_content.py
Original file line number Diff line number Diff line change
@@ -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!"]}}
Loading