-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix TypeError when tracking usage with Anthropic models returning Pydantic objects #8978
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
Merged
Conversation
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
…racker Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix TypeError when tracking usage with Anthropic models
Fix TypeError when tracking usage with Anthropic models returning Pydantic objects
Oct 27, 2025
Copilot finished work on behalf of
TomeHirata
October 27, 2025 01:38
chenmoneygithub
approved these changes
Oct 27, 2025
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.
I simplified the implementation a bit, but copilot did accurately find the error and made the fix.
Meanwhile it seems to understand our unit test poorly, in its code it's using Anthropic's format, while we should use litellm's.
hironow
added a commit
to hironow/dspy
that referenced
this pull request
Oct 30, 2025
commit 056d54e Author: Isaac Miller <17116851+isaacbmiller@users.noreply.github.com> Date: Wed Oct 29 17:23:09 2025 +0100 fix(MIPROv2): zero shot not taking .compile parameters into account before determining if the program was zero shot (stanfordnlp#8909) * fix(MIPROv2): zero shot not taking .compile parameters into account before determining if the program was zero shot * remove extra logs * Remove log * Fix merge conflict * Remove extra whitespace commit da69f9d Author: TomuHirata <tomu.hirata@gmail.com> Date: Wed Oct 29 13:23:34 2025 +0900 Update anthropic model name (stanfordnlp#8992) Signed-off-by: TomuHirata <tomu.hirata@gmail.com> commit aaadf05 Author: Chen Qian <chen.qian@databricks.com> Date: Tue Oct 28 12:21:55 2025 -0700 lints (stanfordnlp#8987) commit e842ba1 Author: eramis73 <130156545+eramis73@users.noreply.github.com> Date: Tue Oct 28 02:40:34 2025 +0300 [docs] Add Google-style docstrings for dspy/evaluate/metrics.py (stanfordnlp#8954) * docs(metrics): add Google-style docstrings for public metrics * docs(metrics): address review feedback (concise openings, mkdocs block examples); revert non-doc changes * fixes --------- Co-authored-by: chenmoneygithub <chen.qian@databricks.com> commit 6c43880 Author: TomuHirata <tomu.hirata@gmail.com> Date: Tue Oct 28 07:21:06 2025 +0900 Cache Ollama to speed up CI (stanfordnlp#8972) * Cache Ollama to speed up CI * fix permission commit 462baef Author: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon Oct 27 11:57:27 2025 -0700 Fix TypeError when tracking usage with Anthropic models returning Pydantic objects (stanfordnlp#8978) * Initial plan * Fix TypeError when merging Anthropic CacheCreation objects in usage tracker Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> * Enhance _flatten_usage_entry to convert Pydantic models on first add Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> * Fix potential TypeError when both usage entries are None Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> * simplify * small fix * lint * robust version handling --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> Co-authored-by: chenmoneygithub <chen.qian@databricks.com> commit 9b467b5 Author: Noah Ziems <nziems2@nd.edu> Date: Mon Oct 27 13:32:07 2025 -0400 Add Disable Fallback Option in ChatAdapter (stanfordnlp#8984) commit bf022c7 Author: Lakshya A Agrawal <lakshyaaagrawal@berkeley.edu> Date: Sat Oct 25 23:37:42 2025 +0530 Update gepa[dspy] dependency version to 0.0.18 (stanfordnlp#8969) * Update gepa[dspy] dependency version to 0.0.18 * Update pyproject.toml * fix test --------- Co-authored-by: TomuHirata <tomu.hirata@gmail.com>
hironow
added a commit
to hironow/dspy
that referenced
this pull request
Oct 30, 2025
commit 31b96af Author: Dushmanta <dushmanta0511@gmail.com> Date: Thu Oct 30 13:52:40 2025 +0530 fix: broken PyPI downloads badge from pepy.tech in README and docs home page (stanfordnlp#8995) * fix: update broken pypi download badge in readme * fix: update broken pypi download badge in docs home page commit 056d54e Author: Isaac Miller <17116851+isaacbmiller@users.noreply.github.com> Date: Wed Oct 29 17:23:09 2025 +0100 fix(MIPROv2): zero shot not taking .compile parameters into account before determining if the program was zero shot (stanfordnlp#8909) * fix(MIPROv2): zero shot not taking .compile parameters into account before determining if the program was zero shot * remove extra logs * Remove log * Fix merge conflict * Remove extra whitespace commit da69f9d Author: TomuHirata <tomu.hirata@gmail.com> Date: Wed Oct 29 13:23:34 2025 +0900 Update anthropic model name (stanfordnlp#8992) Signed-off-by: TomuHirata <tomu.hirata@gmail.com> commit aaadf05 Author: Chen Qian <chen.qian@databricks.com> Date: Tue Oct 28 12:21:55 2025 -0700 lints (stanfordnlp#8987) commit e842ba1 Author: eramis73 <130156545+eramis73@users.noreply.github.com> Date: Tue Oct 28 02:40:34 2025 +0300 [docs] Add Google-style docstrings for dspy/evaluate/metrics.py (stanfordnlp#8954) * docs(metrics): add Google-style docstrings for public metrics * docs(metrics): address review feedback (concise openings, mkdocs block examples); revert non-doc changes * fixes --------- Co-authored-by: chenmoneygithub <chen.qian@databricks.com> commit 6c43880 Author: TomuHirata <tomu.hirata@gmail.com> Date: Tue Oct 28 07:21:06 2025 +0900 Cache Ollama to speed up CI (stanfordnlp#8972) * Cache Ollama to speed up CI * fix permission commit 462baef Author: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon Oct 27 11:57:27 2025 -0700 Fix TypeError when tracking usage with Anthropic models returning Pydantic objects (stanfordnlp#8978) * Initial plan * Fix TypeError when merging Anthropic CacheCreation objects in usage tracker Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> * Enhance _flatten_usage_entry to convert Pydantic models on first add Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> * Fix potential TypeError when both usage entries are None Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> * simplify * small fix * lint * robust version handling --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com> Co-authored-by: chenmoneygithub <chen.qian@databricks.com> commit 9b467b5 Author: Noah Ziems <nziems2@nd.edu> Date: Mon Oct 27 13:32:07 2025 -0400 Add Disable Fallback Option in ChatAdapter (stanfordnlp#8984) commit bf022c7 Author: Lakshya A Agrawal <lakshyaaagrawal@berkeley.edu> Date: Sat Oct 25 23:37:42 2025 +0530 Update gepa[dspy] dependency version to 0.0.18 (stanfordnlp#8969) * Update gepa[dspy] dependency version to 0.0.18 * Update pyproject.toml * fix test --------- Co-authored-by: TomuHirata <tomu.hirata@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Anthropic's API returns
CacheCreationPydantic model objects in usage data when prompt caching is enabled.UsageTracker._merge_usage_entries()attempted to add these objects arithmetically, causingTypeError: unsupported operand type(s) for +: 'CacheCreation' and 'CacheCreation'.Changes
_flatten_usage_entry(): Convert PydanticBaseModelinstances to dicts viamodel_dump()when usage is added_merge_usage_entries(): Detect and convert any Pydantic models before merging; recursively merge nested dicts and sum numeric fieldsTests: Added
test_merge_usage_entries_with_pydantic_models()validating multipleCacheCreationobjects merge correctlyFix applies to any Pydantic model objects from any LM provider.
Original prompt
This section details on the original issue you should resolve
<issue_title>[Bug]
TypeErrorwhen usingtrack_usage()with Anthropic models that returnCacheCreationobjects</issue_title><issue_description>### What happened?
Summary
The
UsageTrackercrashes with aTypeErrorwhen tracking usage for Anthropic models that use prompt caching. Anthropic's API returnsCacheCreationPydantic model objects in the usage data, but DSPy's_merge_usage_entries()attempts to add these objects arithmetically, which fails.Expected Behavior
Usage tracking should work correctly with all LM providers, including Anthropic models with prompt caching enabled. The tracker should either:
Actual Behavior
When calling
tracker.get_total_tokens()after making multiple API calls to Anthropic with caching enabled, aTypeErroris raised:Root Cause
The bug seems to be in
dspy/utils/usage_tracker.py:44:This line assumes all usage values are numeric (int/float), but Anthropic's API returns structured objects like:
When merging usage from multiple API calls, the code tries to add two
CacheCreationobjects together, which fails because Pydantic models don't support addition.Steps to Reproduce
I've included a standalone reproduction script (
repro_dspy_cache_bug.py) that demonstrates the issue. To run it:Additional Context
track_usage()contextCacheCreationobjects (when caching is used)Proposed Solution
The
_merge_usage_entries()method should detect non-numeric values and handle them appropriately. Possible approaches:I can submit a PR with a fix if helpful!
Steps to reproduce
Run the following with:
DSPY_CACHEDIR=./dspycache_$(date +%s).cache uv run --prerelease=allow repro_dspy_cache_bug.py