Skip to content

fix(guardrails): show the /v1/messages web search tool to guardrails - #53376

Merged
yucheng-berri merged 3 commits into
mainfrom
litellm_messages_guardrail_sees_web_search
Nov 22, 2026
Merged

yucheng-berri merged 3 commits into
mainfrom
litellm_messages_guardrail_sees_web_search

Conversation

@yucheng-berri

@yucheng-berri yucheng-berri commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

[insert handwritten description here]

Details

TLDR

Problem this solves:

  • Guardrails on /v1/messages never see Anthropic's web_search tool
  • So a guardrail cannot inspect, allow or remove it
  • It was dropped anyway whenever a guardrail returned tools

How it solves it:

  • Shows web_search to guardrails as sent, in its request position
  • Same as web_fetch, bash and other hosted tools already are
  • Returned by the guardrail: sent on. Left out: removed

Intentional product change: guardrails that return tools now decide whether web_search reaches the provider. A guardrail that allow-lists tools by name must list web_search to keep it.

User Flow

Before: a team that guards tools with a guardrail cannot control web search

  1. They send POST https://litellm-domain/v1/messages with tools [Read, web_search_20250305] behind a guardrail that returns tools
  2. The guardrail is shown only Read
  3. The provider receives only Read, whatever the guardrail answered, and the model says it cannot search

After: the guardrail sees web search and its answer decides

  1. They send the same POST https://litellm-domain/v1/messages
  2. The guardrail is shown Read and web_search_20250305
  3. If it returns both, the provider receives both and the model can search
  4. If it leaves web_search out, the provider receives only Read

Linear ticket

Relates to LIT-10590

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

Screenshots / Proof of Fix

Two live proxies on one VM, 2 workers each, own Postgres and Redis, real provider calls. A forwarding recorder in front of each provider captured what the proxy sent. Every request is POST /v1/messages with tools [Read, web_search_20250305] unless the cell says otherwise.

Setup to rerun: config.yaml, start command, request

config.yaml (the opt-in runs swap the guardrails block for custom-code guardrails that allow all, mask the Read description, or return tools without Read):

model_list:
  - model_name: gpt
    litellm_params:
      model: openai/gpt-5.4-mini
      api_key: os.environ/OPENAI_API_KEY
  - model_name: claude-bedrock-invoke
    litellm_params:
      model: bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0
      aws_region_name: us-west-2
general_settings:
  master_key: sk-1234
guardrails:
  - guardrail_name: content-filter
    litellm_params:
      guardrail: litellm_content_filter
      mode: pre_call
      default_on: true
      patterns:
        - pattern_type: prebuilt
          pattern_name: email
          action: MASK

Env: OPENAI_API_KEY, AWS_BEARER_TOKEN_BEDROCK, DATABASE_URL. Start: litellm --config config.yaml --port 4000 --num_workers 2

curl -s http://localhost:4000/v1/messages -H "Authorization: Bearer sk-1234" -H "content-type: application/json" -d '{
  "model": "gpt", "max_tokens": 512,
  "messages": [{"role": "user", "content": "Use web search to find the latest stable CPython release, then answer with the version number only."}],
  "tools": [
    {"name": "Read", "description": "Reads a file from disk", "input_schema": {"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]}},
    {"type": "web_search_20250305", "name": "web_search", "max_uses": 1}
  ]}'

The same request with "model": "claude-bedrock-invoke" is the Bedrock cell. In the runs below each api_base pointed at a local recorder that logged the request and forwarded it to the real provider.

Before (1fa7e4a)

  1. The request above, model gpt: 200, text 3.13.0, an answer from memory. OpenAI received tools [Read]
  2. Model claude-bedrock-invoke: 200, "I don't have access to a web search tool". Bedrock received tools [Read]

After (7973ad5)

  1. Model gpt: 200, text 3.15.2, a searched answer. OpenAI received tools [Read, web_search_preview]
  2. Model claude-bedrock-invoke: 400 "Bedrock does not support Anthropic's server-side web search tool (tool type 'web_search_20250305' ...). To use web search with this model, enable LiteLLM's web search interception". No provider call

All cells

  • Guardrail configs: default-on content filter (returns the tools it was given); opt-in custom-code guardrails that allow everything, mask the Read description, or drop Read; a guardrail that returns tools: []
  • 63 cells per leg across /v1/messages, /v1/chat/completions, /v1/responses, streaming and not, Bedrock Invoke, Bedrock Converse, OpenAI, direct Anthropic
  • 53 cells sent the same bytes and got the same status on both legs. One more, a request with no guardrail, differs only in how many turns the model took. The 9 the change accounts for:
Config Cell Before (1fa7e4a) After (7973ad5)
default-on claude-anthropic/web-search 400, provider got [Read] 400, provider got [Read, web_search]
default-on gpt/web-search 200, provider got [Read] 200, provider got [Read, web_search_preview]
default-on claude-bedrock-invoke/web-search 200, provider got [Read] 400, no provider call
default-on claude-anthropic/web-search-stream 400, provider got [Read] 400, provider got [Read, web_search]
default-on gpt/web-search-stream 200, provider got [Read] 200, provider got [Read, web_search_preview]
default-on claude-bedrock-invoke/web-search-stream 200, provider got [Read] 400, no provider call
opt-in gpt/web-search/allow-all-code 200, provider got [Read] 200, provider got [Read, web_search_preview]
opt-in gpt/web-search/mask-read-tool 200, provider got [Read] 200, provider got [Read, web_search_preview]
opt-in gpt/web-search/drop-read-tool 200, provider got [] 200, provider got [web_search_preview]
  • A guardrail that returns tools: []: the provider got no tools on both legs, for OpenAI and for Bedrock (200 both)
  • The direct Anthropic cells answer 400 on both legs because the key has no credits; the recorded outbound body is what the table shows
  • 40-request burst with one worker killed, /v1/messages to an OpenAI model behind the content filter: web search forwarded on 0 of 8 before, 8 of 8 after; 40 x 200 with exactly one provider call each on both legs
  • 30-request burst with Postgres paused 6 s: 30 x 200 on both legs

Tests

  • Eight unit tests in tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py, including the legacy {"name": "web_search"} shape. Five fail at the merge base and all pass at the tip
  • At 7973ad5: tests/unit/llms/anthropic, base_llm, bedrock and proxy/guardrails, 16930 passed; three /v1/messages guardrail integration files, 80 passed

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Bedrock Claude with web_search behind a tools-returning guardrail: 200 without search becomes 400
    • Same 400 the request gets today without a guardrail
  • Guardrail vendors now receive one more tool in the tools list
    • A vendor that rejects unknown tool shapes could start failing
    • It is the shape web_fetch and bash already arrive in

Low

  • CrowdStrike AIDR's serializer sends its vendor only type of a hosted tool
    • Already true for web_fetch and bash; the request itself is unaffected
  • Direct Anthropic reply not proven, the key has no credits
  • Gemini deployment answers 404 on both legs
  • Guardrails that scan tool results only are still not shown tools

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

REVIEWER MUST KNOW BEFORE APPROVING

  • A /v1/messages request carrying web_search_20250305 behind any guardrail that receives tools. Before: the guardrail's tools input did not contain it. After: it does, as the Anthropic dict the caller sent
  • Same request, guardrail returns the tools it was given. Before: the provider did not receive web_search and the model answered without search. After: the provider receives it. On a Bedrock Claude deployment that turns a 200 into 400 "Bedrock does not support Anthropic's server-side web search tool", the answer the request gets with no guardrail. Approved by yucheng-berri on 2026-11-21
  • The same 200 to 400 applies on any provider litellm's param check rejects web_search_options for, when the request also carries a function tool and drop_params is not set: 400 UnsupportedParamsError: <provider> does not support parameters: ['web_search_options'], raised before any provider call. Run live on mistral, groq and deepinfra (proxy at 1fa7e4a and 7973ad5, content filter guardrail, stub upstream): before 200 with the provider receiving [Read] and no search, after 400. With no guardrail both commits answer that same 400, with drop_params: true both answer 200, and a request whose only tool is web search answers 400 on both. Approved by yucheng-berri on 2026-11-21
  • Same request, guardrail returns a tool list without web_search (for example an allow-list by name). Before and after: the provider does not receive it. After, that is the guardrail's decision and not a side effect

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no new actionable issues found.

Summary

This PR shows Anthropic web search tools to request guardrails and forwards or removes them according to the returned tool list.

  • Messages guardrails now decide whether Anthropic web search reaches the provider.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A["Messages request tools"] --> B["Translate other tools"]
  A --> C["Keep web search as sent"]
  B --> D["Guardrail tool list"]
  C --> D
  D --> E{"Guardrail returns web search?"}
  E -->|Yes| F["Forward web search as returned"]
  E -->|No| G["Remove web search"]
Loading

Reviews (5) · Last reviewed commit: "Merge branch 'main' into litellm_message..." · Reviewed by Greptile

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d3be73d. Configure here.

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

greptile-apps[bot]

This comment was marked as resolved.

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri
yucheng-berri marked this pull request as ready for review November 21, 2026 22:37
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@veria-ai

veria-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

3 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri
yucheng-berri merged commit 4c62c9a into main Nov 22, 2026
53 checks passed
@yucheng-berri
yucheng-berri deleted the litellm_messages_guardrail_sees_web_search branch November 22, 2026 01:02
mateo-berri pushed a commit to chaznm/litellm that referenced this pull request Nov 25, 2026
…erriAI#53376)

* fix(guardrails): show the /v1/messages web search tool to guardrails

* fix(guardrails): send a returned web search tool on as the guardrail returned it

(cherry picked from commit 4c62c9a)
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