Skip to content

fix(sdk): tolerate null cache_creation_tokens in usage telemetry (minimax-m3 crash) - #5327

Merged
juanmichelini merged 4 commits into
mainfrom
fix/telemetry-cache-creation-null
Sep 28, 2026
Merged

juanmichelini merged 4 commits into
mainfrom
fix/telemetry-cache-creation-null

Conversation

@juanmichelini

@juanmichelini juanmichelini commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

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:

image

AGENT:

Why

Running OpenHands locally with minimax-m3 crashed on every LLM call in usage telemetry:

AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'

This did not reproduce on SaaS or with other models.

normalize_usage() in openhands/sdk/llm/utils/telemetry.py read cache-write tokens with a bare attribute access guarded only by model_fields_set:

if (
    prompt_details is not None
    and "cache_creation_tokens" in prompt_details.model_fields_set
):
    cache_write = int(prompt_details.cache_creation_tokens or 0)   # AttributeError

litellm's PromptTokensDetailsWrapper.__init__() deletes optional fields whose value is None:

if self.cache_creation_tokens is None:
    del self.cache_creation_tokens

So when a provider reports prompt_tokens_details with cache_creation_tokens: null (which minimax-m3 does), the field is present in model_fields_set (guard passes) but physically deleted from the instance (bare access raises AttributeError). 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 safe getattr(usage, "_cache_creation_input_tokens", 0) path (a never-deleted PrivateAttr) that the SaaS still runs. The del-on-None behavior 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

  • Use a hasattr guard instead of a bare attribute access in normalize_usage() so a set-but-deleted field is skipped (treated as 0).
  • 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.

Issue Number

Fixes #5326

How to Test

New regression test test_normalize_usage_cache_write_null_does_not_raise exercises the dict path with cache_creation_tokens: None, mirroring the real minimax-m3 response shape.

Verified end-to-end against litellm 1.93.0 (the SDK pin):

  • The new test fails on the unfixed code with the exact AttributeError:
$ pytest tests/sdk/llm/test_llm_span_cost.py::test_normalize_usage_cache_write_null_does_not_raise -q
FAILED tests/sdk/llm/test_llm_span_cost.py::test_normalize_usage_cache_write_null_does_not_raise
1 failed
AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'
  • It passes with the fix, and the full file is green:
$ pytest tests/sdk/llm/test_llm_span_cost.py -q
7 passed
  • Direct repro of the original crash against the unfixed code, via the provider dict path:
>>> from litellm.types.utils import Usage
>>> import openhands.sdk.llm.utils.telemetry as t
>>> u = Usage(prompt_tokens=100, completion_tokens=5,
...           prompt_tokens_details={'cached_tokens': 3, 'cache_creation_tokens': None})
>>> t.normalize_usage(u)
AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'

With the fix applied, the same call returns a UsageSnapshot with cache_write_tokens=0, cache_read_tokens=3.

To reproduce the original live failure locally, configure an LLM with model minimax-m3 and make any completion request; the crash occurs in the on_response telemetry callback before this fix.

Video/Screenshots

N/A — backend telemetry fix; output is captured in the test logs above.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • No litellm version bump is needed; the fix is self-contained in the SDK.
  • The change is minimal and only affects the cache-write token read path. It uses hasattr (not getattr) to comply with the repo's check_forbidden_dynamic_attributes precommit hook, which forbids new getattr/setattr calls 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:2d22680-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-2d22680-python \
  ghcr.io/openhands/agent-server:2d22680-python

All tags pushed for this build

ghcr.io/openhands/agent-server:2d22680-golang-amd64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-golang-amd64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-golang-amd64
ghcr.io/openhands/agent-server:2d22680-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:2d22680-golang-arm64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-golang-arm64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-golang-arm64
ghcr.io/openhands/agent-server:2d22680-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:2d22680-java-amd64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-java-amd64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-java-amd64
ghcr.io/openhands/agent-server:2d22680-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:2d22680-java-arm64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-java-arm64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-java-arm64
ghcr.io/openhands/agent-server:2d22680-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:2d22680-python-amd64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-amd64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-amd64
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-amd64
ghcr.io/openhands/agent-server:2d22680-python-arm64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-arm64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-arm64
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-arm64
ghcr.io/openhands/agent-server:2d22680-python-minimal-amd64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-minimal-amd64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-minimal-amd64
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:2d22680-python-minimal-arm64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-minimal-arm64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-minimal-arm64
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:2d22680-python-slim-amd64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-slim-amd64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-slim-amd64
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:2d22680-python-slim-arm64
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-slim-arm64
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-slim-arm64
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:2d22680-golang
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-golang
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-golang
ghcr.io/openhands/agent-server:2d22680-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:2d22680-java
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-java
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-java
ghcr.io/openhands/agent-server:2d22680-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:2d22680-python-minimal
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-minimal
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-minimal
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-minimal
ghcr.io/openhands/agent-server:2d22680-python-slim
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python-slim
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python-slim
ghcr.io/openhands/agent-server:2d22680-python-node-runtime-slim
ghcr.io/openhands/agent-server:2d22680-python
ghcr.io/openhands/agent-server:2d22680df8f219f4010c2e1d12237dd48a1a80f4-python
ghcr.io/openhands/agent-server:fix-telemetry-cache-creation-null-python
ghcr.io/openhands/agent-server:2d22680-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 2d22680-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 2d22680-python-amd64) are also available if needed

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>
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/llm/utils
   telemetry.py2171792%129, 251, 276, 282–283, 315–317, 340–341, 352–353, 434, 436, 450, 456, 461
TOTAL437661212272% 

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>
@juanmichelini
juanmichelini requested review from all-hands-bot and removed request for all-hands-bot September 25, 2026 19:01
@juanmichelini
juanmichelini requested review from all-hands-bot and removed request for all-hands-bot September 25, 2026 23:42

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@juanmichelini

juanmichelini commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Screenshot showing error as requested
image

@juanmichelini
juanmichelini merged commit 7161237 into main Sep 28, 2026
58 checks passed
@juanmichelini
juanmichelini deleted the fix/telemetry-cache-creation-null branch September 28, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens' on minimax-m3 (local, not SaaS)

4 participants