litellm/tests/unit/llms/anthropic/chat
drewv19 dfc4c5fb53
fix(anthropic): keep caller tool_use ids unless the upstream rejects them (#54468)
* fix(anthropic): keep tool_use ids for non-Anthropic backends on /v1/messages

Since v1.91.0 every /v1/messages request rewrote tool ids to Anthropic's ^[a-zA-Z0-9_-]+$ pattern, so Kimi on vLLM got functions_Bash_0 back for its own functions.Bash:0 and stopped calling tools

The anthropic provider now rewrites only for a Claude model or an api_base that serves Anthropic's API (api.anthropic.com or Foundry, explicit or from ANTHROPIC_API_BASE / ANTHROPIC_BASE_URL), in Python and Rust. Every other native provider keeps the rewrite. Chat-bridged providers send and return the provider's own tool call ids, with only the Gemini thought suffix stripped

* fix(anthropic): rename tool_use ids on Bedrock and Azure AI, skip logins when deciding

A Bedrock application inference profile ARN or an Azure AI deployment name can serve Claude without saying so, and Bedrock rejects ids like functions.Bash:0, so both providers always rename

The check now resolves the provider the way request metadata does, so a github_copilot or chatgpt model never starts that provider's login just to decide whether to rename

* fix(anthropic): keep caller tool_use ids unless the upstream rejects them

Rename tool_use ids up front only for Anthropic's own hosts and Foundry, Bedrock, Azure AI and providers with a native messages config, in Python and Rust, with the claude model name shortcut removed

When an upstream answers 400 with a tool_use id pattern mismatch, native /v1/messages (Python and Rust), the chat-bridged adapter and anthropic chat completions resend once with renamed ids. The retry fires only when renaming changes the request and composes with the thinking and strict tools retries, so the attempt cap is now 3 and the thinking retry also fires only when stripping changes the request

Token counting follows the same per-deployment decision so counts match the ids that are sent

* fix(anthropic): rename ids when counting tokens, allow four attempts, fill empty tool ids

Token counting renames tool_use ids again for every deployment, as it did before this branch, since a count has no resend and a Claude gateway rejects the caller's ids there

The messages retry cap is 4 in Python and Rust, so the strict-tools drop, the tool_use id resend and the thinking strip can all fire on one request

A tool call that comes back without an id, or with only a thought signature, gets a generated id on the streamed and non-streamed paths, so two such calls never share one

* test(anthropic): cover function results and an unresolvable model in the tool id decision

* fix(anthropic): keep caller tool ids through guardrail rewrites and the global api_base

* fix(anthropic): send an empty tool id as tool_use_id when keeping caller ids

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-26 05:55:01 +00:00
..
guardrail_translation fix(anthropic): keep caller tool_use ids unless the upstream rejects them (#54468) 2026-11-26 05:55:01 +00:00
__init__.py
conftest.py
test_anthropic_chat_handler.py fix(anthropic): keep caller tool_use ids unless the upstream rejects them (#54468) 2026-11-26 05:55:01 +00:00
test_anthropic_chat_transformation.py fix(gateway): serve messages drop, summary and timeout settings like Python (#51236) 2026-11-25 09:26:12 +00:00
test_code_interpreter_results_extraction.py feat(anthropic): workload identity federation and pluggable identity sources (#44448) 2026-10-03 17:08:30 -07:00
test_transformation.py test: move whole-unit legacy test files into tests/unit and delete dead skips (#44807) 2026-10-07 05:02:28 +00:00