Skip to content

Commit 41579d2

Browse files
FOWEPJF255lhl-lhicursoragent
authored
Python: Concatenate code-interpreter stream deltas with prefix overlap (microsoft#8955) (microsoft#9142)
* fix(python): concatenate code-interpreter stream deltas with prefix overlap (microsoft#8955) Stop treating string-prefix relationships as a full resend. Only replace accumulated text when the incoming payload is a provider *.done / complete value; ordinary deltas always concatenate (also covers microsoft#8903). Co-authored-by: Cursor <[email protected]> * fix(python): address microsoft#8955 review -- empty list and done marker Handle empty accumulated outputs without IndexError, and mark the session-history full-code snapshot as stream_complete so it replaces deltas instead of concatenating. Co-authored-by: Cursor <[email protected]> * fix(python): use generic stream_complete for code-interpreter merge (microsoft#8955) Drop OpenAI *.done raw_representation checks from core. Providers mark authoritative snapshots with additional_properties stream_complete; OpenAI done events now set that flag. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: minelhi <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 967d2c6 commit 41579d2

5 files changed

Lines changed: 120 additions & 8 deletions

File tree

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,21 +2524,48 @@ def _content_items_text(items: Any) -> str | None:
25242524
return "".join(text_parts)
25252525

25262526

2527+
def _content_items_are_complete_value(items: Any) -> bool:
2528+
"""Return True when items are marked as an authoritative full value.
2529+
2530+
Providers that resend a complete snapshot after incremental deltas should set
2531+
``additional_properties["stream_complete"]`` (or ``"complete"``) on the text
2532+
content. Core only looks at that generic flag -- not provider event shapes --
2533+
so ordinary deltas always concatenate even when one text is a string prefix of
2534+
the other (see #8955 / #8903).
2535+
"""
2536+
if not isinstance(items, list):
2537+
return False
2538+
for item in cast(list[object], items):
2539+
if not isinstance(item, Content):
2540+
continue
2541+
props = item.additional_properties or {}
2542+
if props.get("stream_complete") or props.get("complete"):
2543+
return True
2544+
return False
2545+
2546+
25272547
def _merge_content_item_lists(existing: Any, incoming: Any) -> Any:
2528-
"""Merge streamed nested content lists, replacing deltas with a later full value when present."""
2548+
"""Merge streamed nested content lists.
2549+
2550+
Incremental text deltas are always concatenated. A later complete/full value
2551+
(``stream_complete`` / ``complete`` on ``additional_properties``) replaces the
2552+
accumulation.
2553+
"""
25292554
if incoming is None:
25302555
return existing
25312556
if existing is None:
25322557
return deepcopy(incoming)
25332558

2559+
if _content_items_are_complete_value(incoming):
2560+
return deepcopy(incoming)
2561+
2562+
# Empty accumulation (e.g. initial outputs=[]) keeps the first real payload.
2563+
if isinstance(existing, list) and len(cast(list[object], existing)) == 0:
2564+
return deepcopy(incoming)
2565+
25342566
existing_text = _content_items_text(existing)
25352567
incoming_text = _content_items_text(incoming)
25362568
if existing_text is not None and incoming_text is not None:
2537-
if incoming_text.startswith(existing_text):
2538-
return deepcopy(incoming)
2539-
if existing_text.startswith(incoming_text):
2540-
return existing
2541-
25422569
existing_items = cast(list[Content], existing)
25432570
merged = deepcopy(existing_items[0])
25442571
merged.text = existing_text + incoming_text

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,13 @@ async def test_after_run_stores_coalesced_code_interpreter_chunks(self) -> None:
673673
contents=[
674674
Content.from_code_interpreter_tool_call(
675675
call_id="ci_123",
676-
inputs=[Content.from_text(text="import pandas as pd")],
676+
# Final snapshot: authoritative full value via stream_complete.
677+
inputs=[
678+
Content.from_text(
679+
text="import pandas as pd",
680+
additional_properties={"stream_complete": True},
681+
)
682+
],
677683
additional_properties={"sequence_number": 3},
678684
)
679685
],

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,81 @@ def test_code_interpreter_tool_call_content_parses_inputs():
425425
assert call.inputs[0].text == "print('hi')"
426426

427427

428+
def test_code_interpreter_tool_call_stream_concatenates_prefix_deltas() -> None:
429+
"""Genuine deltas must concatenate even when one text is a prefix of the other (#8955)."""
430+
updates = [
431+
ChatResponseUpdate(
432+
contents=[Content.from_code_interpreter_tool_call(call_id="ci_1", inputs=[Content.from_text(text="(")])]
433+
),
434+
ChatResponseUpdate(
435+
contents=[Content.from_code_interpreter_tool_call(call_id="ci_1", inputs=[Content.from_text(text="()")])]
436+
),
437+
]
438+
resp = ChatResponse.from_updates(updates)
439+
joined = "".join(i.text or "" for i in resp.messages[0].contents[0].inputs or [])
440+
assert joined == "(()"
441+
442+
443+
def test_code_interpreter_tool_call_stream_concatenates_indent_prefix_deltas() -> None:
444+
deltas = [" ", " print(i)\n"]
445+
updates = [
446+
ChatResponseUpdate(
447+
contents=[Content.from_code_interpreter_tool_call(call_id="ci_1", inputs=[Content.from_text(text=d)])]
448+
)
449+
for d in deltas
450+
]
451+
resp = ChatResponse.from_updates(updates)
452+
joined = "".join(i.text or "" for i in resp.messages[0].contents[0].inputs or [])
453+
assert joined == " print(i)\n"
454+
455+
456+
def test_code_interpreter_tool_call_stream_done_replaces_accumulated_deltas() -> None:
457+
"""A stream_complete snapshot is authoritative and replaces prior delta text."""
458+
updates = [
459+
ChatResponseUpdate(
460+
contents=[
461+
Content.from_code_interpreter_tool_call(call_id="ci_1", inputs=[Content.from_text(text="import os\n")])
462+
]
463+
),
464+
ChatResponseUpdate(
465+
contents=[
466+
Content.from_code_interpreter_tool_call(
467+
call_id="ci_1",
468+
inputs=[
469+
Content.from_text(
470+
text="import os\nimport sys",
471+
additional_properties={"stream_complete": True},
472+
)
473+
],
474+
)
475+
]
476+
),
477+
]
478+
resp = ChatResponse.from_updates(updates)
479+
joined = "".join(i.text or "" for i in resp.messages[0].contents[0].inputs or [])
480+
assert joined == "import os\nimport sys"
481+
482+
483+
def test_code_interpreter_tool_result_empty_outputs_then_text() -> None:
484+
"""Initial empty outputs=[] must not IndexError when a text chunk arrives."""
485+
updates = [
486+
ChatResponseUpdate(
487+
contents=[Content.from_code_interpreter_tool_result(call_id="ci_1", outputs=[])]
488+
),
489+
ChatResponseUpdate(
490+
contents=[
491+
Content.from_code_interpreter_tool_result(
492+
call_id="ci_1",
493+
outputs=[Content.from_text(text="ok")],
494+
)
495+
]
496+
),
497+
]
498+
resp = ChatResponse.from_updates(updates)
499+
outputs = resp.messages[0].contents[0].outputs or []
500+
assert "".join(o.text or "" for o in outputs if o.type == "text") == "ok"
501+
502+
428503
def test_code_interpreter_tool_result_content_outputs():
429504
result = Content.from_code_interpreter_tool_result(
430505
call_id="call-2",

‎python/packages/openai/agent_framework_openai/_chat_client.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,8 @@ def parse_annotations(
38883888
"output_index": event.output_index,
38893889
"sequence_number": event.sequence_number,
38903890
"item_id": event.item_id,
3891+
# Authoritative full code; core merges replace prior deltas.
3892+
"stream_complete": True,
38913893
}
38923894
contents.append(
38933895
Content.from_code_interpreter_tool_call(

‎python/packages/openai/tests/openai/test_openai_chat_client.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8213,10 +8213,12 @@ def test_parse_chunk_from_openai_code_interpreter_done() -> None:
82138213
assert result.contents[0].inputs[0].type == "text"
82148214
assert result.contents[0].inputs[0].text is not None
82158215
assert "import pandas as pd" in result.contents[0].inputs[0].text
8216-
# Verify additional_properties for stream ordering
8216+
# Verify additional_properties for stream ordering and authoritative snapshot
82178217
assert result.contents[0].additional_properties["output_index"] == 0
82188218
assert result.contents[0].additional_properties["sequence_number"] == 5
82198219
assert result.contents[0].additional_properties["item_id"] == "ci_456"
8220+
assert result.contents[0].additional_properties["stream_complete"] is True
8221+
assert result.contents[0].inputs[0].additional_properties["stream_complete"] is True
82208222

82218223

82228224
def test_parse_chunk_from_openai_reasoning() -> None:

0 commit comments

Comments
 (0)