Skip to content

fix(python): avoid duplicating chunks into multiple matching items in StreamingContentMixin (#14786) - #14844

Open
Parchuri Poojith Kumar (poojithparchuri-iiitk) wants to merge 1 commit into
microsoft:mainfrom
poojithparchuri-iiitk:fix-streaming-chunk-merge-duplication
Open

Parchuri Poojith Kumar (poojithparchuri-iiitk) wants to merge 1 commit into
microsoft:mainfrom
poojithparchuri-iiitk:fix-streaming-chunk-merge-duplication

Conversation

@poojithparchuri-iiitk

Summary

Fixes #14786

When merging streaming chunks in StreamingContentMixin._merge_items_lists, the inner loop that iterates through new_items_list was setting added = True after a successful item + other_item merge, but didn't break out of the loop.

As a result, if the message contained multiple items of the same type (for example, multiple text parts or parallel tool call chunks), the incoming chunk was getting added to every compatible item in the list instead of stopping after merging into the first match, causing duplicated text and parameters.

Changes

  • In python/semantic_kernel/contents/streaming_content_mixin.py:
    • Added a break immediately after added = True so once an incoming chunk is merged into its corresponding item, the inner loop terminates.
  • In python/tests/unit/contents/test_streaming_chat_message_content.py:
    • Added unit test test_scmc_merge_does_not_duplicate_to_multiple_matching_items verifying that an incoming chunk merges only into the first matching item when multiple items of the same type exist.

Testing

  • Ran pytest on test_streaming_chat_message_content.py locally — all 44 tests passed in under 1 second.

AI Disclosure: I used AI to help trace the loop logic and draft the regression test case; I reviewed the fix and verified all tests pass locally.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change preventing duplicate streaming-chunk merges.

No open issues 🎉

What changed in this PR

Fixes #14786 by preventing an incoming streaming chunk from being merged into multiple compatible items, avoiding duplicated text or tool-call arguments.

Changes:

  • Stops the inner merge loop after the first successful merge.
  • Adds a regression test confirming that only the first matching text item receives the incoming chunk.
File Description
python/​tests/​unit/​contents/​test_streaming_chat_message_content.py Adds regression coverage for merging into only the first matching item.
python/​semantic_kernel/​contents/​streaming_content_mixin.py Adds a break after a successful merge to prevent duplication.

Review effort: Balanced

This branch was successfully deployed

1 active deployment
github-app-auth — 47555e2c Deployed Nov 20, 2026 by poojithparchuri-iiitk via add_label #29770
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Bug: streaming chunk merge adds an item to every matching item, duplicating text

2 participants