Repository navigation
fix(router): honor disable_fallbacks on mid-stream fallback - #43111
Conversation
The mid-stream fallback hop on chat, Responses, and Messages hardcoded disable_fallbacks=False, so a request or key that opted out of fallbacks still got a fallback deployment's answer when the primary's stream died before its first chunk. Each hop now reads the opt-out from the request the way the pre-stream path does, and the sync chat stream re-raises the primary's error instead of re-entering the fallback chain
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…ugh the public entrypoints
|
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 cc26034. Configure here.
TLDR
Problem this solves:
disable_fallbackson the request or key was ignored once a stream died before its first chunkdisable_fallbacks=Falseon chat, Responses, and MessagesHow it solves it:
User Flow
Before: a developer whose key opts out of fallbacks still gets a fallback deployment's answer when the primary's stream dies before its first chunk
primary-openaiwith a fallback tofallback-openaiunderrouter_settings.fallbacksPOST https://litellm-domain/key/generateand{"metadata": {"disable_fallbacks": true}}, or sends"disable_fallbacks": truein each request bodyPOST https://litellm-domain/v1/chat/completionswith"model": "primary-openai", "stream": truewhile the primary answers 200 and then an error event before any content; the same on/v1/responsesand/v1/messagesmodelisfallback-openai: the answer came from the deployment they opted out ofAfter: the same request surfaces the primary's own error and never touches the fallback
primary-openaiwith a fallback tofallback-openaiunderrouter_settings.fallbacksPOST https://litellm-domain/key/generateand{"metadata": {"disable_fallbacks": true}}, or sends"disable_fallbacks": truein each request bodyPOST https://litellm-domain/v1/chat/completionswith"model": "primary-openai", "stream": truewhile the primary answers 200 and then an error event before any content; the same on/v1/responsesand/v1/messagesOpenAIException - The server had an error while processing your requeston/v1/chat/completions, aresponse.failedevent withserver_erroron/v1/responses, HTTP 503Overloadedon/v1/messages; a key without the opt-out still gets the fallback's 200Linear ticket
Resolves LIT-8606
Screenshots / Proof of Fix
Live proxy QA against real providers, both legs on the same rig, only the proxy commit differing. Setup:
primary-openai(openai/gpt-5.4-mini) andprimary-claude(anthropic/claude-sonnet-5) point at a local upstream that answers every request200 text/event-streamand then sends one in-stream error event before any content, in each API's own wire shape (what an overloaded provider does)router_settings.fallbacks:primary-openai->fallback-openai(real OpenAIgpt-5.4-mini) andprimary-claude->fallback-claude(real Anthropicclaude-haiku-4-5),num_retries: 0POST /key/generate:<optout key>created with{"metadata": {"disable_fallbacks": true}}and<control key>with no metadata; the body opt-out cases send"disable_fallbacks": truewith the control key--num_workers 2) on the same Postgres DB; the primary never produces content, so any text in a response is the fallback'sBefore (eff6fc1)
Every request returns HTTP 200 served by the fallback deployment, opt-out or not
/v1/chat/completions
{"metadata": {"disable_fallbacks": true}}"disable_fallbacks": truein the body/v1/responses
{"metadata": {"disable_fallbacks": true}}"disable_fallbacks": truein the body/v1/messages
{"metadata": {"disable_fallbacks": true}}"disable_fallbacks": truein the bodyAfter (cc26034)
The control requests still get the fallback's 200; every opt-out request gets the primary's own error: HTTP 500 on
/v1/chat/completions, aresponse.failedevent carryingserver_erroron/v1/responses(the Responses API's own shape for a stream that fails after it opened), HTTP 503Overloadedon/v1/messages/v1/chat/completions
{"metadata": {"disable_fallbacks": true}}"disable_fallbacks": truein the body/v1/responses
{"metadata": {"disable_fallbacks": true}}"disable_fallbacks": truein the body/v1/messages
{"metadata": {"disable_fallbacks": true}}"disable_fallbacks": truein the bodyObservations from the run:
response.failed: API's shape, left alonemodelprimary-claude: pre-existing, left aloneType
🐛 Bug Fix
Caveats (if any)
Low
On the three async surfaces an opted-out request still logs
Fallback also failed: <primary error>at error level before the primary's error reaches the caller, left as is: a guarded log adds a branch to three functions at ruff's complexity ceiling or rewords the message on the genuine fallback-failed path, and the line still carries the primary's real errormcp-integration / Run tests(Unit Tests, not required) red at cc26034 in run 36086449586 ontest_cancellation_delivers_termination_over_tcp[True-hang_body-5-wait_for], a runner-timing TimeoutError: main's last seven Unit Tests runs (36085675306 back to 36083395188) are green, the same test family failed on the unrelated run 36085591635 ([True-hang-5-task]), and all 48 parameters pass locally at the tip in 60sci/circleci: local_testing_part1red at cc26034 (job 2215463) ontest_get_model_info_bedrock_cross_region_capability_parity, the same cost-map drift main's pipelines 90363 (job 2214856) and 90306 (job 2212274) fail on; no router code in its pathci/circleci: local_testing_part2red at cc26034 (job 2215432) ontest_openai_stream_options_call_text_completionwith the'MockValSer' object is not an instance of 'SchemaSerializer'mock leak that main's pipeline 90306 (job 2212310) shows too; a directlitellm.text_completionstream, no router in its pathci/circleci: using_litellm_on_windowsred at cc26034 (job 2215462): its Install Dependencies step timed out before any test ran, the same way main's pipelines 90363 (job 2214877) and 90306 (job 2212279) failci/circleci: unitred at cc26034 (job 2215470): 18 of its 24 failures are the same ones main's pipeline 90363 unit job 2214837 fails on, and the six tip-only ones aretests/unit/proxy/test_proxy_server.pyauth tests this PR never touches (nolitellm/proxyortests/unit/proxypath in the diff) that pass locally at the tip; main's pipeline 90306 unit job 2212287 was green, so the shard is order-dependentci/circleci: build_and_testred at cc26034 (job 2215435):test_key_model_listfails the same way on main's jobs 2214865 and 2212306, and the twotest_implemented_namespace_case_modules_remain_importable[...sdk.ocr...]rows import harness case modules that refactor(ocr): remove the Python OCR execution path and require the Rust route #43081 (e2302be) removed from the tree before this branch's merge base whiletests/test_rust_python_harness.pystill lists them; main's last build_and_test runs (pipelines 90306 and 90364) predate that removal, so main's next run inherits the same red, and nothing in this diff touches the harnessIntended behavior change: a caller who opted out of fallbacks (key metadata or request body
disable_fallbacks: true) and whose stream dies before its first chunk used to get a fallback deployment's 200 and now gets the primary's own error, on the SDK sync stream too; that is the control doing what the ticket asks for, so changing it back would undo the fix, and a caller without the opt-out still gets the fallback exactly as beforecc26034 passes /live-pr-risk