litellm/tests/unit/test_router/core
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
..
__init__.py
conftest.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
construction_precall_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
endpoints_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
fallbacks_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
gaps_l11100_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
registry_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
retries_health_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
rust_parity_pending.txt feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
scripted_host.py fix(router): free a failed rust router call's request without waiting for a full gc (#51963) 2026-11-15 17:04:38 -05:00
selection_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
streaming_helpers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_callback_filters_step.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_callback_filters_v2_dispatch.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_construction_precall_characterization.py fix(router): optionally reject raw model collisions across groups (#52895) 2026-11-19 13:36:28 -08:00
test_construction_precall_characterization_precall.py fix(router): count pre-call context-window tokens with each deployment's tokenizer (#49764) 2026-11-25 03:04:19 +00:00
test_deprecated_members.py fix(router): re-check and reserve per-model rate limits on fallback targets (#50994) 2026-11-10 16:36:10 -08:00
test_endpoints.py fix(streaming): preserve failure context without retaining requests (#52408) 2026-11-18 06:18:22 +00:00
test_endpoints_characterization_batch.py fix(managed files): keep one downstream copy per litellm_proxy deployment that covers every target it serves (#54181) 2026-11-25 06:07:13 +00:00
test_endpoints_characterization_generic.py feat(bedrock): search knowledge bases by image and return image hits (#51063) 2026-11-12 00:19:34 +00:00
test_endpoints_characterization_inference.py fix(llms): treat chat-latest and gpt-chat-latest as gpt-5 family models (#51151) 2026-11-24 04:02:51 -08:00
test_fallbacks.py fix(router)!: let an unmatched content_policy_fallbacks list fall through like context_window_fallbacks (#52132) 2026-11-17 09:28:49 -08:00
test_fallbacks_characterization.py fix(router): validate request fields and ignore scalar fallback overrides (#51502) 2026-11-13 01:24:23 +00:00
test_fallbacks_characterization_outcomes.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_fallbacks_characterization_refusal.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_fallbacks_walk_parity.py test(router): pin the usage minute in the usage refusal fallback tests (#54299) 2026-11-24 23:50:55 +00:00
test_gaps_adapter_timeout.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_fuzz_access.py fix(router): preserve team order and weighted failover across endpoints (#53264) 2026-11-20 18:18:11 -08:00
test_gaps_l0001.py test(router): pin the usage minute in the request scoped rpm check test (#54300) 2026-11-25 00:27:16 +00:00
test_gaps_l2800.py fix(router)!: stop a client's metadata flag from turning off silent_model mirroring (#52083) 2026-11-16 15:45:56 -08:00
test_gaps_l2800_streaming.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_l5600.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_l5600_anthropic.py fix(streaming): preserve failure context without retaining requests (#52408) 2026-11-18 06:18:22 +00:00
test_gaps_l8400_precall.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_gaps_l8400_pricing.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_l8400_retries.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_l11100_registry.py fix(openrouter): report the context window and output cap from OpenRouter's catalog (#54046) 2026-11-24 04:37:30 -08:00
test_gaps_l11100_strategy.py fix(management): keep classifier deployments serving during reloads (#51111) 2026-11-11 18:48:06 +00:00
test_gaps_l14000.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_l14000_selection.py fix(responses): accept a string or tuple allowed_openai_params on Responses models instead of answering 500 (#51751) 2026-11-14 04:32:38 -05:00
test_gaps_pick_input_and_compaction.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_request_overrides.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_router_core_crate.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_gaps_router_core_crate_picks.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_health_registry_views.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_host_settings_changes_characterization.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_legacy_members.py feat(redis): serve the proxy's common Redis call with Rust stores (#50170) 2026-11-06 06:01:11 +00:00
test_parity_cooldown_service_model.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_parity_decisions_routing.py fix(router): preserve team order and weighted failover across endpoints (#53264) 2026-11-20 18:18:11 -08:00
test_parity_failure_logging.py fix(proxy): the failure row after a failed fallback stands for the attempt the client saw (#53313) 2026-11-21 19:52:06 +00:00
test_parity_malformed_after_rate_limit.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_parity_pick_count_and_fallback_log.py fix(router)!: let an unmatched content_policy_fallbacks list fall through like context_window_fallbacks (#52132) 2026-11-17 09:28:49 -08:00
test_parity_provider_scoped_fallbacks.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_parity_regress5.py fix(bedrock)!: split the OpenAI breakpoint and Bedrock cachePoint model flags (#52180) 2026-11-16 18:50:32 -08:00
test_parity_regress5_errors.py fix(router): sync responses() applies the Router's num_retries and fallbacks (#51011) 2026-11-10 12:55:49 -08:00
test_parity_regress5_settings.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_parity_regress5_streams.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_parity_response_headers.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_parity_rulings_2026_11.py feat(redis): serve the health checks' deployment states from a native store (#50357) 2026-11-13 03:22:28 +00:00
test_registry_changesets.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_registry_characterization_construction.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_registry_characterization_lookups.py fix(router): hold the registry lock across router-core writes and mirror reads (#50933) 2026-11-10 17:58:46 +00:00
test_registry_characterization_model_info.py test(router): pin the usage minute in the group usage tests (#54301) 2026-11-24 23:49:49 +00:00
test_registry_characterization_writes.py fix(management): keep classifier deployments serving during reloads (#51111) 2026-11-11 18:48:06 +00:00
test_request_scoped_outcomes.py refactor(router): delete router code nothing calls (#52516) 2026-11-19 06:11:10 +09:00
test_retries_cooldowns.py feat(proxy): total_timeout bounds a whole non-streaming request (#50496) 2026-11-08 00:43:50 -08:00
test_retries_health_characterization_cooldowns.py feat(proxy): skip blocked deployments in background health checks (#52761) 2026-11-24 07:53:23 -08:00
test_retries_health_characterization_failure_exits.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_retries_health_characterization_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_selection.py fix(router): optionally reject raw model collisions across groups (#52895) 2026-11-19 13:36:28 -08:00
test_selection_characterization_groups.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_selection_characterization_prerouting.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_selection_characterization_session_repin.py feat(redis): serve the router's session pins from a native store (#50870) 2026-11-13 03:22:28 +00:00
test_selection_characterization_strategies.py feat(router): add the Rust router core with the host as its only boundary (#49513) 2026-11-04 21:47:58 -08:00
test_selection_characterization_traffic.py test: fix four flaky router and s3 log unit tests (#53034) 2026-11-20 18:27:51 +00:00
test_streaming_characterization_anthropic.py fix(router): keep both errors when a streamed cross-group fallback also fails (#52217) 2026-11-16 22:04:16 -08:00
test_streaming_characterization_chat.py feat(guardrails): reject requests that name an unregistered guardrail when reject_unregistered_guardrails is on (#53214) 2026-11-20 18:39:29 -08:00
test_streaming_characterization_outcomes.py feat(redis): serve the proxy's common Redis call with Rust stores (#50170) 2026-11-06 06:01:11 +00:00
test_streaming_characterization_responses.py fix(responses): forward lifecycle events once output starts (#52858) 2026-11-19 17:49:23 +00:00
test_usage_and_failure_counting_parity.py test(router): read failure usage across a minute change in the shared logging tests (#54356) 2026-11-25 05:55:07 +00:00