Repository navigation
Conversation
…e chunk lacks it _calculate_usage_per_chunk overwrote the accumulated prompt_tokens_details on every usage-bearing chunk, so a final usage chunk without the breakdown reset it to None and the cached_tokens split was lost from the combined response. The neighboring completion_tokens_details accumulator already replaces only on non-None; this applies the same guard. The cache-creation TTL rescue keeps working since a non-None later wrapper still replaces the accumulated one
Greptile SummaryThis PR fixes a silent data-loss bug in
Confidence Score: 5/5Safe to merge — the change is a targeted, additive fix that aligns The fix is minimal and consistent with the surrounding code pattern. The new helper is stateless, the logic is trivially correct, and the regression test exercises the exact failing scenario end-to-end without touching any network or shared state. Files Needing Attention: No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/streaming_chunk_builder_utils.py | Adds _capture_prompt_tokens_details helper that keeps the accumulated value when an incoming chunk carries no prompt_tokens_details, matching the existing non-None guard pattern used by completion_tokens_details and cache_creation_token_details. |
| tests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_utils.py | New mock-only test test_stream_chunk_builder_preserves_prompt_tokens_details_from_earlier_chunk directly reproduces the reported bug; no network calls, consistent with test folder conventions. |
Reviews (3): Last reviewed commit: "style(streaming): use PEP 604 optionals ..." | Re-trigger Greptile
Greptile SummaryThis PR fixes a "last-wins" bug in
Confidence Score: 5/5Safe to merge — the change is a one-statement guard that aligns The fix is minimal and mechanically correct: it adds a No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/streaming_chunk_builder_utils.py | Adds a non-None guard around the prompt_tokens_details assignment in _calculate_usage_per_chunk, matching the existing pattern used for completion_tokens_details. The fix prevents a later chunk with no breakdown from clobbering an earlier accumulated prompt_tokens_details value. |
| tests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_utils.py | Adds test_stream_chunk_builder_preserves_prompt_tokens_details_from_earlier_chunk — a focused regression test using only pre-built dicts (no network calls) that reproduces the exact failure scenario described in the PR and verifies the fix end-to-end. |
Reviews (2): Last reviewed commit: "fix(streaming): don't clobber prompt_tok..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…budget The inline is-not-None guard raised _calculate_usage_per_chunk's cyclomatic complexity from 15 to 16 and tripped scripts/ruff_strict_gate.py (total 317 over limit 316). Move the guard into a _capture_prompt_tokens_details helper mirroring _capture_cache_creation_token_details, restoring the function to its base complexity
… fix-streaming-prompt-tokens-details-clobber # Conflicts: # tests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_utils.py
…guard Keeps the UP045 strict-rule ratchet flat after merging litellm_internal_staging
|
This got fixed on main by #34812, which keeps prompt_tokens_details when a later usage chunk lacks it. Reopen if something is missing |
Relevant issues
Found while auditing the usage accumulation in
stream_chunk_builder; no existing issueLinear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays in PR merge?
Screenshots / Proof of Fix
_calculate_usage_per_chunkoverwrites the accumulatedprompt_tokens_detailson every usage-bearing chunk. When a later usage chunk carries no breakdown (a shape real providers produce when usage arrives on more than one chunk), the accumulatedcached_tokenssplit is reset to None and lost from the combined response, so cache-read discounts disappear from cost tracking and from the OpenAI-compatibleusage.prompt_tokens_detailscustomers read. Every neighboring accumulator in that loop already guards against this (completion_tokens_detailsreplaces only on non-None,cache_read_input_tokenskeeps the last nonzero, and the 5m/1h cache-creation split even has a dedicated rescue added in #30088 because, as its comment says, "prompt_tokens_details is last-wins"); this PR applies the same non-None guard toprompt_tokens_detailsitselfThe repro is the public
stream_chunk_builderentrypoint, exactly what an end user runs; no provider call is needed because the bug is in how already-received chunks are combined:Before the fix:
After the fix:
Regression test added to the mapped test file (
tests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_utils.py); it fails before the fix and passes after.ruff format --checkandruff checkare clean on the touched source fileType
🐛 Bug Fix