Repository navigation
fix(guardrails): keep client tools verbatim when an Anthropic Messages guardrail returns them unchanged - #50637
Closed
zhaojiangang-it wants to merge 1 commit into
Conversation
…s guardrail returns them unchanged The Anthropic guardrail translation rebuilt data["tools"] from the guardrail's return value even when nothing changed. The rebuild drops web_search server tools, which the translation moved to web_search_options, and renames nameless tools like mcp_toolset to litellm_unnamed_tool_0. Snapshot the translated tools before the guardrail call and skip the rebuild when the returned tools match the snapshot, so unchanged tools reach the provider exactly as the client sent them. Fixes BerriAI#50055
9 tasks done
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Handwritten 1-3 sentence description
A guardrail that never touched the tools still made LiteLLM rebuild the tool list on /v1/messages, and the rebuild silently dropped web_search server tools and renamed nameless ones like mcp_toolset to litellm_unnamed_tool_0. This PR snapshots the tools right before the guardrail call and skips the rebuild when the guardrail returns them unchanged, so the provider receives exactly what the client sent. Guardrails that do change tools keep the existing rebuild path.
Details
TLDR
Problem this solves:
How it solves it:
User Flow
Before: a Claude Code user behind a proxy with a pre_call guardrail loses server tools on every request
After: the same request reaches the provider exactly as the client sent it
Relevant issues
Fixes #50055
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
The defect is in the request body LiteLLM sends to the provider, so the proof follows the issue's own repro and points the deployment at a logging upstream that records the body verbatim. A real provider call cannot show more than the body it receives, because the rebuilt request stays valid and the loss is silent. Ran single worker with no Redis since the change is an in-process request transformation with no cross-worker state.
Shared setup, identical for both cases:
fake_anthropic.py, then start it withpython3 fake_anthropic.py received.jsonl 18998config.yaml, then start the proxy withuv run python litellm/proxy/proxy_cli.py --config config.yaml --port 4100 --detailed_debug > proxy.log 2>&1 &curl -s http://127.0.0.1:4100/v1/messages -H "Authorization: Bearer sk-test" -H "Content-Type: application/json" -H "anthropic-version: 2023-06-01" -d @request.jsonpython3 -c "import json; [print(json.dumps(json.loads(line)['body'].get('tools'), indent=1)) for line in open('received.jsonl') if json.loads(line)['path'].endswith('/v1/messages')]"grep "Allowing request" proxy.logfake_anthropic.py, the logging upstream from the issue's repro
config.yaml and request.json
{ "model": "claude", "max_tokens": 64, "messages": [{ "role": "user", "content": "search the docs" }], "tools": [ { "type": "web_search_20250305", "name": "web_search", "max_uses": 1 }, { "type": "mcp_toolset", "mcp_server_name": "github" }, { "name": "Read", "description": "d", "input_schema": { "type": "object", "properties": {} } }, { "name": "mcp__atlassian__create_confluence_footer_comment_with_a_very_long_name_x", "description": "d", "defer_loading": true, "input_schema": { "type": "object", "properties": {} } } ] }Before (e58354f)
Custom code guardrail 'passthrough': Allowing request, so the guardrail ran[{"name": "litellm_unnamed_tool_0", "input_schema": {"type": "object", "properties": {}}, "type": "custom"}, {"name": "Read", "input_schema": {"type": "object", "properties": {}}, "type": "custom", "description": "d"}, {"name": "mcp__atlassian__create_confluence_footer_comment_with_a_very_long_name_x", "input_schema": {"type": "object", "properties": {}}, "type": "custom", "description": "d"}]After (911bfdb)
[{"type": "web_search_20250305", "name": "web_search", "max_uses": 1}, {"type": "mcp_toolset", "mcp_server_name": "github"}, {"name": "Read", "description": "d", "input_schema": {"type": "object", "properties": {}}}, {"name": "mcp__atlassian__create_confluence_footer_comment_with_a_very_long_name_x", "description": "d", "defer_loading": true, "input_schema": {"type": "object", "properties": {}}}]Unit tests,
uv run pytest tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py -q, 134 passed at the tip. The regression testtest_unmodified_guardrail_tools_keep_client_tools_verbatimfails at the merge base with the 3-tool rebuild shown above, and passes at the tip.Type
🐛 Bug Fix
Caveats (if any)
Medium
Low
Final Attestation