Skip to content

Python: Merge a streaming item into only one matching item - #14787

Open
Muhammad Abdullah Butt (1AbdullahButt) wants to merge 1 commit into
microsoft:mainfrom
1AbdullahButt:fix/14786-merge-items-break
Open

Muhammad Abdullah Butt (1AbdullahButt) wants to merge 1 commit into
microsoft:mainfrom
1AbdullahButt:fix/14786-merge-items-break

Conversation

@1AbdullahButt

Copy link
Copy Markdown

Motivation and Context

Fixes #14786.

When two streaming chunks are added, StreamingContentMixin._merge_items_lists merged each incoming item into every compatible item in the list. For example, a first chunk with two text parts plus a second chunk with "tail" gave ['Part one. tail', 'Part two. tail'], so the text was duplicated.

Description

The inner loop set added = True after a successful item + other_item but kept iterating. It now breaks at the first successful merge, so each incoming item is merged into exactly one item, as the existing tests already assume for the single-item case.

Added test_scmc_add_merges_item_into_one_matching_item_only to tests/unit/contents/test_streaming_chat_message_content.py. It fails before the change and passes after.

Tested locally (Python 3.12, Windows):

  • the new test fails without the fix and passes with it
  • test_streaming_chat_message_content.py, test_function_call_content.py and test_streaming_annotation_content.py: 82 passed, 0 failed. I ran them with --noconftest, so the 9 tests that need conftest fixtures (function_call) were not run.
  • ruff check and ruff format --check on both files pass

Claude Code wrote this change.

Contribution Checklist

🤖 Generated with Claude Code

_merge_items_lists kept looping after a successful add, so an incoming
chunk item was added to every compatible item in the list and its text
or arguments were duplicated. Stop at the first successful merge.

Fixes microsoft#14786

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 12:20
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Nov 12, 2026

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 identified for the change to prevent duplicate text during streaming-content merging.

No open issues 🎉

What changed in this PR

Fixes #14786 by ensuring Python streaming-content merging adds each incoming item to only the first compatible item, preventing duplicated text across matching items.

Changes:

  • Stops the inner merge loop after the first successful addition.
  • Adds a regression test confirming that only the first of two compatible text items receives the incoming text.
File Description
python/​tests/​unit/​contents/​test_streaming_chat_message_content.py Adds regression coverage for merging into only one matching item.
python/​semantic_kernel/​contents/​streaming_content_mixin.py Breaks the loop after a successful merge to prevent duplicate additions.

Review effort: Balanced

This branch was successfully deployed

1 active deployment
github-app-auth — 4b5044aa Deployed Nov 12, 2026 by 1AbdullahButt via add_label #29674
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

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