Skip to content

Commit 463ae93

Browse files
lhl-lhicursoragent
andcommitted
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]>
1 parent 772ad82 commit 463ae93

5 files changed

Lines changed: 16 additions & 19 deletions

File tree

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,12 +2351,13 @@ def _content_items_text(items: Any) -> str | None:
23512351

23522352

23532353
def _content_items_are_complete_value(items: Any) -> bool:
2354-
"""Return True when items carry an authoritative full value (e.g. a ``*.done`` stream event).
2354+
"""Return True when items are marked as an authoritative full value.
23552355
2356-
OpenAI Responses emits ``code_interpreter_call_code.done`` with the full code after
2357-
incremental ``.delta`` events. Only that kind of payload should replace prior text;
2358-
ordinary deltas must always concatenate even when one happens to be a string prefix
2359-
of the other (see #8955 / #8903).
2356+
Providers that resend a complete snapshot after incremental deltas should set
2357+
``additional_properties["stream_complete"]`` (or ``"complete"``) on the text
2358+
content. Core only looks at that generic flag -- not provider event shapes --
2359+
so ordinary deltas always concatenate even when one text is a string prefix of
2360+
the other (see #8955 / #8903).
23602361
"""
23612362
if not isinstance(items, list):
23622363
return False
@@ -2366,18 +2367,15 @@ def _content_items_are_complete_value(items: Any) -> bool:
23662367
props = item.additional_properties or {}
23672368
if props.get("stream_complete") or props.get("complete"):
23682369
return True
2369-
raw = item.raw_representation
2370-
raw_type = getattr(raw, "type", None) if raw is not None else None
2371-
if isinstance(raw_type, str) and raw_type.endswith(".done"):
2372-
return True
23732370
return False
23742371

23752372

23762373
def _merge_content_item_lists(existing: Any, incoming: Any) -> Any:
23772374
"""Merge streamed nested content lists.
23782375
23792376
Incremental text deltas are always concatenated. A later complete/full value
2380-
(provider ``*.done`` event or ``stream_complete`` flag) replaces the accumulation.
2377+
(``stream_complete`` / ``complete`` on ``additional_properties``) replaces the
2378+
accumulation.
23812379
"""
23822380
if incoming is None:
23832381
return existing

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,7 @@ 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-
# Final snapshot mimics OpenAI code_interpreter_call_code.done
677-
# (authoritative full value, not another incremental delta).
676+
# Final snapshot: authoritative full value via stream_complete.
678677
inputs=[
679678
Content.from_text(
680679
text="import pandas as pd",

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,7 @@ def test_code_interpreter_tool_call_stream_concatenates_indent_prefix_deltas() -
450450

451451

452452
def test_code_interpreter_tool_call_stream_done_replaces_accumulated_deltas() -> None:
453-
"""A provider *.done payload is authoritative and replaces prior delta text."""
454-
455-
class _DoneEvent:
456-
type = "response.code_interpreter_call_code.done"
457-
453+
"""A stream_complete snapshot is authoritative and replaces prior delta text."""
458454
updates = [
459455
ChatResponseUpdate(
460456
contents=[
@@ -468,7 +464,7 @@ class _DoneEvent:
468464
inputs=[
469465
Content.from_text(
470466
text="import os\nimport sys",
471-
raw_representation=_DoneEvent(),
467+
additional_properties={"stream_complete": True},
472468
)
473469
],
474470
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3863,6 +3863,8 @@ def parse_annotations(
38633863
"output_index": event.output_index,
38643864
"sequence_number": event.sequence_number,
38653865
"item_id": event.item_id,
3866+
# Authoritative full code; core merges replace prior deltas.
3867+
"stream_complete": True,
38663868
}
38673869
contents.append(
38683870
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
@@ -8205,10 +8205,12 @@ def test_parse_chunk_from_openai_code_interpreter_done() -> None:
82058205
assert result.contents[0].inputs[0].type == "text"
82068206
assert result.contents[0].inputs[0].text is not None
82078207
assert "import pandas as pd" in result.contents[0].inputs[0].text
8208-
# Verify additional_properties for stream ordering
8208+
# Verify additional_properties for stream ordering and authoritative snapshot
82098209
assert result.contents[0].additional_properties["output_index"] == 0
82108210
assert result.contents[0].additional_properties["sequence_number"] == 5
82118211
assert result.contents[0].additional_properties["item_id"] == "ci_456"
8212+
assert result.contents[0].additional_properties["stream_complete"] is True
8213+
assert result.contents[0].inputs[0].additional_properties["stream_complete"] is True
82128214

82138215

82148216
def test_parse_chunk_from_openai_reasoning() -> None:

0 commit comments

Comments
 (0)