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]

Merge branch 'litellm_internal_staging' into litellm_fix_autorouter_untagged_hijack

mateo9f1129ae193e
diff --cc litellm/router.py
index 1eb2995f558,1f114f99118..e3b541566e2
--- a/litellm/router.py
+++ b/litellm/router.py
@@@ -11352,20 -11352,15 +11365,24 @@@ class Router
return filtered
+ def _model_name_has_plain_deployments(self, model: str) -> bool:
+ indices: Final = self.model_name_to_deployment_indices.get(model) or ()
+ return any(not self._is_strategy_marker_deployment(self.model_list[idx]) for idx in indices)
+
- def _select_pre_routing_strategy(self, model: str, request_kwargs: dict) -> "PreRoutingStrategy | None":
+ def _select_pre_routing_strategy(
+ self, model: str, request_kwargs: dict
+ ) -> "TaggedPreRoutingStrategy[PreRoutingStrategy] | None":
"""
Resolve the pre-routing strategy for `model`, disambiguating deployments
that share a `model_name` by matching the request's tags against each
registered strategy's tags before falling back to the first registered.
+ Returns the tagged registry entry so the caller can tell whether the
+ request's tags were what selected it.
+
+ With tag filtering enabled, strategies that all carry real tags matching
+ none of the request's do not capture it when the name also has plain
+ deployments: returning None hands the request to ordinary tag-aware
+ deployment selection.
"""
candidates: Final[list[TaggedPreRoutingStrategy[PreRoutingStrategy]]] = [
*self.auto_routers.get(model, []),
@@@ -11382,17 -11379,11 +11399,17 @@@
if tagged.tags and is_valid_deployment_tag(
list(tagged.tags), request_tags, self.tag_filtering_match_any
):
- return tagged.strategy
+ return tagged
for tagged in candidates:
if "default" in tagged.tags:
- return tagged.strategy
+ return tagged
+ if (
+ self.enable_tag_filtering
+ and all(tagged.tags for tagged in candidates)
+ and self._model_name_has_plain_deployments(model)
+ ):
+ return None
- return candidates[0].strategy
+ return candidates[0]
async def async_pre_routing_hook(
self,
diff --cc tests/test_litellm/router_strategy/test_complexity_router.py
index 8abe80ca0d7,7fc78aee619..9ba07056808
--- a/tests/test_litellm/router_strategy/test_complexity_router.py
+++ b/tests/test_litellm/router_strategy/test_complexity_router.py
@@@ -1174,43 -1174,8 +1174,43 @@@ class TestPreRoutingStrategyRegistry
TaggedPreRoutingStrategy(tags=("us",), strategy=us),
]
}
- assert router._select_pre_routing_strategy("smart", {}) is cn
+ assert router._select_pre_routing_strategy("smart", {}).strategy is cn
+ @staticmethod
+ def _router_with_plain_smart_deployment(enable_tag_filtering: bool) -> Router:
+ return Router(
+ model_list=[{"model_name": "smart", "litellm_params": {"model": "openai/gpt-4o-mini"}}],
+ enable_tag_filtering=enable_tag_filtering,
+ )
+
+ def test_select_falls_through_to_plain_deployments_when_no_tag_matches_under_tag_filtering(self):
+ router = self._router_with_plain_smart_deployment(enable_tag_filtering=True)
+ cn, us = object(), object()
+
+ router.complexity_routers = {"smart": [TaggedPreRoutingStrategy(tags=("cn",), strategy=cn)]}
+ assert router._select_pre_routing_strategy("smart", {}) is None
- assert router._select_pre_routing_strategy("smart", {"metadata": {"tags": ["cn"]}}) is cn
++ assert router._select_pre_routing_strategy("smart", {"metadata": {"tags": ["cn"]}}).strategy is cn
+
+ router.complexity_routers = {
+ "smart": [
+ TaggedPreRoutingStrategy(tags=("cn",), strategy=cn),
+ TaggedPreRoutingStrategy(tags=("us",), strategy=us),
+ ]
+ }
+ assert router._select_pre_routing_strategy("smart", {}) is None
+ assert router._select_pre_routing_strategy("smart", {"metadata": {"tags": ["row"]}}) is None
- assert router._select_pre_routing_strategy("smart", {"metadata": {"tags": ["us"]}}) is us
++ assert router._select_pre_routing_strategy("smart", {"metadata": {"tags": ["us"]}}).strategy is us
+
+ router.complexity_routers["router-only"] = [TaggedPreRoutingStrategy(tags=("cn",), strategy=cn)]
- assert router._select_pre_routing_strategy("router-only", {}) is cn
++ assert router._select_pre_routing_strategy("router-only", {}).strategy is cn
+
+ def test_select_keeps_capturing_when_tag_filtering_is_disabled(self):
+ router = self._router_with_plain_smart_deployment(enable_tag_filtering=False)
+ cn = object()
+
+ router.complexity_routers = {"smart": [TaggedPreRoutingStrategy(tags=("cn",), strategy=cn)]}
- assert router._select_pre_routing_strategy("smart", {}) is cn
++ assert router._select_pre_routing_strategy("smart", {}).strategy is cn
+
class TestAsyncPreRoutingHookMultiFormat:
"""Test async_pre_routing_hook with multiple input formats."""