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: guard empty-dict team limits and malformed int in deployment default limits
Ephrim Stanleyae0769b1dfb4
diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.pyindex b4e0093b91a..7d3427ed4c1 100644--- a/litellm/proxy/auth/auth_utils.py+++ b/litellm/proxy/auth/auth_utils.py@@ -555,11 +555,14 @@ def _get_deployment_default_limit(model_name: str, field: str) -> Optional[int]:deployments = llm_router.get_model_list(model_name=model_name)if not deployments:return None- limits = [- int(deployment.get("litellm_params", {}).get(field))- for deployment in deployments- if deployment.get("litellm_params", {}).get(field) is not None- ]+ limits = []+ for deployment in deployments:+ raw = deployment.get("litellm_params", {}).get(field)+ if raw is not None:+ try:+ limits.append(int(raw))+ except (ValueError, TypeError):+ passreturn min(limits) if limits else None@@ -602,7 +605,7 @@ def get_key_model_rpm_limit(# 3. Fallback to team metadataif user_api_key_dict.team_metadata:team_limit = user_api_key_dict.team_metadata.get("model_rpm_limit")- if team_limit:+ if team_limit is not None:return team_limit# 4. Fallback to deployment default_api_key_rpm_limit@@ -645,7 +648,7 @@ def get_key_model_tpm_limit(# 3. Fallback to team metadataif user_api_key_dict.team_metadata:team_limit = user_api_key_dict.team_metadata.get("model_tpm_limit")- if team_limit:+ if team_limit is not None:return team_limit# 4. Fallback to deployment default_api_key_tpm_limitdiff --git a/tests/test_litellm/proxy/auth/test_auth_utils.py b/tests/test_litellm/proxy/auth/test_auth_utils.pyindex 2058f61cb0f..b66c081a943 100644--- a/tests/test_litellm/proxy/auth/test_auth_utils.py+++ b/tests/test_litellm/proxy/auth/test_auth_utils.py@@ -71,6 +71,19 @@ class TestGetKeyModelRpmLimit:assert result is None+ def test_team_metadata_empty_rpm_dict_falls_through_to_deployment_default(self):+ """Explicitly empty team model_rpm_limit ({}) should be returned as-is, not fallen through."""+ # An empty dict is a valid team limit map (no per-model limits configured).+ # It should be returned directly rather than falling through to deployment defaults,+ # so a team with an empty map is treated as unconstrained at the team level.+ user_api_key_dict = UserAPIKeyAuth(+ api_key="sk-123",+ team_metadata={"model_rpm_limit": {}},+ )+ result = get_key_model_rpm_limit(user_api_key_dict)+ assert result == {}++class TestGetKeyModelTpmLimit:"""Tests for get_key_model_tpm_limit function."""@@ -137,6 +150,33 @@ class TestGetKeyModelTpmLimit:assert result == {"gpt-4": 10000}+ def test_team_metadata_empty_tpm_dict_falls_through_to_deployment_default(self):+ """Explicitly empty team model_tpm_limit ({}) should be returned as-is, not fallen through."""+ # An empty dict is a valid team limit map (no per-model limits configured).+ # It should be returned directly rather than falling through to deployment defaults,+ # so a team with an empty map is treated as unconstrained at the team level.+ user_api_key_dict = UserAPIKeyAuth(+ api_key="sk-123",+ team_metadata={"model_tpm_limit": {}},+ )+ result = get_key_model_tpm_limit(user_api_key_dict)+ assert result == {}+++ def test_skips_deployments_with_malformed_limit_value(self):+ """Deployments with non-integer-parseable limit values are skipped without raising."""+ user_api_key_dict = UserAPIKeyAuth(api_key="sk-123")+ mock_router = MagicMock()+ mock_router.get_model_list.return_value = [+ {"model_name": "model1", "litellm_params": {"default_api_key_tpm_limit": "not-a-number"}},+ _make_deployment_dict("model1", tpm=500),+ ]+ with patch(_ROUTER_PATCH, mock_router):+ result = get_key_model_tpm_limit(user_api_key_dict, model_name="model1")+ # The malformed deployment is skipped; the valid one provides 500+ assert result == {"model1": 500}++class TestGetCustomerIdFromStandardHeaders:"""Tests for _get_customer_id_from_standard_headers helper function."""@@ -414,6 +454,20 @@ class TestDeploymentDefaultRpmLimit:assert result == {"model1": 75}+ def test_skips_deployments_with_malformed_limit_value(self):+ """Deployments with non-integer-parseable limit values are skipped without raising."""+ user_api_key_dict = UserAPIKeyAuth(api_key="sk-123")+ mock_router = MagicMock()+ mock_router.get_model_list.return_value = [+ {"model_name": "model1", "litellm_params": {"default_api_key_rpm_limit": "not-a-number"}},+ _make_deployment_dict("model1", rpm=100),+ ]+ with patch(_ROUTER_PATCH, mock_router):+ result = get_key_model_rpm_limit(user_api_key_dict, model_name="model1")+ # The malformed deployment is skipped; the valid one provides 100+ assert result == {"model1": 100}++class TestDeploymentDefaultTpmLimit:"""Tests for deployment default_api_key_tpm_limit fallback in get_key_model_tpm_limit."""