mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-04 02:31:27 +00:00
fix(cost): coerce None/string token counts in cost_per_token
This commit is contained in:
parent
9a715df212
commit
45be10a8e4
2 changed files with 31 additions and 0 deletions
|
|
@ -310,6 +310,17 @@ def _transcription_usage_has_token_details(
|
|||
return (prompt_tokens_val > 0) or (completion_tokens_val > 0)
|
||||
|
||||
|
||||
def _coerce_token_count(value: float | str | None) -> int:
|
||||
if value is None:
|
||||
return 0
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
return int(value)
|
||||
except ValueError:
|
||||
return 0
|
||||
return int(value)
|
||||
|
||||
|
||||
def cost_per_token(
|
||||
model: str = "",
|
||||
prompt_tokens: int = 0,
|
||||
|
|
@ -367,6 +378,9 @@ def cost_per_token(
|
|||
if model is None:
|
||||
raise Exception("Invalid arg. Model cannot be none.")
|
||||
|
||||
prompt_tokens = _coerce_token_count(prompt_tokens)
|
||||
completion_tokens = _coerce_token_count(completion_tokens)
|
||||
|
||||
## RECONSTRUCT USAGE BLOCK ##
|
||||
if usage_object is not None:
|
||||
usage_block = usage_object
|
||||
|
|
|
|||
|
|
@ -4768,3 +4768,20 @@ def test_collect_and_combine_realtime_usage_stores_partitioned_text_tokens() ->
|
|||
assert combined.completion_tokens_details.reasoning_tokens == 95
|
||||
assert combined.completion_tokens_details.text_tokens == 38
|
||||
assert combined.completion_tokens_details.audio_tokens == 0
|
||||
def test_cost_per_token_none_token_counts_cost_zero():
|
||||
prompt_cost, completion_cost = cost_per_token(
|
||||
model="gpt-4o-mini", prompt_tokens=None, completion_tokens=None
|
||||
)
|
||||
assert prompt_cost == 0.0
|
||||
assert completion_cost == 0.0
|
||||
|
||||
|
||||
def test_cost_per_token_string_token_counts_coerced():
|
||||
prompt_cost, completion_cost = cost_per_token(
|
||||
model="gpt-4o-mini", prompt_tokens="10", completion_tokens=5
|
||||
)
|
||||
expected_prompt, expected_completion = cost_per_token(
|
||||
model="gpt-4o-mini", prompt_tokens=10, completion_tokens=5
|
||||
)
|
||||
assert prompt_cost == expected_prompt
|
||||
assert completion_cost == expected_completion
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue