diff --git a/litellm/utils.py b/litellm/utils.py index fb2cbf78523..de2f8e20cf3 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -3054,10 +3054,13 @@ def _resolve_builtin_model_cost_entry(key: str, provider: str) -> dict[str, obje Returns a copy of the matching entry so the caller can inherit its defaults without mutating the shared built-in. Returns ``None`` when no candidate - exists under a provider ``get_model_info`` would accept for ``provider``. + exists under a provider ``get_model_info`` would accept for ``provider``, or, + when the value names none, for the provider prefix the key itself carries, + which is what ``get_model_info`` derives when it reads the key back. """ candidates: Final[list[str]] = [] segments: Final = key.split("/") + match_provider: Final = provider or (segments[0] if len(segments) > 1 and segments[0] in LlmProvidersSet else "") idx = 0 while idx < len(segments) - 1 and segments[idx] in LlmProvidersSet: idx += 1 @@ -3068,14 +3071,18 @@ def _resolve_builtin_model_cost_entry(key: str, provider: str) -> dict[str, obje if base.startswith(region_prefix): candidates.append(base[len(region_prefix) :]) - if provider: - stripped: Final = _strip_model_name(model=base, custom_llm_provider=provider) + if match_provider: + stripped: Final = _strip_model_name(model=base, custom_llm_provider=match_provider) if stripped != base: candidates.append(stripped) for candidate in candidates: entry = litellm.model_cost.get(candidate) - if entry is not None and entry.get("litellm_provider") is not None and _check_provider_match(entry, provider): + if ( + entry is not None + and entry.get("litellm_provider") is not None + and _check_provider_match(entry, match_provider) + ): return dict(entry) return None diff --git a/tests/test_litellm/test_utils.py b/tests/test_litellm/test_utils.py index 94d32a99a52..431aa800e90 100644 --- a/tests/test_litellm/test_utils.py +++ b/tests/test_litellm/test_utils.py @@ -6272,3 +6272,28 @@ def test_provider_prefixed_lookup_never_outranks_an_existing_row(local_model_cos ("openrouter/openai/gpt-4o", "openrouter", "openrouter/openai/gpt-4o"), ): assert litellm.get_model_info(model=model, custom_llm_provider=provider)["key"] == expected_key + + +def test_register_model_without_a_provider_never_inherits_another_providers_entry(monkeypatch): + """ + The router registers a deployment's shared ``{provider}/{model}`` key without + ``litellm_provider``. Matching the built-in entry as a wildcard then handed + ``azure/claude-sonnet-4-6`` Anthropic's whole entry, provider included, and + reading the key back under the provider its prefix names raised "isn't mapped". + """ + from litellm.utils import _invalidate_model_cost_lowercase_map + + key: Final = "azure/claude-sonnet-4-6" + monkeypatch.setattr(litellm, "model_cost", dict(litellm.model_cost)) + litellm.model_cost.pop(key, None) + try: + litellm.register_model( + {key: {"input_cost_per_token": 1e-07, "output_cost_per_token": 4e-07}}, persist_across_reloads=False + ) + assert litellm.model_cost[key].get("litellm_provider") is None + info: Final = litellm.get_model_info(key) + assert info["litellm_provider"] == "azure" + assert info["input_cost_per_token"] == 1e-07 + assert info["output_cost_per_token"] == 4e-07 + finally: + _invalidate_model_cost_lowercase_map()