mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
fix(cost_calculator): strip duplicated region segment from alias cost keys
This commit is contained in:
parent
6d1a7ff8a8
commit
951cef1e98
2 changed files with 30 additions and 5 deletions
|
|
@ -810,11 +810,8 @@ def _strip_unregistered_leading_segments(model: str, region_name: str | None) ->
|
|||
head_len: Final = 2 if region_name is not None and len(segments) > 2 and segments[1] == region_name else 1
|
||||
head: Final = "/".join(segments[:head_len])
|
||||
tail: Final = segments[head_len:]
|
||||
strippable: Final = next(
|
||||
(index for index, segment in enumerate(tail) if segment in LlmProvidersSet or segment == region_name),
|
||||
len(tail),
|
||||
)
|
||||
candidates: Final = tuple(f"{head}/{'/'.join(tail[start:])}" for start in range(min(strippable, len(tail) - 1) + 1))
|
||||
strippable: Final = next((index for index, segment in enumerate(tail) if segment in LlmProvidersSet), len(tail))
|
||||
candidates: Final = (f"{head}/{'/'.join(tail[start:])}" for start in range(min(strippable, len(tail) - 1) + 1))
|
||||
return next((candidate for candidate in candidates if candidate in litellm.model_cost), model)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3942,6 +3942,34 @@ def test_select_model_name_strips_unregistered_alias_prefix(_local_model_cost_ma
|
|||
assert selected == "vertex_ai/claude-opus-5"
|
||||
|
||||
|
||||
def test_select_model_name_strips_duplicated_region_segment(_local_model_cost_map):
|
||||
"""A "region/model" alias whose leading segment repeats the request's region must
|
||||
resolve to the region-priced cost key instead of keeping the region segment twice."""
|
||||
|
||||
from litellm.cost_calculator import _select_model_name_for_cost_calc
|
||||
|
||||
response = litellm.ModelResponse(
|
||||
id="x",
|
||||
choices=[
|
||||
{
|
||||
"index": 0,
|
||||
"message": {"role": "assistant", "content": "hi"},
|
||||
"finish_reason": "stop",
|
||||
}
|
||||
],
|
||||
model="us-east-1/anthropic.claude-v2:1",
|
||||
)
|
||||
response._hidden_params = {"region_name": "us-east-1"}
|
||||
|
||||
selected = _select_model_name_for_cost_calc(
|
||||
model=None,
|
||||
completion_response=response,
|
||||
custom_llm_provider="bedrock",
|
||||
)
|
||||
|
||||
assert selected == "bedrock/us-east-1/anthropic.claude-v2:1"
|
||||
|
||||
|
||||
def test_completion_cost_nonzero_for_slash_alias_model_name(_local_model_cost_map):
|
||||
"""End-to-end cost through a "/"-containing alias must price above zero (#38069)."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue