The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): honor key-level disable_fallbacks after first pre-call pass
yucheng9974cf4bf817
diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.pyindex d18fab1c9f0..36b49ef064c 100644--- a/litellm/proxy/common_request_processing.py+++ b/litellm/proxy/common_request_processing.py@@ -2093,7 +2093,12 @@ class ProxyBaseLLMRequestProcessing:except ProxyRateLimitError as original_exc:rate_limited_data: Final = self.dataoriginal_model: Final = rate_limited_data.get("model")- if pristine is None or not configured_fallbacks or not isinstance(original_model, str):+ if (+ pristine is None+ or not configured_fallbacks+ or rate_limited_data.get("disable_fallbacks")+ or not isinstance(original_model, str)+ ):raisefallback_models: Final = self._resolve_fallback_models(diff --git a/tests/proxy_unit_tests/test_response_polling_pre_call_checks.py b/tests/proxy_unit_tests/test_response_polling_pre_call_checks.pyindex 459834d0fd2..9f1a228855e 100644--- a/tests/proxy_unit_tests/test_response_polling_pre_call_checks.py+++ b/tests/proxy_unit_tests/test_response_polling_pre_call_checks.py@@ -87,7 +87,7 @@ class TestSkipPreCallLogic:await processor.base_process_llm_request(request=MagicMock(spec=Request),fastapi_response=MagicMock(spec=Response),- user_api_key_dict=MagicMock(spec=UserAPIKeyAuth),+ user_api_key_dict=UserAPIKeyAuth(),route_type="aresponses",proxy_logging_obj=mock_proxy_logging,llm_router=MagicMock(),diff --git a/tests/test_litellm/proxy/test_common_request_processing.py b/tests/test_litellm/proxy/test_common_request_processing.pyindex 7d9a78fd981..e9d74a46daa 100644--- a/tests/test_litellm/proxy/test_common_request_processing.py+++ b/tests/test_litellm/proxy/test_common_request_processing.py@@ -6410,7 +6410,9 @@ class TestPreCallWithFallbacksOnLocalRateLimit:@staticmethoddef _otel_key(- rpm_limit: int | None = None, model_rpm_limit: dict[str, int] | None = None+ rpm_limit: int | None = None,+ model_rpm_limit: dict[str, int] | None = None,+ disable_fallbacks: bool = False,) -> ProxyUserAPIKeyAuth:from opentelemetry.sdk.trace import TracerProvider@@ -6419,7 +6421,10 @@ class TestPreCallWithFallbacksOnLocalRateLimit:api_key="hashed-key",parent_otel_span=span,rpm_limit=rpm_limit,- metadata={"model_rpm_limit": model_rpm_limit} if model_rpm_limit else {},+ metadata={+ **({"model_rpm_limit": model_rpm_limit} if model_rpm_limit else {}),+ **({"disable_fallbacks": True} if disable_fallbacks else {}),+ },)@staticmethod@@ -6540,6 +6545,27 @@ class TestPreCallWithFallbacksOnLocalRateLimit:assert data["model"] == fallback_modelassert rig[3] == [primary_model, primary_model, fallback_model]+ @pytest.mark.asyncio+ async def test_key_metadata_disable_fallbacks_returns_429_instead_of_retrying(+ self, monkeypatch: pytest.MonkeyPatch+ ):+ """``disable_fallbacks`` set in key metadata only lands on ``data`` during the first+ pre-call pass (``add_key_level_controls``), so it must be honored after that pass."""+ from litellm.proxy.common_utils.proxy_rate_limit_error import ProxyRateLimitError++ primary_model = "gpt-4.1"+ fallback_model = "gpt-4.1-mini"+ key = self._otel_key(model_rpm_limit={primary_model: 1}, disable_fallbacks=True)+ rig = self._v3_limiter_rig(monkeypatch, key, [{primary_model: [fallback_model]}])+ request = {"model": primary_model, "messages": [{"role": "user", "content": "hi"}]}++ await self._pre_call(dict(request), key, rig)+ with pytest.raises(ProxyRateLimitError) as exc_info:+ await self._pre_call(dict(request), key, rig)++ assert exc_info.value.status_code == 429+ assert rig[3] == [primary_model, primary_model]+class _RecordingSuccessLogger(CustomLogger):def __init__(self):