Skip to content

Commit 772ad82

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

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,6 +2387,10 @@ def _merge_content_item_lists(existing: Any, incoming: Any) -> Any:
23872387
if _content_items_are_complete_value(incoming):
23882388
return deepcopy(incoming)
23892389

2390+
# Empty accumulation (e.g. initial outputs=[]) keeps the first real payload.
2391+
if isinstance(existing, list) and len(cast(list[object], existing)) == 0:
2392+
return deepcopy(incoming)
2393+
23902394
existing_text = _content_items_text(existing)
23912395
incoming_text = _content_items_text(incoming)
23922396
if existing_text is not None and incoming_text is not None:

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,14 @@ 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 mimics OpenAI code_interpreter_call_code.done
677+
# (authoritative full value, not another incremental delta).
678+
inputs=[
679+
Content.from_text(
680+
text="import pandas as pd",
681+
additional_properties={"stream_complete": True},
682+
)
683+
],
677684
additional_properties={"sequence_number": 3},
678685
)
679686
],

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,26 @@ class _DoneEvent:
480480
assert joined == "import os\nimport sys"
481481

482482

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+
483503
def test_code_interpreter_tool_result_content_outputs():
484504
result = Content.from_code_interpreter_tool_result(
485505
call_id="call-2",

0 commit comments

Comments
 (0)