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(routing): address state consistency and type safety issues
Sameer Kankute8db867c51c2b
diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.pyindex 48e83f1395b..96a271dd027 100644--- a/litellm/proxy/litellm_pre_call_utils.py+++ b/litellm/proxy/litellm_pre_call_utils.py@@ -1311,13 +1311,20 @@ def _update_model_if_team_alias_exists(# Skip alias rewrite if this model resolves to team-specific deployments# (team models use team_public_model_name, not model_aliases)- # Use O(1) index lookup instead of map_team_model to avoid O(n) scan- if llm_router and user_api_key_dict.team_id:- key = (user_api_key_dict.team_id, _model)- if key in llm_router.team_model_to_deployment_indices:- return-- data["model"] = user_api_key_dict.team_model_aliases[_model]+ aliased_target = user_api_key_dict.team_model_aliases[_model]++ # Check if the alias points to a stale team-scoped UUID name+ # (format: "model_name_{team_id}_{uuid}")+ if aliased_target.startswith(f"model_name_{user_api_key_dict.team_id}_"):+ # This is a stale alias from pre-PR deployments.+ # Check if current team deployments exist for the public name.+ if llm_router:+ key = (user_api_key_dict.team_id, _model)+ if key in llm_router.team_model_to_deployment_indices:+ # Team deployments exist; skip stale alias+ return++ data["model"] = aliased_targetreturndiff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.pyindex 7682b65657a..7d0181a3687 100644--- a/litellm/proxy/management_endpoints/model_management_endpoints.py+++ b/litellm/proxy/management_endpoints/model_management_endpoints.py@@ -466,7 +466,7 @@ async def _update_existing_team_model_assignment(db_model: Deployment,patch_data: updateDeployment,user_api_key_dict: UserAPIKeyAuth,- prisma_client: PrismaClient,+ prisma_client: Optional[PrismaClient],) -> None:"""Update an existing team model if the public name changed."""old_public_name = (diff --git a/litellm/router.py b/litellm/router.pyindex 130979d25bc..1a57d2f8c0d 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -7135,16 +7135,17 @@ class Router:# Update model_name_to_deployment_indicesfor model_name, indices in list(self.model_name_to_deployment_indices.items()):- # Remove the deleted index- if removal_idx in indices:- indices.remove(removal_idx)-- # Decrement all indices greater than removal_idx+ # Build new list without mutating the originalupdated_indices = []for idx in indices:- if idx > removal_idx:+ if idx == removal_idx:+ # Skip the removed index+ continue+ elif idx > removal_idx:+ # Decrement indices after removalupdated_indices.append(idx - 1)else:+ # Keep indices before removal unchangedupdated_indices.append(idx)# Update or remove the entry@@ -7155,16 +7156,17 @@ class Router:# Update team_model_to_deployment_indicesfor key, indices in list(self.team_model_to_deployment_indices.items()):- # Remove the deleted index- if removal_idx in indices:- indices.remove(removal_idx)-- # Decrement all indices greater than removal_idx+ # Build new list without mutating the originalupdated_indices = []for idx in indices:- if idx > removal_idx:+ if idx == removal_idx:+ # Skip the removed index+ continue+ elif idx > removal_idx:+ # Decrement indices after removalupdated_indices.append(idx - 1)else:+ # Keep indices before removal unchangedupdated_indices.append(idx)# Update or remove the entry