Skip to content

Python: Preserve metadata when merging streaming text content - #14287

Open
Roli Bosch (roli-lpci) wants to merge 1 commit into
microsoft:mainfrom
roli-lpci:codex/fix-streaming-text-metadata-merge
Open

Python: Preserve metadata when merging streaming text content#14287
Roli Bosch (roli-lpci) wants to merge 1 commit into
microsoft:mainfrom
roli-lpci:codex/fix-streaming-text-metadata-merge

Conversation

@roli-lpci

Copy link
Copy Markdown
Contributor

Motivation and Context

StreamingTextContent.__add__ documents that metadata is combined, but it currently retains only the left operand's metadata. Streaming connectors can attach response metadata to different chunks—for example, an identifier on an early chunk and usage or log-probability data on a later one—so accumulating chunks can silently lose the later metadata.

No matching open issue or pull request was found for this narrow bug. The contribution guide permits skipping an issue for a trivial change.

Description

  • Merge both metadata dictionaries when adding StreamingTextContent, with the right operand winning on conflicting keys. This matches the existing behavior of sibling streaming content types.
  • Add regression tests for disjoint metadata, right-side conflict precedence, and preservation of both input objects.

Validation

  • Focused regression tests: 2 passed.
  • Full content unit suite: 396 passed.
  • Full repository pre-commit checks: passed.
  • Ruff lint and format checks: passed.
  • Python source distribution and wheel builds: passed.

Contribution Checklist

Copilot AI lite review requested due to automatic review settings August 14, 2026 06:50
@roli-lpci
Roli Bosch (roli-lpci) requested a review from a team as a code owner August 14, 2026 06:50

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.

Pull request overview

Fixes StreamingTextContent.__add__ to actually combine metadata from both operands (with the right operand winning on key conflicts), aligning behavior with other streaming content types and preventing metadata loss during chunk accumulation.

Changes:

  • Update StreamingTextContent.__add__ to merge metadata via dict union so later chunks’ metadata is preserved.
  • Add regression tests covering disjoint metadata merges, conflict precedence, and ensuring original operands’ metadata is unchanged.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/semantic_kernel/contents/streaming_text_content.py Merge left/right metadata in __add__ using dict union (right side wins).
python/tests/unit/contents/test_streaming_text_content.py Add unit tests validating metadata merge behavior and non-mutation of inputs.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: No findings
Scope: full PR (1 commit(s)): b84a2c6b3cec
Model: claude-opus-4.8

Overview

This PR changes a single production line in StreamingTextContent.__add__ from metadata=self.metadata to metadata=self.metadata | other.metadata, fulfilling the class docstring's rule that metadata is combined and bringing the type into line with its siblings StreamingChatMessageContent and FunctionCallContent. The change is safe: KernelContent.metadata is a non-optional dict with default_factory=dict, so dict | dict can never raise on a None operand, and the | operator returns a new dict without mutating either input—behavior the added regression tests pin explicitly. No public-API, serialization, provider-parity, or security concern survived reconciliation; right-operand-wins precedence is the documented, tested, and consistent contract.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.

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.

2 participants