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(main): an explicit provider outranks a known OpenAI model name (#36800)

Fahima Mokhtarie1ef7775bdd9
diff --git a/litellm/main.py b/litellm/main.py
index 16eff5a0f3e..04ae410db6f 100644
--- a/litellm/main.py
+++ b/litellm/main.py
@@ -5616,7 +5616,12 @@ def completion(
elif custom_llm_provider == "hosted_vllm":
response = _complete_hosted_vllm(_dispatch_ctx)
elif (
- model in litellm.open_ai_chat_completion_models
+ # A known OpenAI model name only decides the route when nothing else
+ # resolved a provider. get_llm_provider() already maps these names to
+ # "openai", so a different value here was asked for explicitly (or came
+ # from a register_model entry), and the provider config built for it
+ # would be handed to the OpenAI handler.
+ (model in litellm.open_ai_chat_completion_models and custom_llm_provider in (None, "openai"))
or custom_llm_provider == "custom_openai"
or custom_llm_provider == "deepinfra"
or custom_llm_provider == "perplexity"
diff --git a/tests/test_litellm/test_main.py b/tests/test_litellm/test_main.py
index 9e160370048..58373df024c 100644
--- a/tests/test_litellm/test_main.py
+++ b/tests/test_litellm/test_main.py
@@ -1,3 +1,5 @@
+import contextlib
+import copy
import json
import os
import sys
@@ -2461,3 +2463,104 @@ async def test_acompletion_forwards_aws_credentials_through_responses_bridge(
finally:
litellm.disable_aiohttp_transport = original_disable_aiohttp
litellm.in_memory_llm_clients_cache.flush_cache()
+
+
+_GEMINI_RESPONSE_BODY = {
+ "candidates": [{"content": {"parts": [{"text": "hello"}], "role": "model"}, "finishReason": "STOP"}],
+ "usageMetadata": {"promptTokenCount": 2, "candidatesTokenCount": 1, "totalTokenCount": 3},
+}
+
+
+def _gemini_client_returning_a_reply():
+ """An injected HTTP client whose post() answers like generativelanguage does."""
+ from litellm.llms.custom_httpx.http_handler import HTTPHandler
+
+ client = HTTPHandler()
+ request = httpx.Request("POST", "https://generativelanguage.googleapis.com/")
+ post = MagicMock(return_value=httpx.Response(200, json=_GEMINI_RESPONSE_BODY, request=request))
+ return client, post
+
+
+def restore_model_registry():
+ """litellm.model_cost and the provider name sets are module-global.
+
+ register_model merges into the existing entry in place, hence the deep copy.
+ """
+ model_cost = copy.deepcopy(litellm.model_cost)
+ openai_models = set(litellm.open_ai_chat_completion_models)
+ yield
+ litellm.model_cost.clear()
+ litellm.model_cost.update(model_cost)
+ litellm.open_ai_chat_completion_models.clear()
+ litellm.open_ai_chat_completion_models.update(openai_models)
+
+
+def test_openai_model_name_does_not_outrank_explicit_provider():
+ """`gemini/gpt-4o` goes to Google, not to litellm's OpenAI handler.
+
+ completion() checks `model in litellm.open_ai_chat_completion_models` ahead of
+ the gemini branch, so the call used to reach the OpenAI handler carrying
+ VertexGeminiConfig, whose transform_request raises NotImplementedError.
+ """
+ assert "gpt-4o" in litellm.open_ai_chat_completion_models
+ client, post = _gemini_client_returning_a_reply()
+
+ with patch.object(client, "post", new=post):
+ response = litellm.completion(
+ model="gemini/gpt-4o",
+ messages=[{"role": "user", "content": "hello"}],
+ api_key="test-api-key",
+ client=client,
+ )
+
+ assert "generativelanguage.googleapis.com" in post.call_args.kwargs["url"]
+ assert "models/gpt-4o" in post.call_args.kwargs["url"]
+ assert response.choices[0].message.content == "hello"
+
+
+def test_mislabelled_pricing_entry_does_not_reroute_provider(restore_model_registry):
+ """register_model is the other way into the same failure.
+
+ An entry claiming litellm_provider "openai" adds its name to
+ open_ai_chat_completion_models, so one mislabelled price reroutes every later
+ call to that model in the process.
+ """
+ litellm.register_model(
+ {
+ "gemini-2.5-pro": {
+ "litellm_provider": "openai",
+ "mode": "chat",
+ "input_cost_per_token": 1e-06,
+ "output_cost_per_token": 4e-06,
+ }
+ }
+ )
+ assert "gemini-2.5-pro" in litellm.open_ai_chat_completion_models
+ client, post = _gemini_client_returning_a_reply()
+
+ with patch.object(client, "post", new=post):
+ response = litellm.completion(
+ model="gemini/gemini-2.5-pro",
+ messages=[{"role": "user", "content": "hello"}],
+ api_key="test-api-key",
+ client=client,
+ )
+
+ assert "generativelanguage.googleapis.com" in post.call_args.kwargs["url"]
+ assert response.choices[0].message.content == "hello"
+
+
+def test_openai_model_without_a_provider_still_routes_to_openai():
+ from openai import OpenAI
+
+ client = OpenAI(api_key="fake-key")
+ raw_response = client.chat.completions.with_raw_response
+ with patch.object(raw_response, "create") as mock_create, contextlib.suppress(Exception):
+ litellm.completion(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hello"}],
+ client=client,
+ )
+
+ mock_create.assert_called()