Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/crewai/src/crewai/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,22 @@ def _resolve_route(cls, model: str, kwargs: dict[str, Any]) -> _Route:
elif explicit_provider:
provider = explicit_provider
use_native = True
model_string = model
# An explicit provider only pins the routing; a model string that
# already carries the same provider's prefix must not keep it,
# otherwise the prefixed name is sent to the API verbatim. Both
# sides are canonicalized so alias spellings ("claude", "google",
# "aws", "azure_openai") match their canonical prefixes too.
prefix, separator, model_part = model.partition("/")
canonical_prefix = PROVIDER_ALIASES.get(prefix.lower(), prefix.lower())
canonical_provider = PROVIDER_ALIASES.get(
provider.lower(), provider.lower()
)
if separator and model_part and canonical_prefix == canonical_provider:
model_string = model_part
else:
model_string = model
if provider == "deepinfra":
cls._require_deepinfra_model(model)
cls._require_deepinfra_model(model_string)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
elif "/" in model:
prefix, _, model_part = model.partition("/")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,20 @@ def test_llm_creates_openai_compatible_for_deepinfra_explicit_provider(self):
assert llm.provider == "deepinfra"
assert llm.model == "deepseek-ai/DeepSeek-V4-Flash-0731"

def test_llm_explicit_deepinfra_provider_strips_matching_prefix(self):
"""A redundant deepinfra/ prefix on the model is stripped, not sent verbatim."""
with patch.dict(os.environ, {"DEEPINFRA_API_KEY": "test-key"}):
llm = LLM(
model="deepinfra/deepseek-ai/DeepSeek-V4-Flash-0731",
provider="deepinfra",
)
assert isinstance(llm, OpenAICompatibleCompletion)
assert llm.provider == "deepinfra"
assert llm.model == "deepseek-ai/DeepSeek-V4-Flash-0731"

@pytest.mark.parametrize(
"model",
[
"deepinfra/deepseek-ai/DeepSeek-V4-Flash-0731",
"DeepSeek-V4-Flash-0731",
"deepseek-ai/DeepSeek-V4-Flash-0731/",
],
Expand Down
67 changes: 67 additions & 0 deletions lib/crewai/tests/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,73 @@ def test_explicit_provider_kwarg_takes_priority():
assert llm2.provider == "openai"


def test_explicit_provider_strips_matching_model_prefix():
"""provider= only pins routing; a model prefix naming the same provider is
stripped instead of being sent to the API verbatim."""
assert LLM._resolve_route("openai/gpt-4o", {"provider": "openai"}).model == "gpt-4o"
assert (
LLM._resolve_route("claude/claude-haiku-4-5", {"provider": "anthropic"}).model
== "claude-haiku-4-5"
)
assert (
LLM._resolve_route(
"deepinfra/meta-llama/Llama-3-8b", {"provider": "deepinfra"}
).model
== "meta-llama/Llama-3-8b"
)

# An unrelated prefix (e.g. a namespaced model id) is left untouched.
assert LLM._resolve_route("custom/gpt-4o", {"provider": "openai"}).model == "custom/gpt-4o"

# A malformed DeepInfra id still fails validation on the stripped part.
with pytest.raises(ValueError, match="org/model"):
LLM._resolve_route("deepinfra/llama", {"provider": "deepinfra"})

# kwargs are read, never mutated.
kwargs = {"provider": "openai"}
LLM._resolve_route("openai/gpt-4o", kwargs)
assert kwargs == {"provider": "openai"}


def test_explicit_alias_provider_strips_canonical_prefix():
"""An alias provider must match its canonical prefix, in both spellings.

``provider="claude"`` is a supported native provider, so a
``claude/<model>`` string has to be stripped just like
``provider="anthropic"`` does; otherwise the alias path keeps the prefix
and the API receives a model name it rejects.
"""
assert (
LLM._resolve_route("claude/claude-haiku-4-5", {"provider": "claude"}).model
== "claude-haiku-4-5"
)
assert (
LLM._resolve_route(
"anthropic/claude-haiku-4-5", {"provider": "claude"}
).model
== "claude-haiku-4-5"
)
assert (
LLM._resolve_route("google/gemini-2.5-pro", {"provider": "google"}).model
== "gemini-2.5-pro"
)
assert LLM._resolve_route("aws/titan-text", {"provider": "aws"}).model == "titan-text"
assert (
LLM._resolve_route("azure/gpt-4o", {"provider": "azure_openai"}).model
== "gpt-4o"
)
assert (
LLM._resolve_route("gemini/gemini-2.5-pro", {"provider": "google"}).model
== "gemini-2.5-pro"
)

# A prefix naming a different provider is still left untouched.
assert (
LLM._resolve_route("openai/gpt-4o", {"provider": "claude"}).model
== "openai/gpt-4o"
)


def test_resolve_route_decides_what_new_constructs():
"""`LLM._resolve_route` is `__new__`'s routing, callable without building anything."""
from crewai.llms.providers.anthropic.completion import AnthropicCompletion
Expand Down
Loading