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(router): repair deployment indices before releasing strategies on delete
Tin Chi Lo50bdf250f652
diff --git a/litellm/router.py b/litellm/router.pyindex 29f548ca284..487d6a31226 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -8505,20 +8505,24 @@ class Router:try:if deployment_idx is not None:- try:- deployment_to_remove = self.get_deployment(model_id=id)- except Exception:- deployment_to_remove = None# Pop the item from the list firstitem = self.model_list.pop(deployment_idx)- if deployment_to_remove is not None:- self._unregister_pre_routing_strategy_for_deployment(deployment=deployment_to_remove)self._invalidate_model_group_info_cache()self._invalidate_access_groups_cache()self._update_deployment_indices_after_removal(model_id=id, removal_idx=deployment_idx)_budget_limiter = self._get_router_deployment_budget_limiter()if _budget_limiter is not None:_budget_limiter.unregister_deployment_budget(model_id=id)+ try:+ self._unregister_pre_routing_strategy_for_deployment(+ deployment=item if isinstance(item, Deployment) else Deployment(**item)+ )+ except Exception:+ verbose_router_logger.exception(+ "delete_deployment: could not release pre-routing strategies for model_id=%s; "+ "the deployment is out of the model_list and its indices are repaired",+ id,+ )return itemelse:return Nonediff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.pyindex f04e4a60283..ad4e430c603 100644--- a/tests/test_litellm/test_router.py+++ b/tests/test_litellm/test_router.py@@ -6128,6 +6128,22 @@ class TestPreRoutingStrategyRegistryLifecycle:assert len(registered) == 1assert set(registered[0].strategy.config.available_models) == {"gpt-4o", "gpt-4o-mini"}+ def test_delete_repairs_indices_even_when_strategy_release_fails(self):+ """Structural removal and strategy release are not equally critical. Once the entry+ leaves model_list the index maps must be repaired no matter what, so releasing the+ registry slot runs after that repair and cannot abandon the router half-updated."""+ router = self._router_with_complexity_router()+ idx = router.model_id_to_deployment_index_map["router-1"]+ router.model_list[idx] = {"model_name": "smart-router", "litellm_params": None}++ returned = router.delete_deployment(id="router-1")++ assert returned is not None+ assert "router-1" not in router.model_id_to_deployment_index_map+ assert all(entry.get("model_info", {}).get("id") != "router-1" for entry in router.model_list)+ assert router.get_deployment(model_id="router-1") is None+ assert "gpt-4o" in self._model_names(router)+def test_delete_of_adaptive_enabled_complexity_router_frees_both_registries(self):"""A complexity router with adaptive set is registered in BOTH complexity_routersand adaptive_routers under the same (model_name, tags). Releasing only the first