litellm/tests/test_litellm/llms/gigachat/test_utils.py
mateo-berri 91595780ec fix(gigachat): fold cached tokens back into prompt and total token counts
GigaChat reports prompt_tokens and total_tokens after subtracting cached
tokens (the docs example is prompt_tokens=1, precached_prompt_tokens=37,
total_tokens=5, so the fields are disjoint, not a subset). Map to the
OpenAI convention by adding precached_prompt_tokens back onto prompt and
total while still surfacing it as prompt_tokens_details.cached_tokens.
2026-08-31 15:16:44 -07:00

79 lines
No EOL
2.4 KiB
Python

"""
Tests for litellm.llms.gigachat.utils
"""
import pytest
from litellm.llms.gigachat.utils import convert_usage
from litellm.types.utils import PromptTokensDetailsWrapper, Usage
class TestConvertUsage:
def test_basic_usage_without_precached(self):
"""Test convert_usage with standard tokens, no precached prompt tokens."""
result = convert_usage(
{
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15,
}
)
assert result == Usage(
prompt_tokens=10,
completion_tokens=5,
total_tokens=15,
prompt_tokens_details=None,
)
def test_usage_with_precached_prompt_tokens(self):
"""GigaChat's prompt_tokens and total_tokens exclude cached tokens (docs example:
prompt_tokens=1, precached_prompt_tokens=37, total_tokens=5), so OpenAI-convention
usage adds precached back in and surfaces it as cached_tokens."""
result = convert_usage(
{
"prompt_tokens": 10,
"completion_tokens": 5,
"precached_prompt_tokens": 3,
"total_tokens": 15,
}
)
assert result == Usage(
prompt_tokens=13,
completion_tokens=5,
total_tokens=18,
prompt_tokens_details=PromptTokensDetailsWrapper(cached_tokens=3),
)
def test_zero_precached_prompt_tokens(self):
"""Test convert_usage with zero precached_prompt_tokens does not create details wrapper."""
result = convert_usage(
{
"prompt_tokens": 10,
"completion_tokens": 5,
"precached_prompt_tokens": 0,
"total_tokens": 15,
}
)
assert result == Usage(
prompt_tokens=10,
completion_tokens=5,
total_tokens=15,
prompt_tokens_details=None,
)
def test_missing_optional_fields(self):
"""Test convert_usage with missing optional fields defaults to zero."""
result = convert_usage(
{
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15,
}
)
assert result.prompt_tokens == 10
assert result.completion_tokens == 5
assert result.total_tokens == 15
assert result.prompt_tokens_details is None