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/ Proxy - Tag Based routing, raise correct error when no deployments found and tag filtering is on (#5745)
Ishaan Jaff1bb1f70a4781
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.pyindex c75ea4760af..adcd3f89d45 100644--- a/litellm/proxy/_types.py+++ b/litellm/proxy/_types.py@@ -10,7 +10,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Unionfrom pydantic import BaseModel, ConfigDict, Extra, Field, Json, model_validatorfrom typing_extensions import Annotated, TypedDict-from litellm.types.router import UpdateRouterConfig+from litellm.types.router import RouterErrors, UpdateRouterConfigfrom litellm.types.utils import ProviderFieldif TYPE_CHECKING:@@ -1826,6 +1826,8 @@ class ProxyException(Exception):or "No deployments available" in self.message):self.code = "429"+ elif RouterErrors.no_deployments_with_tag_routing.value in self.message:+ self.code = "401"def to_dict(self) -> dict:"""Converts the ProxyException instance to a dictionary."""diff --git a/litellm/proxy/proxy_config.yaml b/litellm/proxy/proxy_config.yamlindex 1a2dc3cf88b..9939e92a9a0 100644--- a/litellm/proxy/proxy_config.yaml+++ b/litellm/proxy/proxy_config.yaml@@ -19,9 +19,12 @@ model_list:model: openai/429api_key: fake-keyapi_base: https://exampleopenaiendpoint-production.up.railway.app+ tags: ["fake"]general_settings:master_key: sk-1234+router_settings:+ enable_tag_filtering: truediff --git a/litellm/router.py b/litellm/router.pyindex d3164620379..ec6159da42a 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -5180,6 +5180,7 @@ class Router:# check if user wants to do tag based routinghealthy_deployments = await get_deployments_for_tag( # type: ignorellm_router_instance=self,+ model=model,request_kwargs=request_kwargs,healthy_deployments=healthy_deployments,)diff --git a/litellm/router_strategy/tag_based_routing.py b/litellm/router_strategy/tag_based_routing.pyindex deda1bd77b4..9f8cd9ac5cc 100644--- a/litellm/router_strategy/tag_based_routing.py+++ b/litellm/router_strategy/tag_based_routing.py@@ -9,7 +9,7 @@ Use this to route requests between Teamsfrom typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, TypedDict, Unionfrom litellm._logging import verbose_logger-from litellm.types.router import DeploymentTypedDict+from litellm.types.router import DeploymentTypedDict, RouterErrorsif TYPE_CHECKING:from litellm.router import Router as _Router@@ -21,9 +21,15 @@ else:async def get_deployments_for_tag(llm_router_instance: LitellmRouter,+ model: str, # used to raise the correct errorhealthy_deployments: Union[List[Any], Dict[Any, Any]],request_kwargs: Optional[Dict[Any, Any]] = None,):+ """+ Returns a list of deployments that match the requested model and tags in the request.++ Executes tag based filtering based on the tags in request metadata and the tags on the deployments+ """if llm_router_instance.enable_tag_filtering is not True:return healthy_deployments@@ -80,6 +86,11 @@ async def get_deployments_for_tag()new_healthy_deployments.append(deployment)+ if len(new_healthy_deployments) == 0:+ raise ValueError(+ f"{RouterErrors.no_deployments_with_tag_routing.value}. Passed model={model} and tags={request_tags}"+ )+return new_healthy_deployments# for Untagged requests use default deployments if setdiff --git a/litellm/tests/test_router_tag_routing.py b/litellm/tests/test_router_tag_routing.pyindex f71a9b762d8..4432db5309e 100644--- a/litellm/tests/test_router_tag_routing.py+++ b/litellm/tests/test_router_tag_routing.py@@ -160,3 +160,60 @@ async def test_default_tagged_deployments():print("response_extra_info: ", response_extra_info)assert response_extra_info["model_id"] == "default-model"+++async def test_error_from_tag_routing():+ """+ Tests the correct error raised when no deployments found for tag+ """+ import logging++ from litellm._logging import verbose_logger++ verbose_logger.setLevel(logging.DEBUG)+ router = litellm.Router(+ model_list=[+ {+ "model_name": "gpt-4",+ "litellm_params": {+ "model": "gpt-4o",+ "api_base": "https://exampleopenaiendpoint-production.up.railway.app/",+ },+ "model_info": {"id": "default-model"},+ },+ {+ "model_name": "gpt-4",+ "litellm_params": {+ "model": "gpt-4o",+ "api_base": "https://exampleopenaiendpoint-production.up.railway.app/",+ },+ "model_info": {"id": "default-model-2"},+ },+ {+ "model_name": "gpt-4",+ "litellm_params": {+ "model": "gpt-4o-mini",+ "api_base": "https://exampleopenaiendpoint-production.up.railway.app/",+ "tags": ["teamA"],+ },+ "model_info": {"id": "very-expensive-model"},+ },+ ],+ enable_tag_filtering=True,+ )++ try:+ response = await router.acompletion(+ model="gpt-4",+ messages=[{"role": "user", "content": "Tell me a joke."}],+ metadata={"tags": ["paid"]},+ )++ pytest.fail("this should have failed - expected it to fail")+ except Exception as e:+ from litellm.types.router import RouterErrors++ assert RouterErrors.no_deployments_with_tag_routing.value in str(e)+ print("got expected exception = ", e)+ passdiff --git a/litellm/types/router.py b/litellm/types/router.pyindex 8c8c6a3aa64..67870c313d2 100644--- a/litellm/types/router.py+++ b/litellm/types/router.py@@ -414,6 +414,9 @@ class RouterErrors(enum.Enum):user_defined_ratelimit_error = "Deployment over user-defined ratelimit."no_deployments_available = "No deployments available for selected model"+ no_deployments_with_tag_routing = (+ "Not allowed to access model due to tags configuration"+ )class AllowedFailsPolicy(BaseModel):