Skip to content

fix(anthropic): accept a tool_calls turn without content in count validation - #46192

Open
liotur wants to merge 1 commit into
BerriAI:mainfrom
liotur:fix-anthropic-count-tokens-chat-tools
Open

liotur wants to merge 1 commit into
BerriAI:mainfrom
liotur:fix-anthropic-count-tokens-chat-tools

Conversation

@liotur

@liotur liotur commented Oct 13, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • validate_request rejects an assistant tool_calls turn that carries no content key
  • OpenAI leaves content optional on that turn, and an SDK message dumped with exclude_none drops the key
  • Since fix(responses)!: count input tokens the way inference sends the request #47363 the count path itself translates that turn, so this check is the only layer that still refuses it
  • A caller using AnthropicCountTokensConfig or AnthropicCountTokensHandler directly gets a ValueError

How it solves it:

  • tool_calls counts as content for that check
  • A turn carrying neither content nor tool_calls is still refused

This is what is left of this PR after #47363, which covers the chat tool-turn and function tool translation the rest of it did.

User Flow

Before: a chat history is handed to the Anthropic count config directly, and a tool call turn that the chat translation counts fine is refused here

  1. They call AnthropicCountTokensConfig().validate_request(...) with a tool_calls turn whose content key was dropped
  2. It raises ValueError: Message 1 must have a 'content' field

After: the same history validates

  1. They call validate_request(...) with the same messages
  2. It returns, and a turn with neither content nor tool_calls still raises

Relevant issues

Relates to #26324 — the part of it left after #47363

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

No provider call is involved, so the same script runs at the merge base and at this branch's tip.

proof.py

from litellm.llms.anthropic.count_tokens.transformation import AnthropicCountTokensConfig

messages = [
    {"role": "user", "content": "What's the weather in SF?"},
    {"role": "assistant", "tool_calls": [
        {"id": "call_1", "type": "function",
         "function": {"name": "get_weather", "arguments": '{"city": "SF"}'}}]},
    {"role": "tool", "tool_call_id": "call_1", "content": '{"temp_f": 72}'},
]
try:
    AnthropicCountTokensConfig().validate_request(model="claude-opus-5-5", messages=messages)
    print("validate_request: ok")
except ValueError as e:
    print(f"validate_request: ValueError: {e}")

Before

validate_request: ValueError: Message 1 must have a 'content' field

After

validate_request: ok

Tests

pytest tests/unit/llms/anthropic/test_anthropic_count_tokens_transformation.py -k validate_request

Before: the two new accepts_a_tool_call_turn_without_content cases fail with that ValueError. After: 4 passed.

Type

🐛 Bug Fix

Caveats (if any)

Low

@codspeed

codspeed Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 35 untouched benchmarks


Comparing liotur:fix-anthropic-count-tokens-chat-tools (42ee71b) with main (72f1ac4)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (dddbaa8) during the generation of this report, so 72f1ac4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

#47363 now translates chat tool turns and function tools for Anthropic counts. A tool_calls turn without content still fails, so that part here stays useful

@liotur
liotur force-pushed the fix-anthropic-count-tokens-chat-tools branch from f307ecc to 91538ea Compare October 26, 2026 09:53
@liotur liotur changed the title fix(anthropic): translate chat tool turns and function tools for count_tokens fix(anthropic): accept a tool_calls turn without content in count validation Oct 26, 2026
@liotur

liotur commented Oct 26, 2026

Copy link
Copy Markdown
Author

Rebased on main. #47363 covers the tool-turn and function-tool translation, so I dropped that part. What is left is the turn with tool_calls and no content key, which validate_request still raises on, and the title now matches that scope.

@liotur
liotur force-pushed the fix-anthropic-count-tokens-chat-tools branch 2 times, most recently from 42ee71b to fac1192 Compare October 30, 2026 16:36
…idation

The chat translation merged in BerriAI#47363 counts an assistant tool_calls turn,
but validate_request still rejects that turn when it carries no content
key. OpenAI leaves content optional there, and an SDK message dumped with
exclude_none drops the key entirely, so a caller going through the config
or the handler directly gets a ValueError on a turn every other layer now
accepts

Treat tool_calls as the content for that check. A turn with neither is
still refused

Relates to BerriAI#26324
@liotur
liotur force-pushed the fix-anthropic-count-tokens-chat-tools branch from fac1192 to 8e472d4 Compare November 27, 2026 17:02
@veria-ai

veria-ai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Reviews

This branch has not been deployed

No deployments
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.

2 participants