litellm/tests/unit/test_router
martenj4 9add785f1a
fix(router): with pre-call checks on, retry a context window error on a group deployment serving another model (#54157)
* fix(router): retry a context window error on a deployment serving another model

A model group can admit a prompt on a deployment whose cost-map window is wider than
what the provider account allows. The upstream answers 400 ContextWindowExceededError
and the router raised it at once, even though a sibling deployment in the same group
could serve the prompt, unless context_window_fallbacks was set.

Without a retry policy, the router now retries that error on a healthy sibling that
serves a different model (base_model, else model), skipping every deployment that serves
the refused model, and stops when none is left. A context window error the router raised
itself before any call is still not retried, context_window_fallbacks still take over,
and an explicit retry policy keeps its behavior. The stream retry path does the same.

* fix(router): keep the context window retry inside the deployments the router would pick

Count the deployments left to try with the same pick filters the router
uses (order, pinning, tags, pre-call checks), resolve the refused model
from wildcard copies, only let a matching context_window_fallbacks chain
skip the retry, and hand the original context window error to the caller
and fallbacks when the retries fail for another reason.

* fix(router): answer a context window error when the request num_retries cannot be read

A request num_retries the retry loop cannot compare raised the loop's
TypeError as a 500 once a context window error became retryable. Hand the
context window error on instead, as the request got before.

* fix(router): keep a guardrail block raised on a context window retry

A guardrail that blocks the retry now ends the call with its own error
instead of the saved context window error, so no fallback runs past it.
The two-deployment retry test now expects the context window error to
reach the second deployment, which serves another model.

* fix(router): retry a context window error in the group only with pre-call checks and raise the refusal

The in-group retry after a context window error now runs only when
enable_pre_call_checks is on, and never on a call by deployment id.
Every deployment serving the refused model stays out of later picks in
the call, a failed retry raises the refused attempt's own error, a
stream keeps retrying past each refusing model, the siblings come from
the group a pre-routing hook picked, and a context window error that
first shows up on a later retry is kept. Router.should_retry_this_error
raises a context window error as on main

* fix(router): read one model under two names as one model and retry a copy that declares a bigger window

A context window retry now compares the served model by its last path segment with Bedrock region profiles read as their base model, so us. and global. profiles of one model are skipped together. A copy of the refused model is still retried when both pre-call windows are known and the copy's is bigger

* fix(router): keep a cooled-down deployment's window for the context window retry

A deployment that cools down on its 400 drops out of the pre-call facts, so its
window read as unknown and every copy of the same model was skipped, the copy
declaring a bigger window included. The window now comes from the deployment's
record when the facts no longer carry it.

The refusal also skips its candidate pass when pre-call checks are off, since
no retry can follow it there.

* style(router): format the context window callback filter test with rustfmt

* fix(router): keep refused deployments when a stream retry takes over

A stream that a retry opened and that breaks before content now carries the deployments the request already refused, and the provider's window refusal, into the retry or fallback that takes it over

* test(router): mark the stream refusal take-over test with rstest

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-25 10:57:40 +00:00
..
core fix(router): with pre-call checks on, retry a context window error on a group deployment serving another model (#54157) 2026-11-25 10:57:40 +00:00
__init__.py test: add __init__.py to every tests/unit directory phase 16 touches 2026-09-20 11:52:38 +00:00
test_enforce_model_rate_limits.py fix(router): count streamed responses against deployment TPM (#54283) 2026-11-24 18:06:07 -08:00
test_io_token_rate_limits.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_router.py fix(router): count pre-call context-window tokens with each deployment's tokenizer (#49764) 2026-11-25 03:04:19 +00:00
test_router_callback_hook_sequence.py test: replace 61 live logging and otel tests with offline unit and integration coverage (#45362) 2026-10-08 15:55:57 -07:00
test_router_endpoints.py fix(proxy): resolve admin oidc api_key references on batch cancel, fine-tuning listing, OCR, /v1/messages and cache prediction (#53678) 2026-11-23 10:11:10 -08:00
test_router_entry_points.py feat(proxy): mixed Codex model picker and LiteLLM routing on native codex routes (#49408) 2026-11-06 11:14:47 -08:00
test_router_facade.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_router_fallbacks.py fix(router): preserve destination bearer credentials on Anthropic fallbacks (#54271) 2026-11-24 17:12:02 -08:00
test_router_fuzz_regressions_rust_only.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_router_get_deployments.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_router_helper_utils.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_router_host.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_router_max_parallel_requests.py feat(tts)!: stream audio from the provider as it arrives on /v1/audio/speech (#46222) 2026-11-17 15:03:12 +00:00
test_router_provider_endpoints.py feat(redis): serve the proxy's common Redis call with Rust stores (#50170) 2026-11-06 06:01:11 +00:00
test_router_retries.py fix(router): with pre-call checks on, retry a context window error on a group deployment serving another model (#54157) 2026-11-25 10:57:40 +00:00
test_router_surface_check.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
test_router_timeout.py feat(messages): serve bedrock invoke messages on rust only (#51330) 2026-11-15 16:58:31 -08:00
test_router_vector_store_endpoints.py test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00