From 7ffe97adb7900d5c0f63f9eca29dac10a651bb31 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 17 Sep 2026 15:34:29 -0700 Subject: [PATCH] fix(utils): resolve a tripped lookup from the key's own catalog row before its region sibling --- litellm/utils.py | 17 ++++++----- .../test_register_model_custom_pricing.py | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index de2f8e20cf3..59e2e259da1 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -3052,13 +3052,16 @@ def _resolve_builtin_model_cost_entry(key: str, provider: str) -> dict[str, obje like ``bedrock/bedrock/bedrock/us.anthropic.claude-sonnet-4-6`` or region aliases). - 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``, 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. + The key itself is tried first, so a catalog row whose lookup tripped + inherits from itself rather than from a region-stripped sibling priced at + the base rate. 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``, 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]] = [] + candidates: Final[list[str]] = [key] segments: Final = key.split("/") match_provider: Final = provider or (segments[0] if len(segments) > 1 and segments[0] in LlmProvidersSet else "") idx = 0 @@ -3066,7 +3069,7 @@ def _resolve_builtin_model_cost_entry(key: str, provider: str) -> dict[str, obje idx += 1 candidates.append("/".join(segments[idx:])) - base: Final = candidates[-1] if candidates else key + base: Final = candidates[-1] for region_prefix in _BEDROCK_REGION_PREFIXES: if base.startswith(region_prefix): candidates.append(base[len(region_prefix) :]) diff --git a/tests/test_litellm/test_register_model_custom_pricing.py b/tests/test_litellm/test_register_model_custom_pricing.py index 32bcc620ab2..a7160afb046 100644 --- a/tests/test_litellm/test_register_model_custom_pricing.py +++ b/tests/test_litellm/test_register_model_custom_pricing.py @@ -841,6 +841,36 @@ def test_register_model_prices_a_geo_alias_from_its_builtin_when_the_lookup_fail _invalidate_model_cost_lowercase_map() +def test_register_model_keeps_a_catalog_rows_own_prices_when_the_lookup_fails(monkeypatch): + """A catalog row registered under its own key with no provider + (``au.anthropic.`` from a config ``model_info``) must inherit from + itself when ``get_model_info`` trips, never from its region-stripped + sibling, whose prices are the base rate rather than the geo rate. + """ + monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True") + monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url="")) + _invalidate_model_cost_lowercase_map() + geo_key: Final = "au.anthropic.claude-sonnet-5" + geo_row: Final = dict(litellm.model_cost[geo_key]) + sibling: Final = litellm.model_cost["anthropic.claude-sonnet-5"] + assert geo_row["input_cost_per_token"] != sibling["input_cost_per_token"] + + def lookup_tripped_by_a_concurrent_registration(model: str, custom_llm_provider: str | None = None) -> NoReturn: + raise RuntimeError("dictionary changed size during iteration") + + with pytest.MonkeyPatch.context() as lookup_patch: + lookup_patch.setattr(litellm.utils, "get_model_info", lookup_tripped_by_a_concurrent_registration) + litellm.register_model({geo_key: {"mode": "chat"}}, persist_across_reloads=False) + + try: + for field in ("input_cost_per_token", "output_cost_per_token", "cache_read_input_token_cost"): + assert litellm.model_cost[geo_key][field] == geo_row[field] + assert litellm.get_model_info(geo_key)[field] == geo_row[field] + assert litellm.model_cost[geo_key]["litellm_provider"] == geo_row["litellm_provider"] + finally: + _invalidate_model_cost_lowercase_map() + + def test_register_model_inherits_builtin_token_pricing_for_unmapped_key(monkeypatch): """A key shape ``get_model_info`` cannot resolve inherits the whole built-in entry, not just its cache pricing, so ``get_model_info`` on the raw key