Repository navigation
fix(sdk): read cache buckets via model_dump to fix AttributeError for providers without prompt caching - #5329
atifnaqvi-jpg wants to merge 1 commit into
Conversation
normalize_usage() read prompt_tokens_details.cache_creation_tokens directly, guarded only by membership in model_fields_set. LiteLLM's PromptTokensDetailsWrapper deletes unset optional fields while leaving the name in model_fields_set, so the guard passes and the read raises AttributeError, failing LLM profile validation. Read the details through model_dump() instead: a deleted field is absent from the dump and defaults to 0, with no dynamic attribute access. Also read the cache_write_tokens naming, which was previously ignored, and add regression tests covering the deleted-field shape and that naming.
⏳ Review waiting on checksThe current head
No action is needed. The scheduled scan retries once every check on the head reports a conclusion. This is an automated check - no AI was used to generate this comment. |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope
This change belongs in this repository: it fixes normalize_usage() in openhands-sdk/openhands/sdk/llm/utils/telemetry.py, which is SDK-owned, and the linked issue #5168 carries ready-for-dev. No cross-repository move is needed.
What I verified on head 4305d13c
- The
AttributeErroris real and the fix addresses it. Against the pinned litellm 1.93.0 I reproduced the failing shape:PromptTokensDetailsWrapper(cached_tokens=25, cache_creation_tokens=None)keepscache_creation_tokensinmodel_fields_setwhilehasattr(..., "cache_creation_tokens")isFalse, so the old guard passed and the attribute read raised. The newmodel_dump()read returnscache_creation_tokens: 30for the Anthropic naming andcache_write_tokens: 5for the OpenAI naming, andcached_tokensis read correctly. uv run --frozen pytest tests/sdk/llm/test_llm_telemetry.py -q-> 49 passed on this head, and the new tests use realPromptTokensDetailsWrapperobjects instead of mocks, matching the repo's "prefer real code paths over mocks" guidance.- Head SHA is still
4305d13cd6288059dafb0ac031102ed1b7dd581d. The only PR comment is the automation's "Review waiting on checks" note; there are no reviews or review comments on this head yet. Issue #5168 is still open withbug,llm,priority:medium,sdk,ready-for-dev.
Findings
1. This change is already superseded on main (and now conflicts with it)
main has moved since this branch was cut: base.sha is fcc102a, and the PR is currently mergeable: false / mergeable_state: dirty. The conflict is exactly this hunk. PR #5327 ("fix(sdk): tolerate null cache_creation_tokens in usage telemetry (minimax-m3 crash)") merged to main on 2026-09-28 as commit 71612374a8, and main's normalize_usage() now reads:
if (
prompt_details is not None
and "cache_creation_tokens" in prompt_details.model_fields_set
and hasattr(prompt_details, "cache_creation_tokens")
):
cache_write = int(prompt_details.cache_creation_tokens or 0)So the AttributeError described here is already fixed on main, and rebasing this PR would drop that fix (git merge-tree reports a content conflict in telemetry.py between fcc102a and this head). Two consequences for the merge decision:
- Linked issue #5168 is already resolved on
mainby #5327; the description's note that acached_tokens-only wrapper "does not reproduce under the litellm version pinned inuv.lock" describesmainbefore #5327, not currentmain. - Sibling PRs #5169 and #5335 target the same hunk. Merging a third variant of the same guard is redundant; the incremental value here (also reading the OpenAI
cache_write_tokensnaming viamodel_dump()) should be rebased ontomainand de-duplicated, or folded into the surviving PR.
2. Live production evidence is missing for a production-facing bug fix
The repository review guide requires authentic before/after evidence through the real supported entry point for bug fixes whose failure occurs on a runtime or integration path. The PR reports manual local reproduction and "verified locally" but includes no logs, API responses, or persisted artifacts from the profile-validation path. The new tests forge the failing state directly, which is valid regression protection but does not show that production reaches it.
3. CI on this head is not green
Every pull_request workflow run on 4305d13c (latest at 2026-09-25T18:19Z) reports conclusion: action_required, including Run tests and Pre-commit checks. The checks reporting success (PR Description Check, PR Artifacts) are pull_request_target runs. The "49 passed" in the description is a local run, not this head's CI result. Combined with the dirty merge state above, this is not an approvable head.
Verdict
The fix itself contains no code defect, but this head cannot be approved: it is superseded by the merged #5327, now conflicts with main, and its required CI has not passed. Recommend closing this PR in favor of the change already on main (or rebasing onto main and de-duplicating against #5169/#5335), with a maintainer decision on which PR survives.
🔄 CHANGES REQUESTED
|
Closing this in favour of #5327, which merged to Confirmed by the review on this head: For the record, the one increment this branch had over #5327 is reading both cache-write namings ( Thanks for the review. |
HUMAN:
Reproduced this on my self hosted Agent Canvas where profile saves were blocked while using ollama cloud models specifically deepseek flash 4.1 . Patch is now verified locally; the AttributeError no longer occurs and profile saves now succeed.
AGENT:
Why
normalize_usage()readsprompt_tokens_details.cache_creation_tokensdirectly, guarded only by membership inmodel_fields_set:LiteLLM's
PromptTokensDetailsWrapper.__init__deletes unset optional fields, while its__setattr__mirroring keeps the name inmodel_fields_set. The guard therefore passes and the read raisesAttributeError.Summary
cache_creation_tokens,cache_write_tokens) throughmodel_dump()instead of a direct attribute read, so a deleted optional field defaults to 0 with no dynamic attribute access.MagicMock-based cache-read test with a realPromptTokensDetailsWrapper, and add tests covering the deleted-field shape and thecache_write_tokensnaming.Issue Number
Fixes #5168.
How to Test
Reproduced and verified on a self-hosted Agent Canvas (
@openhands/agent-canvas1.24.0, agent-server 1.49.6, litellm on the OpenAI-compatible route).1. Reproduce the crash on unmodified source. Building a
Usagewith aPromptTokensDetailsWrapperthat omits cache fields, then callingnormalize_usage(), raises before the fix:Shape used (raises on the unfixed code at any litellm version):
Note that a
cached_tokens-only wrapper does not reproduce under the litellm version pinned inuv.lock— the name stays out ofmodel_fields_setthere, so the old guard returns early. That is why the earlier regression test did not guard this bug in CI.2. End-to-end on the Canvas UI. Saving an LLM profile for a provider that reports no cache fields previously failed validation. After the fix:
AttributeErrorappears in the agent-server log for the profile-validation path.3. Commands run.
All hooks pass, including
Forbid dynamic attribute access in SDK(nogetattrintroduced),Ruff format,Ruff lintand pyright.The new naming test fails on unmodified source (
assert 0 == 5) and passes with the fix.