mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(utils): resolve a tripped lookup from the key's own catalog row before its region sibling
This commit is contained in:
parent
7dbd7c093d
commit
7ffe97adb7
2 changed files with 40 additions and 7 deletions
|
|
@ -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) :])
|
||||
|
|
|
|||
|
|
@ -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.<model>`` 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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue