diff --git a/python/semantic_kernel/contents/streaming_content_mixin.py b/python/semantic_kernel/contents/streaming_content_mixin.py index 92cb1cae7c99..dc9be99636b5 100644 --- a/python/semantic_kernel/contents/streaming_content_mixin.py +++ b/python/semantic_kernel/contents/streaming_content_mixin.py @@ -43,6 +43,7 @@ def _merge_items_lists(self, other_items: list[Any]) -> list[Any]: new_item = item + other_item # type: ignore new_items_list[id] = new_item added = True + break except (ValueError, ContentAdditionException) as ex: logger.debug(f"Could not add item {other_item} to {item}.", exc_info=ex) continue diff --git a/python/tests/unit/contents/test_streaming_chat_message_content.py b/python/tests/unit/contents/test_streaming_chat_message_content.py index 9d9fc8c0f4aa..045af38a6619 100644 --- a/python/tests/unit/contents/test_streaming_chat_message_content.py +++ b/python/tests/unit/contents/test_streaming_chat_message_content.py @@ -290,6 +290,22 @@ def test_scmc_add_three(): assert len(combined.inner_content) == 3 +def test_scmc_add_merges_item_into_one_matching_item_only(): + message1 = StreamingChatMessageContent( + choice_index=0, + role=AuthorRole.ASSISTANT, + items=[ + StreamingTextContent(choice_index=0, text="Part one. "), + StreamingTextContent(choice_index=0, text="Part two. "), + ], + ) + message2 = StreamingChatMessageContent( + choice_index=0, role=AuthorRole.ASSISTANT, items=[StreamingTextContent(choice_index=0, text="tail")] + ) + combined = message1 + message2 + assert [item.text for item in combined.items] == ["Part one. tail", "Part two. "] + + @pytest.mark.parametrize( "message1, message2", [