Repository navigation
fix(websearch_interception): drop stream_options when forcing stream=False and replay it on the final stream - #45433
Conversation
…False and replay it on the final stream
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Merging this PR will not alter performance
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…he replay test through mock_response
…nd record follow-ups immutably
…the web search keys included
…r code interpreter and headroom, and keep adapters from replaying nested calls
…ck transport and real Azure config
|
bugbot run |
…es, messages and the SDK
… burst on failure
|
bugbot run |
…e replacement worker before teardown
|
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 a4f376d. Configure here.
TLDR
Problem this solves:
include_usagestays on the forced non-streaming call/v1/responsesstreams with a clientstream_optionshit it toolitellm.responses()streams in the Python SDK hit it tooHow it solves it:
stream_optionsoff the forcedstream=Falsecall/v1/messagesadapters stop passing the replay flag inwardUser Flow
Before: a developer streaming chat completions with web search on Azure gets an error instead of an answer
"model": "azure-gpt-4o","stream": true, and alitellm_web_searchtoolThe 'stream_options' parameter is only allowed when 'stream' is enabled., and code interpreter and headroom streams get the sameazure-gpt-5.4-minithey get HTTP 500'ModelResponse' object has no attribute '__aiter__'instead"stream": falseanswers 200, so they have to turn streaming offAfter: the same streamed request answers with the searched result and the usage the client asked for
"model": "azure-gpt-4o","stream": true, and alitellm_web_searchtoolfinish_reason: stopchunk, and so do code interpreter and headroom streamsazure-gpt-5.4-ministreams the same way, and withinclude_usagethe last chunk carries real usage and cost"stream": falsekeeps answering 200Linear ticket
Resolves LIT-9339
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)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Last updated: a4f376d. The proxy cells and /live-pr-risk ran on 7f87030, the last commit with a product diff, the SDK cells ran on eac5e07, and the audit below ran its tests on top of 7f87030. eac5e07, 4ae9f5e, 0dffa7a, e4f147c, and a4f376d only add or change tests
Shared setup: each leg is a proxy booted from its own git worktree and venv with
--num_workers 2and its own Postgres database (python litellm/proxy/proxy_cli.py --config config.yaml --port <port> --num_workers 2 --use_prisma_db_push). Azure (two deployments on one Foundry resource), Gemini, and Fireworks sit behind a forwarding recorder per leg that logs each outbound request and passes it on to the real provider, and Tavily is the search tool. The clients are the OpenAI Python SDK 2.33.0 (/v1/chat/completions,/v1/responses), curl 8.7.1, and the Anthropic Python SDK 0.84.0 (/v1/messages), pointed at the proxy throughqa_client.py <mode>(MODELpicks the deployment,INCLUDE_USAGE=1addsstream_options; source and request bodies below). The sync SDK cells calllitellm.responses()in-process from each checkout (PYTHONPATH=<checkout>, one shared venv) against the same Azure gpt-4o deployment, with Perplexity as the SDK's default search provider. Every call hits Azure, Gemini, Fireworks, Tavily, and Perplexity for real, and both legs ran the same cells in the same orderazure-gpt-4o-direct,azure-brokenwith its fallback,gemini-flash, andlit9339_probe.probe(a logger that records the keys each hook sees) serve the /live-pr-risk cells further downControl, the same body straight at Azure with no proxy:
stream=falseplusstream_optionsanswers 400The 'stream_options' parameter is only allowed when 'stream' is enabled.,stream=falsealone andstream=trueplusstream_optionsboth answer 200Outbound requests the recorders saw: at the base every forced
stream=falsecall carriedstream_options, and Azure and Fireworks answered it 400. At the head nostream=falsecall carriedstream_options, and no internal control key reached a provider. The sync SDK cells went through the same kind of recorder in front of Azure: the base's forcedstream=falseweb search and code interpreter calls carriedstream_optionsand got 400, the head's did not and got 200, and the two control cells sent the same key sets and request counts on both sidesBefore (3571b5d)
Streamed chat completion with the web search tool, azure-gpt-4o
MODEL=azure-gpt-4o qa_client.py chat-stream-websearchstream_options.include_usage:INCLUDE_USAGE=1 MODEL=azure-gpt-4o qa_client.py chat-stream-websearchcurl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @curl_body.jsonStreamed chat completion with the web search tool, azure-gpt-5.4-mini (Responses bridge)
INCLUDE_USAGE=1 MODEL=azure-gpt-5.4-mini qa_client.py chat-stream-websearchNon-streamed chat completion with the web search tool, azure-gpt-4o
MODEL=azure-gpt-4o qa_client.py chat-nostream-websearchStreamed /v1/responses with a web_search tool, azure-gpt-4o
MODEL=azure-gpt-4o qa_client.py responses-stream-websearchStreamed /v1/responses with a web_search tool and a client stream_options, azure-gpt-4o
curl -sS -o c44.out -w '%{http_code}\n' $PROXY/v1/responses -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @responses_stream_ws_stream_options.jsoncat c44.outSync SDK
litellm.responses()streamed with a web_search tool and a client stream_options, azure/gpt-4oPYTHONPATH=<checkout> python sync_responses_web_search.pypython sync_responses_cells.py code_interpreter_stream_optspython sync_responses_cells.py web_search_no_streamstream_optionsand no tool:python sync_responses_cells.py plain_stream_optsStreamed /v1/messages with the Anthropic web_search tool, azure-gpt-4o (Anthropic Python SDK)
MODEL=azure-gpt-4o qa_client.py messages-stream-websearchStreamed chat completion with the code interpreter tool, azure-gpt-4o
curl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @code_interp_stream_usage.jsonStreamed chat completion with the headroom_retrieve tool, azure-gpt-4o
curl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @headroom_stream_usage.jsonStreamed chat completion with the web search tool, Fireworks gpt-oss-120b
curl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @fireworks_chat_stream_ws_usage.jsonAfter (7f87030)
Streamed chat completion with the web search tool, azure-gpt-4o
MODEL=azure-gpt-4o qa_client.py chat-stream-websearchstream_options.include_usage:INCLUDE_USAGE=1 MODEL=azure-gpt-4o qa_client.py chat-stream-websearchcurl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @curl_body.jsonStreamed chat completion with the web search tool, azure-gpt-5.4-mini (Responses bridge)
INCLUDE_USAGE=1 MODEL=azure-gpt-5.4-mini qa_client.py chat-stream-websearchNon-streamed chat completion with the web search tool, azure-gpt-4o
MODEL=azure-gpt-4o qa_client.py chat-nostream-websearchStreamed /v1/responses with a web_search tool, azure-gpt-4o
MODEL=azure-gpt-4o qa_client.py responses-stream-websearchStreamed /v1/responses with a web_search tool and a client stream_options, azure-gpt-4o
curl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/responses -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @responses_stream_ws_stream_options.json > c44.out; grep status= c44.outgrep -o '^data: {"type":"[a-z_.]*"' c44.out | sort | uniq -cgrep '"response.output_text.done"' c44.outgrep -c '"obfuscation"' c44.outSync SDK
litellm.responses()streamed with a web_search tool and a client stream_options, azure/gpt-4oPYTHONPATH=<checkout> python sync_responses_web_search.pypython sync_responses_cells.py code_interpreter_stream_optspython sync_responses_cells.py web_search_no_streamstream_optionsand no tool:python sync_responses_cells.py plain_stream_optsStreamed /v1/messages with the Anthropic web_search tool, azure-gpt-4o (Anthropic Python SDK)
MODEL=azure-gpt-4o qa_client.py messages-stream-websearchStreamed chat completion with the code interpreter tool, azure-gpt-4o
curl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @code_interp_stream_usage.jsonStreamed chat completion with the headroom_retrieve tool, azure-gpt-4o
curl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @headroom_stream_usage.jsonStreamed chat completion with the web search tool, Fireworks gpt-oss-120b
curl -sSN -w '\nstatus=%{http_code} total=%{time_total}s\n' $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" -H 'content-type: application/json' -d @fireworks_chat_stream_ws_usage.jsonMerge-ref check: 4ae9f5e merged into main at 2fcc780 (d81e5b74de, no conflicts) ran every proxy cell again and matched the 7f87030 head on status, body shape, finish reason, and usage, apart from the Gemini cell noted below. The unit suites the merge touched (
test_llm_http_handler.py,test_litellm_pre_call_utils.py,test_utils.py,test_litellm_params.py, and the web search and code interpreter interception tests) pass on the merged tree. An earlier merge of 9f2ae08 into 43f07d0 also matched on the recorded outbound requestsqa_client.py, the SDK scripts, and the request bodies
curl_body.json
code_interp_stream_usage.json
headroom_stream_usage.json
fireworks_chat_stream_ws_usage.json
responses_stream_ws_stream_options.json
sync_responses_web_search.py
sync_responses_cells.py
Observed next to the fix, the same on both legs:
/v1/messagesweb search on Azure answers the raw search dump with zero usage, not a synthesized answer/v1/messageswith web search plus a second tool gets a JSON body instead of SSE on Azure and Gemini, and a 500'StreamingChoices' object has no attribute 'message'on Fireworkslitellm_web_searchtool call, a tool the client sent itself (twice in three head and merge-ref runs; the base answers 500 for that body)tests/integration/streaming/test_stream_contracts.py::test_proxy_stream_usage_visibility_keeps_exact_persisted_chargefails at the base too/live-pr-risk report (7f87030)
Verdict: no regression found. Every difference between base and head is a base 400 or 500 that answers 200 at the head. 7f87030 got its own graph walk, below
Breaking
None. Across 44 proxy cells on Azure, Gemini, and Fireworks plus 4 sync SDK cells, the cells that differ are base failures that pass at the head (web search, code interpreter, and headroom streams,
/v1/responseswith a clientstream_options, malformed and duplicatedstream_options, client-sent stash keys). Every other cell matches on status, body shape, recorded outbound requests, and attempt countBackward incompatible
None observed. The proxy now strips the web search control keys and the new
*_stream_optionskeys from client bodies, like the agentic-loop keys it already stripped. At the base a client-sent_headroom_interception_stream_optionsreached Azure and answered 400; at the head it is dropped and the call answers 200Regression risk
stream_optionsmoves to an internal key (checked in-process, no cache on the rig)include_usagefrom the logging object (traced)num_retriesand the context-window fallback reuse the post-hook kwargs, so the replay flag and the storedstream_optionscarry over (traced)stream_optionsinoptional_paramsfor the forced non-streaming call (traced)litellm.responses(SDK only) used to merge the hook output over the original params, so a key a hook removed came back. 7f87030 keeps only the keys the hook returned, in both the request params andlitellm_params, which is what the async path already did. Every deployment hook in the repo either returns the full kwargs orNone, so only the hooks that remove a key behave differently: web search, code interpreter, and headroom, the three this PR fixes (traced; the web search and code interpreter cases verified live). A custom hook that returns only the keys it changed loses the rest on this path now, which the shared hook chain and the async path already didDependency graph
/v1/chat/completionsweb search on Azure, streamed and not, with and without usage: verified live/v1/responsesweb search, streamed and not: verified live/v1/responsesweb search stream with a clientstream_options: verified live, base 400, head 200 with noobfuscationfields/v1/messagesweb search on the chat adapter (Azure, Gemini, Fireworks; one and two tools): verified live, same as basestream_options, no auth: verified livelitellm.responses()web search and code interpreter streams withstream_options: verified live, base 400, head 200litellm.responses()web search without streaming and a plain stream withstream_options: verified live, same as basetests/integrationproviders 17 of 18 pass (the one failure also fails at base), management 20 pass and 7 skip, guardrail timeouts 45 passNot verified
stream_optionswhenstream=false(hosted_vllm, groq, xai, openrouter, together_ai and more); Fireworks stands in for themAudit round 1 of 3
Verdict PASS. Every inventory row below has a cell that ran at the head with the expected outcome, and nothing inside the proxy was mocked or patched
Merge base 3571b5d, product tip 7f87030, tests commit a4f376d. Every row ran at e4f147c except R20, R20b, and R30, whose cells a4f376d rewrote or added, so those three ran again on both legs at a4f376d. Neither commit has a product diff after 7f87030
Each leg is a two-worker proxy on the GCE box with its own Postgres database and Redis. Azure and Tavily are a scripted upstream (
tests/integration/_support/interception_vendor.py) that answers 400 tostream_optionson a non-streaming call and to any_-prefixed key, the way Azure does. Each cell checks what the client got, what the upstream received, and the spend row, matched by the response id. Thebaseandbase-sdklanes ran on the merge base.head-run1,head-run1-sdk,head-run2, andhead-run2-sdkran on the head with seed 9339 and order seed 9339, with no skips and no retries. At e4f147c run 1 and run 2 each collected the same 35 cells and failed the same two, R20's old error assertion and R30's teardown, which a4f376d rewrote. At a4f376d run 1 and run 2 collected and passed the same 3 changed cells. Thectllane ran the six controls once on the head at e4f147cEvery red base cell failed on Azure's
stream_optionsrefusal, except R17 (the forged_websearch_interception_stream_optionskey was refused) and R12 (the missing deployment's 404, with the fallback refused forstream_options). R16 and R26 pass on both legs, so a malformed or emptystream_optionson/v1/responsesdid not reach Azure before this PR either. R10 shows that a streamed/v1/messagesrequest whose only tool is web search still answers from the search itself and never calls the model. R13 and R14 cover the cache case that the /live-pr-risk section lists as not verified. R20 and R20b show what a failed follow-up model call does. The first call's search tool call comes back with 200 and finish_reasontool_calls, on the JSON request at both legs (R20b) and now on the stream too (R20), with the first call's usage chunk because the caller asked for it. The Caveats section says why that stays as is. L1 and L2 are the live rows from the A/B above, on real Azure gpt-4o at 3571b5d and 7f87030Node ids
Type
🐛 Bug Fix
Caveats (if any)
Low
stream_optionsis kept on its own internal keyinclude_usageonly, notinclude_obfuscation/v1/responses: base 400, head 200 withoutobfuscationfields/v1/messagesweb search plus a second tool still fails/v1/messagesweb search on Azure returns the raw search dump, unchangedFinal Attestation
The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
9f2ae08 passes /live-pr-risk
7f87030 passes /live-pr-risk
Note
Medium Risk
Touches request shaping and streaming replay across web search, code interpreter, headroom, proxy stripping, and sync Responses merging; behavior change is intentional but broad across hot paths.
Overview
Fixes Azure (and similar) 400 errors when web search, code interpreter, or headroom interceptors turn a client stream into an internal
stream=Falseagentic call but still forwardedstream_options.Shared helpers (
as_converted_stream,stashed_stream_options,CONVERTED_STREAM_KEYS) now stripstream_optionsfrom upstream requests, stash the client value on per-interceptor internal keys, and the chat agentic loop replays the final answer as a fake stream with an usage chunk wheninclude_usagewas requested. Anthropic/v1/messagesbridges stop forwarding converted-stream flags into nested completions/responses; the sync Responses HTTP handler only merges hook kwargs that the hook actually returned (so removed keys do not reappear). The proxy strips expanded agentic-loop control fields viaAGENTIC_LOOP_KWARG_NAMES.Large integration coverage asserts upstream bodies never leak
stream_optionsor_-prefixed interception keys.Reviewed by Cursor Bugbot for commit a4f376d. Bugbot is set up for automated code reviews on this repo. Configure here.