Skip to content

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]

fix: address CI violations for adaptive router

Krrish Dholakia24a2e3e89e06
diff --git a/litellm/router.py b/litellm/router.py
index 33736fbfff5..d547fb706fd 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -7000,10 +7000,13 @@ class Router:
model_to_prefs: Dict[str, AdaptiveRouterPreferences] = {}
model_to_cost: Dict[str, float] = {}
- for d in self.model_list or []:
- name = d.get("model_name") if isinstance(d, dict) else d.model_name
- if name not in config.available_models:
+ # O(k) via the name→indices map: only touch deployments whose name
+ # is listed in `available_models`, instead of scanning model_list.
+ for name in config.available_models:
+ indices = self.model_name_to_deployment_indices.get(name, [])
+ if not indices:
continue
+ d = (self.model_list or [])[indices[0]]
mi = d.get("model_info") if isinstance(d, dict) else d.model_info
mi_dict: Dict[str, Any] = (
mi if isinstance(mi, dict) else (mi.model_dump() if mi else {})
@@ -7034,7 +7037,7 @@ class Router:
model_to_cost=model_to_cost,
)
self.adaptive_routers[deployment.model_name] = adaptive_router
- litellm.callbacks.append(
+ litellm.logging_callback_manager.add_litellm_callback(
AdaptiveRouterPostCallHook(adaptive_router=adaptive_router)
)
verbose_router_logger.info(
@@ -7137,7 +7140,7 @@ class Router:
# by _create_deployment -> _add_model_to_list_and_index_map
# Deferred: build the AdaptiveRouter strategy now that all underlying
- # deployments are visible in self.model_list.
+ # deployments have been registered.
self._finalize_adaptive_router_if_configured()
def _add_deployment(self, deployment: Deployment) -> Deployment:
diff --git a/tests/router_unit_tests/test_router_index_management.py b/tests/router_unit_tests/test_router_index_management.py
index 2694c62827c..47946bbe30a 100644
--- a/tests/router_unit_tests/test_router_index_management.py
+++ b/tests/router_unit_tests/test_router_index_management.py
@@ -229,6 +229,7 @@ class TestRouterIndexManagement:
# Methods that are allowed to iterate through self.model_list
ALLOWED_METHODS = [
"_get_deployment_by_litellm_model", # Edge case: lookup by litellm_params.model (not indexed)
+ "_finalize_adaptive_router_if_configured", # Init-time prefix scan for "auto_router/adaptive_router" (no index for prefix match)
]
# Get path to router.py