Skip to content

fix(router): honor disable_fallbacks on mid-stream fallback - #43111

Merged
mateo-berri merged 2 commits into
mainfrom
litellm_midstream_fallback_honors_disable_fallbacks
Sep 25, 2026
Merged

mateo-berri merged 2 commits into
mainfrom
litellm_midstream_fallback_honors_disable_fallbacks

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • disable_fallbacks on the request or key was ignored once a stream died before its first chunk
  • The mid-stream hop hardcoded disable_fallbacks=False on chat, Responses, and Messages
  • A caller who opted out still got a fallback deployment's answer, billed as such

How it solves it:

  • Each mid-stream hop reads the opt-out from the request, the way the pre-stream path does
  • The sync chat stream re-raises the primary's error instead of re-entering the fallback chain
  • A fourth site beyond the ticket's three: the sync SDK stream had the same gap, so it is fixed here too
  • Fourteen regression tests, one per surface and opt-out shape plus the public entrypoints, all failing on main before the fix
  • Docs: docs(proxy): say disable_fallbacks also covers mid-stream fallback litellm-docs#1714 says the opt-out covers the mid-stream hop too

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

  1. The proxy admin configures primary-openai with a fallback to fallback-openai under router_settings.fallbacks
  2. The developer generates a key with POST https://litellm-domain/key/generate and {"metadata": {"disable_fallbacks": true}}, or sends "disable_fallbacks": true in each request body
  3. They send POST https://litellm-domain/v1/chat/completions with "model": "primary-openai", "stream": true while the primary answers 200 and then an error event before any content; the same on /v1/responses and /v1/messages
  4. They get HTTP 200 with chunks whose model is fallback-openai: the answer came from the deployment they opted out of

After: the same request surfaces the primary's own error and never touches the fallback

  1. The proxy admin configures primary-openai with a fallback to fallback-openai under router_settings.fallbacks
  2. The developer generates a key with POST https://litellm-domain/key/generate and {"metadata": {"disable_fallbacks": true}}, or sends "disable_fallbacks": true in each request body
  3. They send POST https://litellm-domain/v1/chat/completions with "model": "primary-openai", "stream": true while the primary answers 200 and then an error event before any content; the same on /v1/responses and /v1/messages
  4. They get the primary's own error back instead of a fallback's answer: HTTP 500 OpenAIException - The server had an error while processing your request on /v1/chat/completions, a response.failed event with server_error on /v1/responses, HTTP 503 Overloaded on /v1/messages; a key without the opt-out still gets the fallback's 200

Linear 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) and primary-claude (anthropic/claude-sonnet-5) point at a local upstream that answers every request 200 text/event-stream and 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 OpenAI gpt-5.4-mini) and primary-claude -> fallback-claude (real Anthropic claude-haiku-4-5), num_retries: 0
  • Two keys from POST /key/generate: <optout key> created with {"metadata": {"disable_fallbacks": true}} and <control key> with no metadata; the body opt-out cases send "disable_fallbacks": true with the control key
  • Each proxy runs with two uvicorn workers (--num_workers 2) on the same Postgres DB; the primary never produces content, so any text in a response is the fallback's

Before (eff6fc1)

Every request returns HTTP 200 served by the fallback deployment, opt-out or not

/v1/chat/completions

  1. Control key, no opt-out
curl -sS -X POST http://127.0.0.1:31552/chat/completions -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
data: {"id":"chatcmpl-ERpoEWK6p7oVHiWsJFkCR2Wm1bTUQ","object":"chat.completion.chunk","created":1790302718,"model":"fallback-openai","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}
data: {"id":"chatcmpl-ERpoEWK6p7oVHiWsJFkCR2Wm1bTUQ","object":"chat.completion.chunk","created":1790302718,"model":"fallback-openai","choices":[{"index":0,"delta":{"content":"Hi"}}]}
  1. Key generated with {"metadata": {"disable_fallbacks": true}}
curl -sS -X POST http://127.0.0.1:31552/chat/completions -H 'Authorization: Bearer <optout key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
data: {"id":"chatcmpl-ERpoFHhBP95ix8L0eYLpPxdQbTQdJ","object":"chat.completion.chunk","created":1790302719,"model":"fallback-openai","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}
data: {"id":"chatcmpl-ERpoFHhBP95ix8L0eYLpPxdQbTQdJ","object":"chat.completion.chunk","created":1790302719,"model":"fallback-openai","choices":[{"index":0,"delta":{"content":"Hi"}}]}
  1. Control key plus "disable_fallbacks": true in the body
curl -sS -X POST http://127.0.0.1:31552/chat/completions -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "disable_fallbacks": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
data: {"id":"chatcmpl-ERpoG93HxmvXG0eKDutYzV3lva9js","object":"chat.completion.chunk","created":1790302720,"model":"fallback-openai","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}
data: {"id":"chatcmpl-ERpoG93HxmvXG0eKDutYzV3lva9js","object":"chat.completion.chunk","created":1790302720,"model":"fallback-openai","choices":[{"index":0,"delta":{"content":"Hi"}}]}

/v1/responses

  1. Control key, no opt-out
curl -sS -X POST http://127.0.0.1:31552/v1/responses -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "input": "Say hi in three words."}'
HTTP 200
data: {"type":"response.created","response":{"id":"resp_...", "model":"gpt-5.4-mini-2026-03-17", ... }}
data: {"type":"response.output_text.delta","item_id":"msg_02cc77e86284c387006ab5da012fec87d09b903e25103ca9e9","output_index":0,"content_index":0,"delta":"Hi","logprobs":[],"obfuscation":"0GtIJDgGry3nBB","sequence_number" ...
  1. Key generated with {"metadata": {"disable_fallbacks": true}}
curl -sS -X POST http://127.0.0.1:31552/v1/responses -H 'Authorization: Bearer <optout key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "input": "Say hi in three words."}'
HTTP 200
data: {"type":"response.created","response":{"id":"resp_...", "model":"gpt-5.4-mini-2026-03-17", ... }}
data: {"type":"response.output_text.delta","item_id":"msg_03d63990404583a0006ab5da0203d087d2bd57da1571e65622","output_index":0,"content_index":0,"delta":"Hi","logprobs":[],"obfuscation":"iEcXKNdFDryLFl","sequence_number" ...
  1. Control key plus "disable_fallbacks": true in the body
curl -sS -X POST http://127.0.0.1:31552/v1/responses -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "disable_fallbacks": true, "input": "Say hi in three words."}'
HTTP 200
data: {"type":"response.created","response":{"id":"resp_...", "model":"gpt-5.4-mini-2026-03-17", ... }}
data: {"type":"response.output_text.delta","item_id":"msg_01abae566a2aecb3006ab5da02f60c87d083945fc720dabbd1","output_index":0,"content_index":0,"delta":"Hi","logprobs":[],"obfuscation":"8MScu7nyZnEz4k","sequence_number" ...

/v1/messages

  1. Control key, no opt-out
curl -sS -X POST http://127.0.0.1:31552/v1/messages -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-claude", "stream": true, "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
event: message_start
data: {"type":"message_start","message":{"model":"primary-claude", ... }}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hi there"}          }
  1. Key generated with {"metadata": {"disable_fallbacks": true}}
curl -sS -X POST http://127.0.0.1:31552/v1/messages -H 'Authorization: Bearer <optout key>' -H 'Content-Type: application/json' -d '{"model": "primary-claude", "stream": true, "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
event: message_start
data: {"type":"message_start","message":{"model":"primary-claude", ... }}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hi there"}}
  1. Control key plus "disable_fallbacks": true in the body
curl -sS -X POST http://127.0.0.1:31552/v1/messages -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-claude", "stream": true, "max_tokens": 32, "disable_fallbacks": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
event: message_start
data: {"type":"message_start","message":{"model":"primary-claude", ... }}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hi,"}        }

After (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, a response.failed event carrying server_error on /v1/responses (the Responses API's own shape for a stream that fails after it opened), HTTP 503 Overloaded on /v1/messages

/v1/chat/completions

  1. Control key, no opt-out
curl -sS -X POST http://127.0.0.1:39855/chat/completions -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
data: {"id":"chatcmpl-ERpzeDkDqsv5hcNpOvRwrmqgYJRjp","object":"chat.completion.chunk","created":1790303427,"model":"fallback-openai","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}
data: {"id":"chatcmpl-ERpzeDkDqsv5hcNpOvRwrmqgYJRjp","object":"chat.completion.chunk","created":1790303427,"model":"fallback-openai","choices":[{"index":0,"delta":{"content":"Hi"}}]}
  1. Key generated with {"metadata": {"disable_fallbacks": true}}
curl -sS -X POST http://127.0.0.1:39855/chat/completions -H 'Authorization: Bearer <optout key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 500
{"error":{"message":"litellm.APIConnectionError: APIConnectionError: OpenAIException - The server had an error while processing your request. Sorry about that!","type":null,"param":null,"code":"500"}}
  1. Control key plus "disable_fallbacks": true in the body
curl -sS -X POST http://127.0.0.1:39855/chat/completions -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "disable_fallbacks": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 500
{"error":{"message":"litellm.APIConnectionError: APIConnectionError: OpenAIException - The server had an error while processing your request. Sorry about that!","type":null,"param":null,"code":"500"}}

/v1/responses

  1. Control key, no opt-out
curl -sS -X POST http://127.0.0.1:39855/v1/responses -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "input": "Say hi in three words."}'
HTTP 200
data: {"type":"response.created","response":{"id":"resp_...", "model":"gpt-5.4-mini-2026-03-17", ... }}
data: {"type":"response.output_text.delta","item_id":"msg_0732554f72131202006ab5dcc5091487d288fffa3b8dcc27fa","output_index":0,"content_index":0,"delta":"Hi","logprobs":[],"obfuscation":"QKDnesFXobQFRn","sequence_number" ...
  1. Key generated with {"metadata": {"disable_fallbacks": true}}
curl -sS -X POST http://127.0.0.1:39855/v1/responses -H 'Authorization: Bearer <optout key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "input": "Say hi in three words."}'
HTTP 200
event: response.failed
data: {"type":"response.failed","response":{"id":"resp_8bfcffabe0fc40d388e5c02dc3660d09","created_at":1790303429,"error":{"code":"server_error","message":"Response API in-stream error"},"object":"response","output":[],"status":"failed"},"sequence_number":0}
  1. Control key plus "disable_fallbacks": true in the body
curl -sS -X POST http://127.0.0.1:39855/v1/responses -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-openai", "stream": true, "disable_fallbacks": true, "input": "Say hi in three words."}'
HTTP 200
event: response.failed
data: {"type":"response.failed","response":{"id":"resp_76055342b0d04e79ad31e3a3a6ae1dc3","created_at":1790303429,"error":{"code":"server_error","message":"Response API in-stream error"},"object":"response","output":[],"status":"failed"},"sequence_number":0}

/v1/messages

  1. Control key, no opt-out
curl -sS -X POST http://127.0.0.1:39855/v1/messages -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-claude", "stream": true, "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 200
event: message_start
data: {"type":"message_start","message":{"model":"primary-claude", ... }}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hi,"} }
  1. Key generated with {"metadata": {"disable_fallbacks": true}}
curl -sS -X POST http://127.0.0.1:39855/v1/messages -H 'Authorization: Bearer <optout key>' -H 'Content-Type: application/json' -d '{"model": "primary-claude", "stream": true, "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 503
{"error":{"message":"litellm.APIError: Overloaded","type":"internal_server_error","param":null,"code":"503"}}
  1. Control key plus "disable_fallbacks": true in the body
curl -sS -X POST http://127.0.0.1:39855/v1/messages -H 'Authorization: Bearer <control key>' -H 'Content-Type: application/json' -d '{"model": "primary-claude", "stream": true, "max_tokens": 32, "disable_fallbacks": true, "messages": [{"role": "user", "content": "Say hi in three words."}]}'
HTTP 503
{"error":{"message":"litellm.APIError: Overloaded","type":"internal_server_error","param":null,"code":"503"}}

Observations from the run:

  • Responses opt-out fails as 200 response.failed: API's shape, left alone
  • Messages fallback stream labels model primary-claude: pre-existing, left alone

Type

🐛 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 error

  • mcp-integration / Run tests (Unit Tests, not required) red at cc26034 in run 36086449586 on test_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 60s

  • ci/circleci: local_testing_part1 red at cc26034 (job 2215463) on test_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 path

  • ci/circleci: local_testing_part2 red at cc26034 (job 2215432) on test_openai_stream_options_call_text_completion with the 'MockValSer' object is not an instance of 'SchemaSerializer' mock leak that main's pipeline 90306 (job 2212310) shows too; a direct litellm.text_completion stream, no router in its path

  • ci/circleci: using_litellm_on_windows red 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) fail

  • ci/circleci: unit red 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 are tests/unit/proxy/test_proxy_server.py auth tests this PR never touches (no litellm/proxy or tests/unit/proxy path in the diff) that pass locally at the tip; main's pipeline 90306 unit job 2212287 was green, so the shard is order-dependent

  • ci/circleci: build_and_test red at cc26034 (job 2215435): test_key_model_list fails the same way on main's jobs 2214865 and 2212306, and the two test_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 while tests/test_rust_python_harness.py still 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 harness

  • Intended 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 before

  • cc26034 passes /live-pr-risk

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
@devin-ai-integration

devin-ai-integration Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Changes when mid-stream fallbacks are skipped during streaming.

The PR appears safe to merge; no new actionable issue was identified.

Summary

The PR makes chat, Responses, and Messages streams honor a request’s fallback opt-out when the primary stream fails before its first chunk.

  • Adds regression tests for raw and metadata-stamped opt-outs.
  • Adds sync and async chat entrypoint tests for the request-to-stream handoff.

Reviews (2) · Last reviewed commit: "test(router): prove disable_fallbacks re..."

Comment thread tests/test_litellm/test_router.py Resolved
@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_midstream_fallback_honors_disable_fallbacks (cc26034) with main (cbba146)

Open in CodSpeed

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot 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.

✅ 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.

@mateo-berri mateo-berri added run-ci and removed run-ci labels Sep 25, 2026

@mateo-berri mateo-berri 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.

LGTM

@mateo-berri
mateo-berri merged commit f3cf1cd into main Sep 25, 2026
154 of 161 checks passed
@mateo-berri
mateo-berri deleted the litellm_midstream_fallback_honors_disable_fallbacks branch September 25, 2026 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant