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: Model matching priority in configuration
Sameer Kankutedfece51f8c4b
diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.pyindex b2eda5a8d38..75d8253a904 100644--- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py+++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py@@ -226,12 +226,6 @@ async def chat_completion_pass_through_endpoint( # noqa: PLR0915and data["model"] in llm_router.model_group_alias): # model set in model_group_aliasllm_response = asyncio.create_task(llm_router.aadapter_completion(**data))- elif (- llm_router is not None and data["model"] in llm_router.deployment_names- ): # model in router deployments, calling a specific deployment on the router- llm_response = asyncio.create_task(- llm_router.aadapter_completion(**data, specific_deployment=True)- )elif llm_router is not None and llm_router.has_model_id(data["model"]): # model in router model list@@ -239,9 +233,15 @@ async def chat_completion_pass_through_endpoint( # noqa: PLR0915elif (llm_router is not Noneand data["model"] not in router_model_names- and llm_router.default_deployment is not None- ): # model in router deployments, calling a specific deployment on the router+ and (llm_router.default_deployment is not None or len(llm_router.pattern_router.patterns) > 0)+ ): # check for wildcard routes or default deployment before checking deployment_namesllm_response = asyncio.create_task(llm_router.aadapter_completion(**data))+ elif (+ llm_router is not None and data["model"] in llm_router.deployment_names+ ): # model in router deployments, calling a specific deployment on the router (lowest priority)+ llm_response = asyncio.create_task(+ llm_router.aadapter_completion(**data, specific_deployment=True)+ )elif user_model is not None: # `litellm --model <your-model-name>`llm_response = asyncio.create_task(litellm.aadapter_completion(**data))else:diff --git a/litellm/proxy/route_llm_request.py b/litellm/proxy/route_llm_request.pyindex 4b3a65737d4..0e061a839d9 100644--- a/litellm/proxy/route_llm_request.py+++ b/litellm/proxy/route_llm_request.py@@ -260,12 +260,9 @@ async def route_request():return getattr(llm_router, f"{route_type}")(**data)- elif data["model"] in llm_router.deployment_names:- return getattr(llm_router, f"{route_type}")(- **data, specific_deployment=True- )-elif data["model"] not in router_model_names:+ # Check wildcards before checking deployment_names+ # Priority: 1. Exact model_name match, 2. Wildcard match, 3. deployment_names matchif llm_router.router_general_settings.pass_through_all_models:return getattr(litellm, f"{route_type}")(**data)elif (@@ -273,6 +270,11 @@ async def route_request(or len(llm_router.pattern_router.patterns) > 0):return getattr(llm_router, f"{route_type}")(**data)+ elif data["model"] in llm_router.deployment_names:+ # Only match deployment_names if no wildcard matched+ return getattr(llm_router, f"{route_type}")(+ **data, specific_deployment=True+ )elif route_type in ["amoderation","aget_responses",diff --git a/tests/local_testing/test_router_pattern_matching.py b/tests/local_testing/test_router_pattern_matching.pyindex 5695034c7a5..7f38d14c480 100644--- a/tests/local_testing/test_router_pattern_matching.py+++ b/tests/local_testing/test_router_pattern_matching.py@@ -314,3 +314,79 @@ def test_calculate_pattern_specificity():assert PatternUtils.calculate_pattern_specificity("llmengine/*") == (11, 1)assert PatternUtils.calculate_pattern_specificity("*") == (1, 1)+++def test_wildcard_priority_over_deployment_names():+ """+ Test that wildcard routes take priority over deployment_names (litellm_params.model) matching.++ Scenario:+ - deployment 1: model_name="zapier-multi-provider-text-embedding-3-small", model="openai/text-embedding-3-small"+ - deployment 2: model_name="*", model="openai/*"+ - deployment 3: model_name="openai/*", model="openai/*"++ When calling "openai/text-embedding-3-small", it should match deployment 3 (wildcard),+ NOT deployment 1 (even though deployment 1's litellm_params.model matches).++ Priority order should be:+ 1. Exact model_name match+ 2. Wildcard model_name match+ 3. deployment_names (litellm_params.model) match+ """+ router = Router(+ model_list=[+ {+ "model_name": "zapier-multi-provider-text-embedding-3-small",+ "litellm_params": {+ "model": "openai/text-embedding-3-small",+ "api_base": "http://localhost:8080/openai",+ "api_key": "test-key-1"+ },+ "model_info": {+ "id": "zapier-multi-provider-text-embedding-3-small-openai"+ }+ },+ {+ "model_name": "*",+ "litellm_params": {+ "model": "openai/*",+ "api_base": "http://localhost:8081/openai",+ "api_key": "test-key-2"+ }+ },+ {+ "model_name": "openai/*",+ "litellm_params": {+ "model": "openai/*",+ "api_base": "http://localhost:8082/openai",+ "api_key": "test-key-3"+ }+ }+ ]+ )++ # Test 1: Request "openai/text-embedding-3-small" should match wildcard "openai/*", not deployment_names+ deployments = router.get_model_list(model_name="openai/text-embedding-3-small")++ assert deployments is not None, "No deployments found"+ assert len(deployments) == 1, f"Expected 1 deployment, got {len(deployments)}"++ # Should match the "openai/*" wildcard deployment (api_base ending in 8082)+ assert deployments[0]['litellm_params']['api_base'] == "http://localhost:8082/openai", \+ f"Expected wildcard deployment (8082), got {deployments[0]['litellm_params']['api_base']}"++ # Test 2: Request exact model_name should still work+ deployments = router.get_model_list(model_name="zapier-multi-provider-text-embedding-3-small")++ assert deployments is not None, "No deployments found"+ assert len(deployments) == 1, f"Expected 1 deployment, got {len(deployments)}"+ assert deployments[0]['litellm_params']['api_base'] == "http://localhost:8080/openai", \+ f"Expected exact match deployment (8080), got {deployments[0]['litellm_params']['api_base']}"++ # Test 3: Request with "*" wildcard should match the "*" deployment+ deployments = router.get_model_list(model_name="some-random-model")++ assert deployments is not None, "No deployments found"+ assert len(deployments) == 1, f"Expected 1 deployment, got {len(deployments)}"+ assert deployments[0]['litellm_params']['api_base'] == "http://localhost:8081/openai", \+ f"Expected '*' wildcard deployment (8081), got {deployments[0]['litellm_params']['api_base']}"