Repository navigation
fix(chat-completions): merge a streamed turn's message into its pending tool-call message - #4728
Merged
seratch merged 1 commit intoAug 28, 2026
Conversation
…ll message A provider that streams tool-call deltas before the same turn's text yields response outputs ordered [function_call, message]. Replaying that history through Converter.items_to_messages flushed the tool_calls message and opened a second assistant message for the text. Chat Completions rejects an assistant message with tool_calls that is not directly followed by its tool messages, so the next turn failed with 400 and the session was unrecoverable. The same turn converted correctly when not streamed. Merge the message into the pending assistant message instead. A function_call_output always flushes, so tool calls from a previous turn cannot still be pending when a message item arrives: the merge only triggers on the same-turn streamed ordering. Streamed and non-streamed histories now convert identically, and histories already persisted in sessions are healed on replay. Four regression tests; all fail on main.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A provider that streams tool-call deltas before the same turn's text yields response outputs ordered
[function_call, message]. Replaying that history throughConverter.items_to_messagesflushed the pending tool_calls message when the message item arrived and opened a second assistant message for the text. Chat Completions rejects an assistant message withtool_callsthat is not directly followed by its tool messages, so the next turn failed with 400 and the session was unrecoverable. The identical turn converted correctly when not streamed.This PR merges the message into the pending assistant message instead. The merge only triggers on the same-turn streamed ordering: a
function_call_outputalways flushes, so tool calls from a previous turn cannot still be pending when a message item arrives. Streamed and non-streamed histories now convert identically, and histories already persisted in sessions are healed on replay. When legacy thinking blocks were already reconstructed into content parts, the text is appended as a content part rather than discarded.Fixes #4727.
Test plan
tests/models/test_openai_chatcompletions_converter.py: the merged turn, streamed/non-streamed convergence, no merging across turn boundaries, and refusal merging. All four fail onmain..agents/skills/code-change-verification/scripts/run.sh): format, lint, typecheck pass; tests 9217 passed, 28 skipped, with one failure —tests/sandbox/test_run_cwd.py::test_python_skill_uses_absolute_root_from_nested_workdir, which fails identically on a cleanmaincheckout in this environment (local sandbox setup, unrelated to this change).Issue number
Closes #4727
Checks
.agents/skills/code-change-verification/scripts/run.sh/reviewbefore submitting this PR