Repository navigation
fix(responses): announce every reasoning run the bridge streams, not only the first - #45282
mateo-berri wants to merge 2 commits into
Conversation
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 8daf874. Configure here.
… open A chunk carrying both reasoning content and a finish_reason never counted as the end of the run, so the reasoning item was announced and listed by response.completed but never closed. The done path now closes any open run before the final events, and the test helper that checks announcements is a pure comprehension.
|
Closing this one to re-open the same branch as a Devin-authored PR, which can be self-approved; link coming in a moment |
|
Re-opened as #45300 (Devin-authored) so it can be self-approved; same branch and tip, the review history carries on there |
TLDR
Problem this solves:
responses.streamcrashed withIndexErroron that hole; SDK 3.x passed through deltas for an item it never saw added or closedresponse.completedlisted a reasoning item the stream never announced, or dropped the second runHow it solves it:
response.completedlists one reasoning item per run, signed thinking blocks split by the run that streamed themUser Flow
Before: a developer streaming
/v1/responsesthrough the gateway to a provider that thinks again after a tool call (Claude with web search and reasoning here) gets reasoning deltas for an item the stream never announces or closesclient.responses.stream(model="claude-sonnet-5-5", input=..., tools=[{"type": "web_search_preview"}], reasoning={"effort": "high"})with the OpenAI Python SDK, which sends POST https://litellm-domain/v1/responses with"stream": trueresponse.created, thenresponse.reasoning_summary_text.deltaevents atoutput_index1 for a reasoning itemrs_…, with noresponse.output_item.addedfor that item before themresponse.reasoning_summary_text.doneorresponse.output_item.doneever arrives for that item; the stream announces the message atoutput_index2 and streams its textresponse.completedlists a reasoning item the stream never announced or closed, so an app that renders items as they are added shows the answer with no reasoning (a function tool call followed by reasoning does the same and crashes the OpenAI SDK 2.x withIndexError: list index out of rangeat the message; reasoning, a tool call, then reasoning again streams the second run's delta into the first reasoning item after it was closed, andresponse.completedlists one reasoning item for two runs)After: the same request announces each reasoning item before its deltas, closes it, and completes with the items the stream announced
client.responses.stream(model="claude-sonnet-5-5", input=..., tools=[{"type": "web_search_preview"}], reasoning={"effort": "high"})with the OpenAI Python SDK, which sends POST https://litellm-domain/v1/responses with"stream": trueresponse.created, thenresponse.output_item.addedfor a reasoning item atoutput_index1, then itsresponse.reasoning_summary_text.deltaevents carrying that item's idresponse.reasoning_summary_text.doneandresponse.output_item.doneclose the reasoning item, then the stream announces the message atoutput_index2 and streams its textresponse.completedlists the web search call, the reasoning item, and the message under the ids and indexes the stream announced, so the app shows the reasoning and the answer (a function tool call followed by reasoning streams and completes the same way, on SDK 2.x too; reasoning, a tool call, then reasoning again gives two reasoning items with distinct ids, each announced and closed on its own)Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Setup shared by both legs. One proxy per leg, booted with
python litellm/proxy/proxy_cli.py --config config.yaml --port <random port> --num_workers 2(2 uvicorn workers, Before at the merge base on port 52187, After at the tip on port 44277, run one after the other on the same machine). The client is the OpenAI Python SDK 3.26.0 (pip install openaitoday) drivingclient.responses.streamagainsthttp://127.0.0.1:<port>/v1. The bridge iterator only serves/v1/responsesstreaming, so that is the one endpoint under test. The live provider case is Anthropicclaude-sonnet-5-5withweb_search_previewandreasoning.efforthigh, the only shape in which a real provider thinks again after a tool call inside one stream (costs real $$$). The three other cases go through the same gateway and client to a scripted chat-completions upstream that replays a fixed delta sequence (adeepseek/alias, so the request takes the bridge), because no live provider thinks again after a client-side function tool call inside one stream; they are the shapes the new unit tests cover, seen from the client.config.yaml:
Scripted upstream deltas, in order per model:
tool-then-reasoningsends aget_weathertool call, thenreasoning_content, then text;reasoning-tool-reasoningsendsreasoning_content, the tool call,reasoning_contentagain, then text;text-then-reasoningsends text, thenreasoning_content, then text.client_stream.py (
TOOLSis oneget_weatherfunction tool taking acitystring):Before (03ba79d)
Claude web search, then reasoning (live Anthropic)
python client_stream.py 52187 claude-sonnet-5-5 "Use web search once to find the current population of Iceland from a reliable source. After reading the result, reason step by step about how many years at 1% annual growth it would take to reach half a million, then answer in one sentence." '[{"type":"web_search_preview"}]' '{"reasoning":{"effort":"high"},"max_output_tokens":6000}'output_index1 forrs_6ca9d47c…with noresponse.output_item.addedbefore them and noreasoning_summary_text.doneoroutput_item.doneafter;response.completedlists that reasoning item anyway):Function tool call, then reasoning (scripted upstream)
python client_stream.py 52187 tool-then-reasoning "What is the weather in Reykjavik?" "$TOOLS" '{}'output_index1 is never announced or closed;response.completedlists the reasoning item the stream never announced):Reasoning, tool call, reasoning again (scripted upstream)
python client_stream.py 52187 reasoning-tool-reasoning "What is the weather in Reykjavik?" "$TOOLS" '{}'output_index0,rs_18080366…) after that item was already closed, andresponse.completedlists one reasoning item for two runs):Text, then reasoning (scripted upstream)
python client_stream.py 52187 text-then-reasoning "What is the weather in Reykjavik?" "$TOOLS" '{}'output_index1 is never announced or closed;response.completedlists it):After (8daf874)
Claude web search, then reasoning (live Anthropic)
python client_stream.py 44277 claude-sonnet-5-5 "Use web search once to find the current population of Iceland from a reliable source. After reading the result, reason step by step about how many years at 1% annual growth it would take to reach half a million, then answer in one sentence." '[{"type":"web_search_preview"}]' '{"reasoning":{"effort":"high"},"max_output_tokens":6000}'output_index1 before its 36 deltas, closed withreasoning_summary_text.doneandoutput_item.done, andresponse.completedlists the web search call, that reasoning item, and the message under the announced ids):Function tool call, then reasoning (scripted upstream)
python client_stream.py 44277 tool-then-reasoning "What is the weather in Reykjavik?" "$TOOLS" '{}'output_index1 before its delta and closed, andresponse.completedlists function call, reasoning, message under the announced ids):Reasoning, tool call, reasoning again (scripted upstream)
python client_stream.py 44277 reasoning-tool-reasoning "What is the weather in Reykjavik?" "$TOOLS" '{}'output_index0 and 2, each announced before its delta and closed on its own, both listed byresponse.completed):Text, then reasoning (scripted upstream)
python client_stream.py 44277 text-then-reasoning "What is the weather in Reykjavik?" "$TOOLS" '{}'output_index1 after the message, closed, and listed byresponse.completed):Observations from the run:
web_search_callitem announced after the message; pre-existing, left alonefunction_calldone event arrives after later items; pre-existing, untouchedType
🐛 Bug Fix
Caveats (if any)
Low
encrypted_content; whichever lands second rebases a few linesmainand pass here (IndexErrorin the SDK onmain)web_search_callitem after the message, which crashes the OpenAI SDK 2.x even with reasoning off; pre-existing, independent of this change, and tracked separatelyFinal Attestation
Note
Medium Risk
Changes event ordering and output shape for
/v1/responsesstreaming (reasoning item lifecycle andresponse.completedoutput), which can affect OpenAI SDK clients but is covered by new contract tests.Overview
Fixes the chat-completions → Responses API streaming bridge so each reasoning segment is a first-class output item instead of reusing a single global reasoning slot.
The iterator now tracks
_ReasoningRuninstances (ownitem_id,output_index, accumulated text, and signed thinking-block keys). Starting a new run queuesresponse.output_item.addedbefore anyreasoning_summary_text.delta; ending a run (when non-reasoning output appears) emits the matching done events on both sync and async paths.response.completedis rebuilt to emit one reasoning item per run, withencrypted_contentsplit across runs viaencode_thinking_blocks/decode_thinking_blocks.Adds integration and unit tests for reasoning after tool calls, multiple reasoning runs, and reasoning after visible text.
Reviewed by Cursor Bugbot for commit 8daf874. Bugbot is set up for automated code reviews on this repo. Configure here.