Skip to content

Commit 9e79980

Browse files
committed
Python: fix(core): fold streamed text runs in one pass instead of repeated +=
_coalesce_text_content folded chunks with first_new_content += content, and every Content.__add__ rebuilds the text string, re-merges additional_properties, and re-concatenates raw_representation lists, so aggregating n chunks cost O(n^2) (#8908). Collect each mergeable run and fold it in one pass in _merge_content_run: one join per segment, a single props merge (the earliest chunk still wins per key), one flattened raw-representation list. The fold reproduces the old semantics exactly, including the run head's raw representation being dropped (the old path deep-copied the head, and deepcopy discards raw_representation via _SHALLOW_COPY_FIELDS) and the text_reasoning id / reasoning-text split boundaries. 16000 chunks: 0.302s -> 0.011s on this machine. Signed-off-by: Yufeng He <[email protected]>
1 parent cb77f68 commit 9e79980

2 files changed

Lines changed: 214 additions & 27 deletions

File tree

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 87 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,39 +2280,99 @@ def _merge_function_call_content(message: Message, content: Content) -> None:
22802280
message.contents.append(content)
22812281

22822282

2283+
def _merge_content_run(run: list[Content], type_str: Literal["text", "text_reasoning"]) -> Content:
2284+
"""Fold a collected run of same-type contents in one pass.
2285+
2286+
The result matches folding the run with repeated ``+=``: the earliest chunk
2287+
wins per additional-property key, annotations concatenate in order, and raw
2288+
representations flatten into one list (left untouched when a single chunk
2289+
carries the only value). The run head's own raw representation is dropped,
2290+
matching the old path whose initial ``deepcopy`` discards it
2291+
(``_SHALLOW_COPY_FIELDS``).
2292+
"""
2293+
merged_props: dict[str, Any] = {}
2294+
for content in reversed(run):
2295+
merged_props.update(content.additional_properties)
2296+
annotation_seqs = [c.annotations for c in run if c.annotations is not None]
2297+
annotations = [a for seq in annotation_seqs for a in seq] if annotation_seqs else None
2298+
non_null_raws = [c.raw_representation for c in run[1:] if c.raw_representation is not None]
2299+
raw_representation: Any
2300+
if not non_null_raws:
2301+
raw_representation = None
2302+
elif len(non_null_raws) == 1:
2303+
raw_representation = non_null_raws[0]
2304+
else:
2305+
raw_representation = []
2306+
for raw in non_null_raws:
2307+
raw_representation.extend(raw if isinstance(raw, list) else [raw])
2308+
if type_str == "text":
2309+
return Content(
2310+
"text",
2311+
text="".join(c.text or "" for c in run),
2312+
annotations=annotations,
2313+
additional_properties=merged_props,
2314+
raw_representation=raw_representation,
2315+
)
2316+
return Content(
2317+
"text_reasoning",
2318+
id=next((c.id for c in run if c.id), None),
2319+
text=None if all(c.text is None for c in run) else "".join(c.text or "" for c in run),
2320+
protected_data=next((c.protected_data for c in reversed(run) if c.protected_data is not None), None),
2321+
annotations=annotations,
2322+
additional_properties=merged_props,
2323+
raw_representation=raw_representation,
2324+
)
2325+
2326+
22832327
def _coalesce_text_content(contents: list[Content], type_str: Literal["text", "text_reasoning"]) -> None:
2284-
"""Take any subsequence Text or TextReasoningContent items and coalesce them into a single item."""
2328+
"""Take any subsequence Text or TextReasoningContent items and coalesce them into a single item.
2329+
2330+
Mergeable runs are collected and folded in a single pass, so aggregating n
2331+
chunks costs O(n) instead of the O(n^2) of repeated ``+=``.
2332+
"""
22852333
if not contents:
22862334
return
22872335
coalesced_contents: list[Content] = []
2288-
first_new_content: Any | None = None
2336+
run: list[Content] = []
2337+
run_id: str | None = None
2338+
run_has_text = False
2339+
run_has_reasoning_text = False
2340+
2341+
def flush() -> None:
2342+
nonlocal run_id, run_has_text, run_has_reasoning_text
2343+
if run:
2344+
coalesced_contents.append(deepcopy(run[0]) if len(run) == 1 else _merge_content_run(run, type_str))
2345+
run.clear()
2346+
run_id = None
2347+
run_has_text = run_has_reasoning_text = False
2348+
22892349
for content in contents:
2290-
if content.type == type_str:
2291-
if first_new_content is None:
2292-
first_new_content = deepcopy(content)
2293-
elif type_str == "text" and first_new_content.additional_properties.get(
2294-
_MODEL_OUTPUT_KIND_KEY
2295-
) != content.additional_properties.get(_MODEL_OUTPUT_KIND_KEY):
2296-
coalesced_contents.append(first_new_content)
2297-
first_new_content = deepcopy(content)
2298-
else:
2299-
try:
2300-
first_new_content += content
2301-
except AdditionItemMismatch:
2302-
# Different IDs means a new logical segment; flush the current one
2303-
coalesced_contents.append(first_new_content)
2304-
first_new_content = deepcopy(content)
2305-
else:
2306-
# skip this content, it is not of the right type
2307-
# so write the existing one to the list and start a new one,
2308-
# once the right type is found again
2309-
if first_new_content:
2310-
coalesced_contents.append(first_new_content)
2311-
first_new_content = None
2312-
# but keep the other content in the new list
2350+
if content.type != type_str:
2351+
flush()
23132352
coalesced_contents.append(content)
2314-
if first_new_content:
2315-
coalesced_contents.append(first_new_content)
2353+
continue
2354+
if run:
2355+
if type_str == "text":
2356+
# A folded run keeps the first chunk's value for this key, so the
2357+
# run head alone decides whether the next chunk joins or splits.
2358+
if run[0].additional_properties.get(_MODEL_OUTPUT_KIND_KEY) != content.additional_properties.get(
2359+
_MODEL_OUTPUT_KIND_KEY
2360+
):
2361+
flush()
2362+
else:
2363+
other_id: str | None = getattr(content, "id", None)
2364+
if (run_id and other_id and run_id != other_id) or (
2365+
run_has_text
2366+
and getattr(content, "text", None)
2367+
and (run_has_reasoning_text != ("reasoning_text" in content.additional_properties))
2368+
):
2369+
flush()
2370+
run.append(content)
2371+
if type_str == "text_reasoning":
2372+
run_id = run_id or getattr(content, "id", None)
2373+
run_has_text = run_has_text or bool(getattr(content, "text", None))
2374+
run_has_reasoning_text = run_has_reasoning_text or "reasoning_text" in content.additional_properties
2375+
flush()
23162376
contents.clear()
23172377
contents.extend(coalesced_contents)
23182378

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import warnings
88
from collections.abc import AsyncIterable, Awaitable, Callable, Sequence
9+
from copy import deepcopy
910
from dataclasses import dataclass
1011
from datetime import datetime, timezone
1112
from functools import partial
@@ -41,6 +42,7 @@
4142
GROUP_TOKEN_COUNT_KEY,
4243
)
4344
from agent_framework._types import (
45+
_MODEL_OUTPUT_KIND_KEY,
4446
_append_instructions,
4547
_get_data_bytes,
4648
_get_data_bytes_as_str,
@@ -2494,6 +2496,131 @@ def test_coalesce_text_reasoning_with_different_ids():
24942496
assert contents[1].text == "Thinking B1 B2"
24952497

24962498

2499+
def _reference_coalesce(contents: list[Content], type_str: Literal["text", "text_reasoning"]) -> None:
2500+
"""The pre-fix fold, kept as the equivalence oracle: deepcopy the run head, then += the rest."""
2501+
if not contents:
2502+
return
2503+
coalesced: list[Content] = []
2504+
acc: Content | None = None
2505+
for content in contents:
2506+
if content.type == type_str:
2507+
if acc is None:
2508+
acc = deepcopy(content)
2509+
elif type_str == "text" and acc.additional_properties.get(
2510+
_MODEL_OUTPUT_KIND_KEY
2511+
) != content.additional_properties.get(_MODEL_OUTPUT_KIND_KEY):
2512+
coalesced.append(acc)
2513+
acc = deepcopy(content)
2514+
else:
2515+
try:
2516+
acc += content
2517+
except AdditionItemMismatch:
2518+
coalesced.append(acc)
2519+
acc = deepcopy(content)
2520+
else:
2521+
if acc:
2522+
coalesced.append(acc)
2523+
acc = None
2524+
coalesced.append(content)
2525+
if acc:
2526+
coalesced.append(acc)
2527+
contents.clear()
2528+
contents.extend(coalesced)
2529+
2530+
2531+
@pytest.mark.parametrize("type_str", ["text", "text_reasoning"])
2532+
def test_coalesce_matches_repeated_add(type_str: Literal["text", "text_reasoning"]) -> None:
2533+
"""The one-pass fold must reproduce the repeated-+= result on a mixed stream."""
2534+
from agent_framework._types import _coalesce_text_content
2535+
2536+
def make(i: int, **kwargs: Any) -> Content:
2537+
if type_str == "text":
2538+
return Content.from_text(f"chunk{i} ", **kwargs)
2539+
return Content.from_text_reasoning(text=f"chunk{i} ", **kwargs)
2540+
2541+
streams = [
2542+
# plain run
2543+
[make(i) for i in range(5)],
2544+
# run with props, annotations and raw representations of mixed shapes
2545+
[
2546+
make(0, additional_properties={"a": 1}, raw_representation={"n": 0}),
2547+
make(1, additional_properties={"a": 2, "b": 1}, raw_representation=[{"n": 1}]),
2548+
make(2),
2549+
make(3, raw_representation={"n": 3}),
2550+
],
2551+
# split by an unrelated content type, then resume
2552+
[make(0), Content.from_data(b"\x00", media_type="application/octet-stream"), make(1), make(2)],
2553+
# single chunk stays a plain copy
2554+
[make(0, raw_representation={"n": 0})],
2555+
]
2556+
if type_str == "text":
2557+
marker = {_MODEL_OUTPUT_KIND_KEY: "refusal"}
2558+
streams.append([make(0), make(1, additional_properties=marker), make(2, additional_properties=marker), make(3)])
2559+
else:
2560+
streams.append([make(0, id="rs_a"), make(1, id="rs_a"), make(2, id="rs_b"), make(3)])
2561+
streams.append([
2562+
make(0, additional_properties={"reasoning_text": True}),
2563+
make(1),
2564+
make(2, additional_properties={"reasoning_text": True}),
2565+
])
2566+
2567+
for stream in streams:
2568+
expected = [deepcopy(c) for c in stream]
2569+
_reference_coalesce(expected, type_str)
2570+
actual = list(stream)
2571+
_coalesce_text_content(actual, type_str)
2572+
assert actual == expected, type_str
2573+
2574+
2575+
def test_coalesce_fold_does_not_readd_chunks() -> None:
2576+
"""Aggregating n chunks must not invoke Content.__add__ per chunk (the old O(n^2) path)."""
2577+
from agent_framework._types import _coalesce_text_content
2578+
2579+
calls = 0
2580+
original_add = Content.__add__
2581+
2582+
def counting_add(self: Content, other: Content) -> Content:
2583+
nonlocal calls
2584+
calls += 1
2585+
return original_add(self, other)
2586+
2587+
contents = [Content.from_text(f"c{i} ", raw_representation={"i": i}) for i in range(2000)]
2588+
try:
2589+
Content.__add__ = counting_add # type: ignore[method-assign]
2590+
_coalesce_text_content(contents, "text")
2591+
finally:
2592+
Content.__add__ = original_add # type: ignore[method-assign]
2593+
2594+
assert calls == 0
2595+
assert len(contents) == 1
2596+
assert contents[0].text == "".join(f"c{i} " for i in range(2000))
2597+
# The run head's raw representation is dropped by the fold's deepcopy semantics;
2598+
# the remaining 1999 entries flatten in order.
2599+
assert contents[0].raw_representation == [{"i": i} for i in range(1, 2000)]
2600+
2601+
2602+
def test_coalesce_text_reasoning_one_pass_semantics() -> None:
2603+
"""text_reasoning specifics: id from the first tagged chunk, protected_data from the last non-null."""
2604+
from agent_framework._types import _coalesce_text_content
2605+
2606+
contents = [
2607+
Content.from_text_reasoning(id="rs_a", text="t1", protected_data="sig1"),
2608+
Content.from_text_reasoning(text="t2"),
2609+
Content.from_text_reasoning(text=None, protected_data="sig2"),
2610+
]
2611+
_coalesce_text_content(contents, "text_reasoning")
2612+
assert len(contents) == 1
2613+
assert contents[0].id == "rs_a"
2614+
assert contents[0].text == "t1t2"
2615+
assert contents[0].protected_data == "sig2"
2616+
2617+
# a fully text-less run keeps text=None rather than ""
2618+
none_run = [Content.from_text_reasoning(text=None), Content.from_text_reasoning(text=None)]
2619+
_coalesce_text_content(none_run, "text_reasoning")
2620+
assert len(none_run) == 1
2621+
assert none_run[0].text is None
2622+
2623+
24972624
def test_agent_response_from_updates_preserves_refusal_marker() -> None:
24982625
marker = {"model_output_kind": "refusal"}
24992626
response = AgentResponse.from_updates([

0 commit comments

Comments
 (0)