Skip to content

Python: Concatenate code-interpreter stream deltas with prefix overlap (#8955) - #9142

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 3 commits into
microsoft:mainfrom
FOWEPJF255:fix/8955-code-interpreter-prefix-delta-merge
Oct 12, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 3 commits into
microsoft:mainfrom
FOWEPJF255:fix/8955-code-interpreter-prefix-delta-merge

Conversation

@FOWEPJF255

Copy link
Copy Markdown
Contributor

Summary

  • Fixes #8955: streamed code_interpreter input/output deltas were silently dropped when one chunk's text was a string prefix of another (e.g. "(" then "()", or indent whitespace then the indented line).
  • Root cause: _merge_content_item_lists treated any prefix relationship as an OpenAI-style full resend and discarded one side.
  • Fix: always concatenate ordinary deltas; only replace accumulated text when the incoming payload is an authoritative complete value (*.done raw_representation.type, or stream_complete / complete on additional_properties).
  • Same heuristic also caused #8903; this change covers that path as well.

Test plan

  • python -m pytest tests/core/test_types.py -k "code_interpreter_tool_call_stream or code_interpreter_tool_call_content or code_interpreter_tool_result_content" -q
  • CI

Closes #8955

…verlap (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 <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Empty lists can raise IndexError, and an existing session-history test now fails.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Fixes corruption when aggregating prefix-overlapping code-interpreter stream deltas.

Changes:

  • Concatenates ordinary deltas unconditionally.
  • Replaces accumulated text for explicitly completed payloads.
  • Adds regression tests for prefix overlap and done events.
File Description
python/​packages/​core/​agent_framework/​_types.py Updates code-interpreter stream merging.
python/​packages/​core/​tests/​core/​test_types.py Adds aggregation regression tests.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread python/packages/core/agent_framework/_types.py
Comment thread python/packages/core/agent_framework/_types.py
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 <cursoragent@cursor.com>
@FOWEPJF255

Copy link
Copy Markdown
Contributor Author

Thanks Copilot -- addressed both findings in 772ad82f:

  1. Empty accumulation -- if existing is an empty list (e.g. initial outputs=[]), return a copy of incoming before indexing [0]. Added test_code_interpreter_tool_result_empty_outputs_then_text.
  2. Session-history fixture -- marked the final full-code snapshot with stream_complete=True so it replaces deltas instead of concatenating. test_after_run_stores_coalesced_code_interpreter_chunks passes again.

Comment thread python/packages/core/agent_framework/_types.py
…icrosoft#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 <cursoragent@cursor.com>
@FOWEPJF255

Copy link
Copy Markdown
Contributor Author

Thanks Eduard van Valkenburg (@eavanvalkenburg) -- agreed. In 463ae934:

  • Core _content_items_are_complete_value only looks at the generic additional_properties["stream_complete"] / "complete" flags (no raw_representation / *.done inspection).
  • OpenAI's code_interpreter_call_code.done path now sets stream_complete=True on the emitted content so the replace-vs-concat behavior stays correct at the provider boundary.

Merged via the queue into microsoft:main with commit 41579d2 Oct 12, 2026
49 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — 463ae934 Deployed Oct 8, 2026 by FOWEPJF255 via add_label #24820
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Streamed code interpreter deltas get silently dropped when one chunk's text happens to start with another's

4 participants