Repository navigation
fix(sdk): tolerate missing cache fields in token usage buckets #5169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,6 +195,28 @@ def test_record_usage_with_cache_write(self, basic_telemetry): | |
| token_usage = basic_telemetry.metrics.token_usages[0] | ||
| assert token_usage.cache_write_tokens == 30 | ||
|
|
||
| def test_record_usage_with_real_details_no_cache_fields(self, basic_telemetry): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test does not fail on the pre-fix code in the environment CI installs. With the old A shape that does raise on the old code at 1.93.0 is the explicit null, which keeps the name in usage = Usage(
prompt_tokens=100,
completion_tokens=50,
total_tokens=150,
prompt_tokens_details={"cached_tokens": 25, "cache_creation_tokens": None},
)Covering that (or the equivalent raw |
||
| """Providers without prompt caching (e.g. MiniMax) return a real | ||
| PromptTokensDetailsWrapper with the cache attributes deleted. | ||
|
|
||
| Regression test: _record_usage must not raise AttributeError here, | ||
| and must still count cache reads. | ||
| """ | ||
| from litellm.types.utils import PromptTokensDetailsWrapper | ||
|
|
||
| usage = Usage( | ||
| prompt_tokens=100, | ||
| completion_tokens=50, | ||
| total_tokens=150, | ||
| prompt_tokens_details=PromptTokensDetailsWrapper(cached_tokens=25), | ||
| ) | ||
|
|
||
| basic_telemetry._record_usage(usage, "test-id", 4096) | ||
|
|
||
| token_usage = basic_telemetry.metrics.token_usages[0] | ||
| assert token_usage.cache_read_tokens == 25 | ||
| assert token_usage.cache_write_tokens == 0 | ||
|
|
||
| def test_record_usage_missing_tokens(self, basic_telemetry): | ||
| """Test token usage recording with missing token counts.""" | ||
| usage = Usage() # Empty usage | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking: these
getattrcalls fail the repository's ownForbid dynamic attribute access in SDKpre-commit hook (scripts/check_forbidden_dynamic_attributes.pyexits 1 with violations at 262 x2 and 265), and the committed baseline may only shrink, so this cannot be allowed through.uv run ruff format --checkalso reports this file needs reformatting.model_dump()achieves the same tolerance without dynamic attribute access — deleted optional fields are simply absent from the dump:Note this would also require replacing the
MagicMockintest_record_usage_with_cache_read, since a mock'smodel_dump()returns another mock.