-
Notifications
You must be signed in to change notification settings - Fork 296
fix(langchain): handle Anthropic cache_creation nested-dict in _parse_usage_model #1698
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
Open
ASAD-BE18
wants to merge
5
commits into
langfuse:main
Choose a base branch
from
ASAD-BE18:fix/anthropic-cache-creation-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
538ec1d
fix(langchain): handle Anthropic cache_creation nested-dict in _parse…
ASAD-BE18 348a4ab
Merge branch 'main' into fix/anthropic-cache-creation-usage
ASAD-BE18 7ce5654
Merge branch 'main' into fix/anthropic-cache-creation-usage
ASAD-BE18 fe3af4d
test(langchain): use distinct legacy value in setdefault test
ASAD-BE18 d2d1432
Merge branch 'main' into fix/anthropic-cache-creation-usage
ASAD-BE18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🟡 The
test_anthropic_cache_creation_legacy_field_not_overwrittentest uses legacycache_creation_input_tokens=300and tier values that sum to200+100=300— the same value. Because the legacy value equals the tier sum, the assertionresult["cache_creation_input_tokens"] == 300would pass even ifsetdefaultwere regressed to a plain assignment (usage_model["cache_creation_input_tokens"] = total), defeating the test's stated purpose of guarding setdefault semantics. Use a legacy value that differs from the tier sum (e.g.cache_creation_input_tokens=999with tiers summing to 300) so a regression to direct assignment would be caught.Extended reasoning...
What the bug is. The new test
test_anthropic_cache_creation_legacy_field_not_overwritten(tests/unit/test_parse_usage_model.py:57-73) is intended to prove that the production code usessetdefault("cache_creation_input_tokens", total)— i.e. that a pre-existing legacy scalar value is preserved instead of being overwritten by the dict total. As constructed, however, the test cannot distinguish between the correctsetdefaultimplementation and a buggy plain-assignment regression.How it manifests. The fixture sets
cache_creation_input_tokens=300(legacy scalar) and a nestedcache_creationdict whose tiers sum to200+100=300. The only assertion on this field isassert result["cache_creation_input_tokens"] == 300. Since the legacy value (300) numerically equals the tier sum (300), both implementations produce the same final value:setdefault): key already exists with 300 → kept at 300 ✓total=300→ ends at 300 ✓Both branches satisfy the assertion, so the test is silent on which behavior the code actually has.
Why existing code doesn't prevent it. Nothing else in the test file pins
cache_creation_input_tokensto a value different from the tier sum. The two sibling tests use a single non-overlapping field (test_anthropic_cache_creation_dict_flattenedhas no legacy field present;test_anthropic_cache_creation_all_zeros_no_aggregateasserts the key is absent). So no test in the suite would fail ifsetdefaultwere swapped for direct assignment.Step-by-step proof. Mentally apply the regressed implementation
usage_model["cache_creation_input_tokens"] = totalto the fixture:cache_creation_input_tokens: 300andcache_creation: {ephemeral_1h_input_tokens: 200, ephemeral_5m_input_tokens: 100}.cache_creation, computestotal = 200 + 100 = 300.usage_model["cache_creation_input_tokens"] = 300overwrites the legacy 300 with the new 300 — value unchanged.result["cache_creation_input_tokens"] == 300— assertion passes despite the regression.Impact. Pure test-quality issue, no production impact. The production code in
langfuse/langchain/CallbackHandler.pycorrectly usessetdefaultand the fix as shipped is correct. But the regression test guarding it has zero discriminating power — a future refactor (or AI-assisted edit) that accidentally dropssetdefaultfor direct assignment would slip through CI.How to fix. Change the fixture so the legacy value differs from the tier sum, e.g.:
and assert
result["cache_creation_input_tokens"] == 999. Undersetdefaultthe legacy 999 wins; under plain assignment the value would become 300 and the assertion would fail — giving the test the discriminating power it claims to have.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.
fixed in fe3af4d