mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-06 07:48:06 +00:00
fix(cost_calculator): return (0.0, 0.0) for unknown openai model instead of crashing
When cost_per_token() is called with custom_llm_provider='openai' and an unrecognized model name, openai_cost_per_token() raises an Exception instead of gracefully returning (0.0, 0.0). Wraps the openai_cost_per_token() call in a try/except so unknown models fall back to (0.0, 0.0), consistent with how other branches handle this. Adds a regression test to confirm the behavior. Ref: https://github.com/BerriAI/litellm/pull/27587
This commit is contained in:
parent
f6b9518ddb
commit
ed43c74bfa
2 changed files with 32 additions and 6 deletions
|
|
@ -599,12 +599,19 @@ def cost_per_token(
|
|||
elif custom_llm_provider == "bedrock":
|
||||
return bedrock_cost_per_token(model=model, usage=usage_block, service_tier=service_tier)
|
||||
elif custom_llm_provider == "openai":
|
||||
return openai_cost_per_token(
|
||||
model=model,
|
||||
usage=usage_block,
|
||||
service_tier=service_tier,
|
||||
data_residency=data_residency,
|
||||
)
|
||||
try:
|
||||
return openai_cost_per_token(
|
||||
model=model,
|
||||
usage=usage_block,
|
||||
service_tier=service_tier,
|
||||
data_residency=data_residency,
|
||||
)
|
||||
except Exception:
|
||||
verbose_logger.debug(
|
||||
"cost_per_token: unknown model=%s, custom_llm_provider=openai. Returning 0.0, 0.0",
|
||||
model,
|
||||
)
|
||||
return 0.0, 0.0
|
||||
elif custom_llm_provider == "databricks":
|
||||
return databricks_cost_per_token(model=model, usage=usage_block)
|
||||
elif custom_llm_provider == "fireworks_ai":
|
||||
|
|
|
|||
|
|
@ -3530,3 +3530,22 @@ def test_completion_cost_prices_anthropic_shaped_cache_read_tokens():
|
|||
)
|
||||
|
||||
assert cost == pytest.approx(3 * 5e-6 + 4014 * 5e-7 + 5 * 3e-5, rel=1e-9)
|
||||
def test_cost_per_token_unknown_model_with_explicit_provider():
|
||||
"""
|
||||
Test: cost_per_token() with a known provider but unknown model.
|
||||
This is the second guard point flagged in PR #27587 review.
|
||||
A fake model name with explicit custom_llm_provider="openai"
|
||||
should not crash — it should return (0.0, 0.0).
|
||||
|
||||
Ref: https://github.com/BerriAI/litellm/pull/27587
|
||||
"""
|
||||
import litellm
|
||||
from litellm import cost_per_token
|
||||
|
||||
result = cost_per_token(
|
||||
model="fake-model-xyz-123",
|
||||
custom_llm_provider="openai",
|
||||
prompt_tokens=10,
|
||||
completion_tokens=5,
|
||||
)
|
||||
assert result == (0.0, 0.0), f"Expected (0.0, 0.0) for unknown model, got {result}"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue