Repository navigation
Conversation
Merging this PR will not alter performance1 warning
Comparing Footnotes |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
#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 |
f307ecc to
91538ea
Compare
|
Rebased on main. #47363 covers the tool-turn and function-tool translation, so I dropped that part. What is left is the turn with |
42ee71b to
fac1192
Compare
…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
fac1192 to
8e472d4
Compare
TLDR
Problem this solves:
validate_requestrejects an assistanttool_callsturn that carries nocontentkeycontentoptional on that turn, and an SDK message dumped withexclude_nonedrops the keyAnthropicCountTokensConfigorAnthropicCountTokensHandlerdirectly gets aValueErrorHow it solves it:
tool_callscounts as content for that checkcontentnortool_callsis still refusedThis is what is left of this PR after #47363, which covers the chat tool-turn and
functiontool 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
AnthropicCountTokensConfig().validate_request(...)with atool_callsturn whosecontentkey was droppedValueError: Message 1 must have a 'content' fieldAfter: the same history validates
validate_request(...)with the same messagescontentnortool_callsstill raisesRelevant 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
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 moreDelays 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.pyBefore
After
Tests
pytest tests/unit/llms/anthropic/test_anthropic_count_tokens_transformation.py -k validate_requestBefore: the two new
accepts_a_tool_call_turn_without_contentcases fail with thatValueError. After: 4 passed.Type
🐛 Bug Fix
Caveats (if any)
Low
acount_tokensand/utils/token_counterpaths already count this turn after fix(responses)!: count input tokens the way inference sends the request #47363; this only lines the validation check up with them