litellm/tests/unit/proxy/guardrails
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_hooks fix(anthropic): keep caller tool_use ids unless the upstream rejects them (#54468) 2026-11-26 05:55:01 +00:00
__init__.py
test_anthropic_sse.py fix(guardrails)!: mask PII in Presidio tool calls and refuse streams it cannot read (#53396) 2026-11-23 03:09:43 -08:00
test_auto_router_compression.py
test_content_filter_path_traversal.py
test_content_utils.py fix(decisions): accept OpenAI input_json content parts on /v1/decisions (#52805) 2026-11-20 13:33:36 -08:00
test_custom_code_security.py fix(guardrails): mask reasoning text on /v1/responses output (#52504) 2026-11-19 01:25:06 -08:00
test_deferred_guardrail_logging.py fix(logging): retain streaming success logging tasks until they finish (#52067) 2026-11-16 11:21:05 -08:00
test_endpoint_image_scan.py feat(guardrails): scan an image on /apply_guardrail with a Bedrock guardrail (#51016) 2026-11-17 18:13:28 +00:00
test_guardrail_coverage.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
test_guardrail_endpoints.py fix(guardrails)!: close gaps in bedrock tool call and tool description scans (#54169) 2026-11-25 06:56:15 +00:00
test_guardrail_helpers.py feat(guardrails): reject requests that name an unregistered guardrail when reject_unregistered_guardrails is on (#53214) 2026-11-20 18:39:29 -08:00
test_guardrail_initializers.py
test_guardrail_load_balancing.py
test_guardrail_registry.py fix(guardrails)!: close gaps in bedrock tool call and tool description scans (#54169) 2026-11-25 06:56:15 +00:00
test_init_guardrails.py fix(guardrails): stop Presidio logging_only from masking the client response (#54502) 2026-11-25 21:42:48 +00:00
test_llm_as_a_judge.py fix(guardrails): mask reasoning text on /v1/responses output (#52504) 2026-11-19 01:25:06 -08:00
test_mcp_jwt_signer.py fix(mcp)!: forward the LiteLLM key when the admin configures it (#50604) 2026-11-10 20:51:34 -08:00
test_pillar_guardrails.py refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986) 2026-11-06 13:31:15 -08:00
test_prompt_security_guardrails.py
test_qostodian_nexus_guardrail.py
test_usage_endpoints.py fix(guardrails): serve usage detail for a guardrail removed from config (#53315) 2026-11-23 11:54:37 -08:00
test_usage_tracking.py feat(spend-logs): rust spend log ledger writer (#52163) 2026-11-20 16:31:04 -08:00