Skip to content

fix: normalize Anthropic server tool usage#26904

Open
Genmin wants to merge 3 commits into
BerriAI:litellm_internal_stagingfrom
Genmin:fix/anthropic-passthrough-server-tool-use
Open

fix: normalize Anthropic server tool usage#26904
Genmin wants to merge 3 commits into
BerriAI:litellm_internal_stagingfrom
Genmin:fix/anthropic-passthrough-server-tool-use

Conversation

@Genmin

@Genmin Genmin commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • normalize dict server_tool_use values into ServerToolUse inside Usage
  • preserve constructor/model_dump round-trips for raw Anthropic-compatible usage payloads
  • cover the built-in tool cost tracking path that reads usage.server_tool_use.web_search_requests

Fixes #26749.

Tests

  • /tmp/uv-0.10.9-env/bin/uv run --extra proxy pytest tests/test_litellm/types/test_types_utils.py tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py -q
  • /tmp/uv-0.10.9-env/bin/uv run --extra proxy ruff check --ignore PLR0915 litellm/types/utils.py tests/test_litellm/types/test_types_utils.py tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py
  • git diff --check

Note: full touched-file ruff still reports the existing PLR0915 limit on Delta.__init__; the targeted lint command above keeps the check focused on actionable findings for this patch.

@CLAassistant

CLAassistant commented Apr 30, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where Anthropic passthrough responses that construct Usage from a raw dict payload would fail downstream when StandardBuiltInToolCostTracking tried to read usage.server_tool_use.web_search_requests — because the dict was never coerced into a ServerToolUse instance. The fix normalizes dict values to ServerToolUse inside Usage.__init__, mirrors the existing pattern already used for prompt_tokens_details and completion_tokens_details, and is covered by two new unit tests including a model_dump() round-trip check.

Confidence Score: 5/5

Safe to merge — targeted, well-tested fix with no backwards-incompatible changes.

The change is minimal and follows an established pattern already used for other nested usage fields. Both the conversion logic and the round-trip are directly tested. Unused imports (including a rule-violating fastapi.testclient import) are cleaned up as a side effect.

No files require special attention.

Important Files Changed

Filename Overview
litellm/types/utils.py Adds dict-to-ServerToolUse normalization in Usage.init; field annotation stays typed as Optional[ServerToolUse] and conversion happens correctly after super().init().
tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py Adds test covering the dict server_tool_use path through StandardBuiltInToolCostTracking; removes unused imports including the fastapi TestClient import that violated the no-FastAPI-outside-proxy rule.
tests/test_litellm/types/test_types_utils.py Adds test for dict-to-ServerToolUse conversion and model_dump round-trip; cleans up unused imports.

Reviews (1): Last reviewed commit: "fix: normalize Anthropic server tool usa..." | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing Genmin:fix/anthropic-passthrough-server-tool-use (380214f) with main (3e1479c)

Open in CodSpeed

@codecov

codecov Bot commented Apr 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

Anthropic passthrough: server_tool_use parsed as dict instead of ServerToolUse in usage

2 participants