Repository navigation
Python: make streamed text coalescing linear - #9058
Closed
Suhas Aggarwal (suhasagg) wants to merge 2 commits into
Closed
Suhas Aggarwal (suhasagg) wants to merge 2 commits into
Suhas Aggarwal (suhasagg) wants to merge 2 commits into
Conversation
Suhas Aggarwal (suhasagg)
requested review from
SergeyMenshykh,
Tao Chen (TaoChenOSU),
Eduard van Valkenburg (eavanvalkenburg),
Giles Odigwe (giles17),
Jose Alvarez (jpalvarezl),
Evan Mattson (moonbox3),
Roger Barreto (rogerbarreto),
sophia-ramsey and
westey (westey-m)
as code owners
October 5, 2026 13:42
Suhas Aggarwal (suhasagg)
temporarily deployed
to
github-app-auth
October 5, 2026 13:42 — with
GitHub Actions
Inactive
Suhas Aggarwal (suhasagg)
temporarily deployed
to
github-app-auth
October 5, 2026 13:44 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Coalesced annotations now unexpectedly alias the first input chunk.
Review effort: Balanced
Findings: 1
What changed in this PR
Optimizes streamed text coalescing from quadratic to linear complexity.
Changes:
- Aggregates compatible text chunks in one pass.
- Preserves output-kind boundaries and merge semantics.
- Adds a regression test preventing repeated
Content.__add__calls.
| File | Description |
|---|---|
python/packages/core/agent_framework/_types.py |
Implements linear text coalescing. |
python/packages/core/tests/core/test_types.py |
Tests the optimized aggregation path. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Suhas Aggarwal (suhasagg)
temporarily deployed
to
github-app-auth
October 5, 2026 14:02 — with
GitHub Actions
Inactive
Member
|
Suhas Aggarwal (@suhasagg) Thanks for the contribution. This implements the same change as #8953, so we'll continue the work there and close this duplicate. |
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Motivation & Context
Streaming responses may contain a large number of consecutive text chunks.
_coalesce_text_content()previously merged those chunks incrementally usingContent.__add__.Each addition reconstructs the accumulated text and associated metadata. As the accumulated content grows, repeatedly merging it causes increasingly large copies and can result in quadratic behavior for long streaming responses.
This change makes ordinary text-stream coalescing linear by collecting compatible consecutive text chunks and aggregating them in a single pass.
The change is intentionally scoped to
textcontent.text_reasoningcontinues to use the existing merge behavior because it has additional semantic boundaries involving IDs, protected data, andAdditionItemMismatch.Description & Review Guide
What are the major changes?
textcontent into runs instead of repeatedly applyingContent.__add__."".join(...)._MODEL_OUTPUT_KIND_KEYboundaries so different logical output kinds are not incorrectly coalesced.additional_propertiesprecedence, where earlier chunks win for duplicate keys.raw_representationbehavior.text_reasoningcoalescing path unchanged.Content.__add__and verifies that coalescing 100 text chunks performs zero repeatedContent.__add__calls. The previous implementation performed 99.What is the impact of these changes?
What do you want reviewers to focus on?
textpath, particularly property precedence, annotations, raw representations, and_MODEL_OUTPUT_KIND_KEYboundaries.text_reasoningon the existing merge path is the appropriate scope for this fix.Content.__add__aggregation.Related Issue
Fixes #8908
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.Validation
Full core type test suite:
Static checks: