Skip to content

fix(responses): run the guardrails a Responses WebSocket frame selects - #48725

Merged
mateo-berri merged 17 commits into
BerriAI:mainfrom
tavren27:litellm_responses_ws_frame_guardrails
Oct 30, 2026
Merged

mateo-berri merged 17 commits into
BerriAI:mainfrom
tavren27:litellm_responses_ws_frame_guardrails

Conversation

@tavren27

@tavren27 tavren27 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Clients on the Responses WebSocket can now pick guardrails and policies per turn on top of the key's and team's. Before, OpenAI rejected those turns with a 400 and other providers ran them unguarded. It also fixes a 500 when a guardrail with settings meets another guardrail list

Original

TLDR

Problem this solves:

  • WS frame guardrail picks never ran
  • A guardrail with settings next to another guardrail list answered 500

How it solves it:

  • Frame picks add to the socket's guardrails, run first, and need the team's guardrail permission
  • Guardrail-only fields never go upstream
  • Guardrail lists merge by name, keeping a guardrail's settings

User Flow

Before: a guardrail picked per turn on the Responses WebSocket errors on OpenAI and is skipped elsewhere

  1. The admin adds a pre_call guardrail with default_on: false that blocks "confidential"
  2. A developer opens wss://litellm-domain/v1/responses?model=<OpenAI model> and sends response.create with "guardrails": ["<name>"]
  3. With "confidential" in the input, they get 400 "Unknown parameter: 'guardrails'", and Anthropic models just answer
  4. A guardrail with settings next to another guardrail list answers 500

After: the same frame is blocked on every provider, and the socket stays usable

  1. The admin adds the same guardrail
  2. The developer sends the same frame
  3. They get an error event "Content blocked: keyword 'confidential' detected", and the next turn still answers
  4. The same requests run every guardrail once, with its settings

Relevant issues

Refs #47927. Its handshake header and per-turn guardrail result stay open there

Design decisions
  • A frame picks at the top level, inside a nested response, next to it, or as guardrails in metadata or litellm_metadata, like an HTTP body
  • A frame adds guardrails and never removes the key, team, or policy ones. Letting a frame opt out was the alternative, but a client could then skip an admin's guardrail. A picked policy's pipeline still decides its own steps, as on HTTP
  • Picks inside metadata and litellm_metadata count, like on HTTP, and are stripped before forwarding, so OpenAI never sees a non-string metadata value. A string under those names in metadata is the client's own OpenAI metadata, so it is forwarded as is, picks nothing, and needs no team permission
  • Frame policies apply to that turn only, never to later turns on the socket
  • during_call guardrails run before the turn is forwarded. Running them beside the upstream call like HTTP does was the alternative, but a socket turn cannot be pulled back once it is sent. The forwarded turn carries the pre_call output and never a during_call rewrite, like HTTP. That step runs only guardrails and content-enforcing hooks, like the socket's pre_call step, so moderation-only loggers still skip socket turns
  • include_guardrail_response is stripped and returns nothing for now, like HTTP streaming
  • A malformed pick fails the turn with an error naming the field, billed as a refused turn. Dropping it silently was the alternative, but the turn would then run unguarded
  • A frame that picks guardrails or policies, or turns global ones off, at either level or in metadata, needs the team's modify_guardrails permission, so a locked team gets the refusal an HTTP guardrail pick gets. When the team row is gone or the proxy has no database, the key's cached team metadata decides, as in HTTP auth
  • Next to a nested response, only a top-level input, instructions, or tools is rejected, since it would never reach the model. A top-level guardrail_config or include_guardrail_response there picks nothing, so it is stripped and the turn goes on
  • A guardrail block comes back with the same message and provider_specific_fields HTTP returns, so guardrail_name and guardrail_mode stay readable instead of sitting in raw exception text
  • A socket turn counts as streaming, like the pass-through WebSocket, so stream_scope: streaming guardrails run on it and non_streaming ones are skipped. Running both scopes, or no scope check, were the alternatives, but either runs a guardrail where its admin scoped it out. Key and team guardrails follow the same rule. They first ran on socket turns in fix(responses): run pre_call guardrails on every Responses WebSocket turn and bill each turn its own guardrail cost #47013, which only the v1.109.0-dev.1 pre-release ships, so no RC or stable release loses one
  • post_call guardrails on the socket are out of scope
  • Every guardrail list merge (key, team, project, policy, model, pass-through) names each guardrail once. Its first entry with settings wins over any plain name in either order, so a picked guardrail keeps its settings, and a guardrail with no settings entry keeps its first plain name

Linear ticket

Part of LIT-10067

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

Delays 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

Last updated: ba70a3a. QA ran on 18d9635. Changes since QA: no-database socket team lookup, merge of main. Proof remains valid because all 47 cases re-ran at 73aaf9e and ba70a3a with the same results (rows). Verification: unit and live no-database cases

One 2-worker proxy per leg, real OpenAI gpt-5.4-mini and Anthropic claude-haiku-5-5, one socket per WS case. Setup and outputs are in the gist

Case Before After
WS OpenAI guardrails 400 unknown guardrails blocked
WS Anthropic guardrails answers blocked
WS OpenAI, blocked then benign 400 unknown guardrails, then socket closed blocked, then answers
WS Anthropic, benign then blocked both answer answers, then blocked
WS OpenAI policies 400 unknown policies blocked
WS Anthropic policies answers blocked
WS OpenAI include_guardrail_response 400 unknown include_guardrail_response answers
WS OpenAI string guardrails 400 unknown guardrails error names the field
WS Anthropic metadata pick answers blocked
WS Anthropic string policies answers error names the field
WS Anthropic during_call pick answers blocked
WS Anthropic default-on during_call answers blocked
WS OpenAI key guardrail + pick blocked, then 400 unknown guardrails both blocked
HTTP responses, chat, messages dict + policies 500 unhashable dict 400 blocked
WS Anthropic dict + policies both answer answers, masked, then blocked
WS Anthropic streaming-scoped pick answers blocked
HTTP responses, chat, messages key dict guardrail 500 unhashable dict answers, masked
WS Anthropic key dict guardrail error, unhashable dict answers, masked
HTTP chat key policy + dict pick 500 unhashable dict 400 blocked
WS Anthropic nested + top-level pick answers blocked
WS OpenAI pick, locked team 400 unknown guardrails team may not pick
WS OpenAI policy pick, locked team 400 unknown policies team may not pick
HTTP chat dict pick, model guardrails 500 unhashable dict 400 blocked
HTTP pass-through, key dict guardrail 500 unhashable dict 400 blocked
7 more WS OpenAI frame shapes that changed
Case Before After
WS OpenAI nested frame 400 missing model blocked
WS OpenAI stray guardrails 400 missing model blocked
WS OpenAI dict guardrails 400 unknown guardrails blocked
WS OpenAI litellm_metadata pick 400 unknown litellm_metadata blocked
WS OpenAI streaming-scoped pick 400 unknown guardrails blocked
WS OpenAI masking pick 400 unknown guardrails answers, masked
WS OpenAI during_call mask pick 400 unknown guardrails answers
12 cases that answer the same before and after
Case Before After
HTTP responses guardrails 400 blocked 400 blocked
HTTP chat policies 400 blocked 400 blocked
HTTP messages policies 400 blocked 400 blocked
WS OpenAI benign answers answers
HTTP responses policies 400 blocked 400 blocked
HTTP chat benign 200 200
HTTP responses stream, streaming-scoped 400 blocked 400 blocked
WS Anthropic nested + top-level flag answers answers
HTTP responses pick, locked team 403 team may not pick 403 team may not pick
HTTP responses during_call mask pick 200 200
WS OpenAI string metadata.guardrails answers answers
WS OpenAI string metadata.guardrails, locked team answers answers

Before (d884167)

WS OpenAI guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output:
websockets 15.0.1 connecting ws://127.0.0.1:20004/v1/responses?model=gpt-mini

=== turn 1 >>> {"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}
<<< {
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "code": null,
    "message": "[ObjectParam] [guardrails] [unknown_parameter] Unknown parameter: 'guardrails'.",
    "param": null
  },
  "status": 400
}

=== client closing socket
The other 46 cases

WS Anthropic guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output: full output

HTTP responses guardrails

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output: full output

HTTP chat policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"policies":["confidential-words"]}'
  1. Output: full output

HTTP messages policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"claude-managed","max_tokens":64,"messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI, blocked then benign

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK."}'
  1. Output: full output

WS Anthropic, benign then blocked

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"Reply with the single word OK."}' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output: full output

WS OpenAI policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","policies":["confidential-words"]}'
  1. Output: full output

WS Anthropic policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI nested frame

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","response":{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}}'
  1. Output: full output

WS OpenAI include_guardrail_response

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK.","include_guardrail_response":true}'
  1. Output: full output

WS OpenAI string guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":"example-pre-call-guardrail"}'
  1. Output: full output

WS OpenAI benign

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK."}'
  1. Output: full output

HTTP responses policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","policies":["confidential-words"]}'
  1. Output: full output

HTTP chat benign

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"Reply with the single word OK."}],"policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI dict guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":[{"example-pre-call-guardrail":{"extra_body":{}}}]}'
  1. Output: full output

WS OpenAI litellm_metadata pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","litellm_metadata":{"guardrails":["example-pre-call-guardrail"]}}'
  1. Output: full output

WS Anthropic metadata pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","metadata":{"guardrails":["example-pre-call-guardrail"]}}'
  1. Output: full output

WS Anthropic string policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","policies":"confidential-words"}'
  1. Output: full output

WS Anthropic during_call pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is forbidden. Reply with the single word OK.","guardrails":["during-call-guardrail"]}'
  1. Output: full output

WS Anthropic default-on during_call

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is embargoed. Reply with the single word OK."}'
  1. Output: full output

WS OpenAI masking pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":["masking-guardrail"]}'
  1. Output: full output

WS OpenAI stray guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","guardrails":["example-pre-call-guardrail"],"response":{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK."}}'
  1. Output: full output

WS OpenAI key guardrail + pick

Commands and output: full transcript

HTTP responses dict + policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","litellm_metadata":{"guardrails":[{"masking-guardrail":{"extra_body":{}}}]},"policies":["confidential-words"]}'
  1. Output: full output

HTTP chat dict + policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"metadata":{"guardrails":[{"masking-guardrail":{"extra_body":{}}}]},"policies":["confidential-words"]}'
  1. Output: full output

HTTP messages dict + policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"claude-managed","max_tokens":64,"messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"litellm_metadata":{"guardrails":[{"masking-guardrail":{"extra_body":{}}}]},"policies":["confidential-words"]}'
  1. Output: full output

WS Anthropic dict + policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":[{"masking-guardrail":{"extra_body":{}}}],"policies":["confidential-words"]}' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","guardrails":[{"masking-guardrail":{"extra_body":{}}}],"policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI streaming-scoped pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is secretive. Reply with the single word OK.","guardrails":["streaming-guardrail"]}'
  1. Output: full output

WS Anthropic streaming-scoped pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is secretive. Reply with the single word OK.","guardrails":["streaming-guardrail"]}'
  1. Output: full output

HTTP responses stream, streaming-scoped

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is secretive. Reply with the single word OK.","stream":true,"guardrails":["streaming-guardrail"]}'
  1. Output: full output

HTTP responses key dict guardrail

Commands and output: full transcript

HTTP chat key dict guardrail

Commands and output: full transcript

HTTP messages key dict guardrail

Commands and output: full transcript

WS Anthropic key dict guardrail

Commands and output: full transcript

HTTP chat key policy + dict pick

Commands and output: full transcript

WS Anthropic nested + top-level flag

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","include_guardrail_response":true,"response":{"model":"claude-managed","input":"Reply with the single word OK."}}'
  1. Output: full output

WS Anthropic nested + top-level pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=claude-managed' '{"type":"response.create","metadata":{"guardrails":["example-pre-call-guardrail"]},"response":{"model":"claude-managed","input":"This is confidential. Reply with the single word OK."}}'
  1. Output: full output

WS OpenAI pick, locked team

Commands and output: full transcript

HTTP responses pick, locked team

Commands and output: full transcript

WS OpenAI during_call mask pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":["during-call-masking-guardrail"]}'
  1. Output: full output

HTTP responses during_call mask pick

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":["during-call-masking-guardrail"]}'
  1. Output: full output

WS OpenAI policy pick, locked team

Commands and output: full transcript

HTTP chat dict pick, model guardrails

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:20004/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini-model-guardrail","messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"guardrails":[{"example-pre-call-guardrail":{"extra_body":{}}}]}'
  1. Output: full output

HTTP pass-through, key dict guardrail

Commands and output: full transcript

WS OpenAI string metadata.guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:20004/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK.","metadata":{"guardrails":"example-pre-call-guardrail","trace":"t1"}}'
  1. Output: full output

WS OpenAI string metadata.guardrails, locked team

Commands and output: full transcript

After (18d9635)

WS OpenAI guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output:
websockets 15.0.1 connecting ws://127.0.0.1:28321/v1/responses?model=gpt-mini

=== turn 1 >>> {"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}
<<< {"type": "error", "error": {"type": "invalid_request_error", "message": "Content blocked: keyword 'confidential' detected", "provider_specific_fields": {"error": "Content blocked: keyword 'confidential' detected", "keyword": "confidential", "description": null, "guardrail_name": "example-pre-call-guardrail", "guardrail_mode": "pre_call"}}}

=== client closing socket
The other 46 cases

WS Anthropic guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output: full output

HTTP responses guardrails

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output: full output

HTTP chat policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"policies":["confidential-words"]}'
  1. Output: full output

HTTP messages policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"claude-managed","max_tokens":64,"messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI, blocked then benign

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK."}'
  1. Output: full output

WS Anthropic, benign then blocked

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"Reply with the single word OK."}' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}'
  1. Output: full output

WS OpenAI policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","policies":["confidential-words"]}'
  1. Output: full output

WS Anthropic policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI nested frame

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","response":{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":["example-pre-call-guardrail"]}}'
  1. Output: full output

WS OpenAI include_guardrail_response

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK.","include_guardrail_response":true}'
  1. Output: full output

WS OpenAI string guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":"example-pre-call-guardrail"}'
  1. Output: full output

WS OpenAI benign

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK."}'
  1. Output: full output

HTTP responses policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","policies":["confidential-words"]}'
  1. Output: full output

HTTP chat benign

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"Reply with the single word OK."}],"policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI dict guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","guardrails":[{"example-pre-call-guardrail":{"extra_body":{}}}]}'
  1. Output: full output

WS OpenAI litellm_metadata pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","litellm_metadata":{"guardrails":["example-pre-call-guardrail"]}}'
  1. Output: full output

WS Anthropic metadata pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","metadata":{"guardrails":["example-pre-call-guardrail"]}}'
  1. Output: full output

WS Anthropic string policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","policies":"confidential-words"}'
  1. Output: full output

WS Anthropic during_call pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is forbidden. Reply with the single word OK.","guardrails":["during-call-guardrail"]}'
  1. Output: full output

WS Anthropic default-on during_call

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is embargoed. Reply with the single word OK."}'
  1. Output: full output

WS OpenAI masking pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":["masking-guardrail"]}'
  1. Output: full output

WS OpenAI stray guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","guardrails":["example-pre-call-guardrail"],"response":{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK."}}'
  1. Output: full output

WS OpenAI key guardrail + pick

Commands and output: full transcript

HTTP responses dict + policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is confidential. Reply with the single word OK.","litellm_metadata":{"guardrails":[{"masking-guardrail":{"extra_body":{}}}]},"policies":["confidential-words"]}'
  1. Output: full output

HTTP chat dict + policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"metadata":{"guardrails":[{"masking-guardrail":{"extra_body":{}}}]},"policies":["confidential-words"]}'
  1. Output: full output

HTTP messages dict + policies

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"claude-managed","max_tokens":64,"messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"litellm_metadata":{"guardrails":[{"masking-guardrail":{"extra_body":{}}}]},"policies":["confidential-words"]}'
  1. Output: full output

WS Anthropic dict + policies

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":[{"masking-guardrail":{"extra_body":{}}}],"policies":["confidential-words"]}' '{"type":"response.create","model":"claude-managed","input":"This is confidential. Reply with the single word OK.","guardrails":[{"masking-guardrail":{"extra_body":{}}}],"policies":["confidential-words"]}'
  1. Output: full output

WS OpenAI streaming-scoped pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"This is secretive. Reply with the single word OK.","guardrails":["streaming-guardrail"]}'
  1. Output: full output

WS Anthropic streaming-scoped pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","model":"claude-managed","input":"This is secretive. Reply with the single word OK.","guardrails":["streaming-guardrail"]}'
  1. Output: full output

HTTP responses stream, streaming-scoped

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"This is secretive. Reply with the single word OK.","stream":true,"guardrails":["streaming-guardrail"]}'
  1. Output: full output

HTTP responses key dict guardrail

Commands and output: full transcript

HTTP chat key dict guardrail

Commands and output: full transcript

HTTP messages key dict guardrail

Commands and output: full transcript

WS Anthropic key dict guardrail

Commands and output: full transcript

HTTP chat key policy + dict pick

Commands and output: full transcript

WS Anthropic nested + top-level flag

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","include_guardrail_response":true,"response":{"model":"claude-managed","input":"Reply with the single word OK."}}'
  1. Output: full output

WS Anthropic nested + top-level pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=claude-managed' '{"type":"response.create","metadata":{"guardrails":["example-pre-call-guardrail"]},"response":{"model":"claude-managed","input":"This is confidential. Reply with the single word OK."}}'
  1. Output: full output

WS OpenAI pick, locked team

Commands and output: full transcript

HTTP responses pick, locked team

Commands and output: full transcript

WS OpenAI during_call mask pick

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":["during-call-masking-guardrail"]}'
  1. Output: full output

HTTP responses during_call mask pick

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"Repeat this sentence back exactly, nothing else: my password is hunter2","guardrails":["during-call-masking-guardrail"]}'
  1. Output: full output

WS OpenAI policy pick, locked team

Commands and output: full transcript

HTTP chat dict pick, model guardrails

  1. Run:
curl -sS -w '\nHTTP %{http_code}\n' http://127.0.0.1:28321/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini-model-guardrail","messages":[{"role":"user","content":"This is confidential. Reply with the single word OK."}],"guardrails":[{"example-pre-call-guardrail":{"extra_body":{}}}]}'
  1. Output: full output

HTTP pass-through, key dict guardrail

Commands and output: full transcript

WS OpenAI string metadata.guardrails

  1. Run:
python ws_client.py --url 'ws://127.0.0.1:28321/v1/responses?model=gpt-mini' '{"type":"response.create","model":"gpt-mini","input":"Reply with the single word OK.","metadata":{"guardrails":"example-pre-call-guardrail","trace":"t1"}}'
  1. Output: full output

WS OpenAI string metadata.guardrails, locked team

Commands and output: full transcript

Surprises
  • OpenAI closes the socket after its 400. Not this PR
  • Socket team refusal is invalid_request_error, HTTP's a 403. This PR
/live-pr-risk: one Medium found at 18d9635 and fixed in 3878bda, nothing else breaks

Base d884167 and head 18d9635, then head 3878bda, each ran as a 2-worker proxy on its own Postgres. Every model call went through a forwarding recorder in front of api.openai.com (TLS for the socket relay), so the request litellm sent upstream is diffed too, not only what the client saw. 26 cases ran on each tree, then 5 more on a proxy with no database and custom auth. At 3878bda every case and every upstream request matches 18d9635 except the fixed one. Rows are in the gist. After merging main, base 73aaf9e and head ba70a3a re-ran every case with the same results and upstream requests (rows)

Breaking

  • A team key on a proxy with no database had every socket frame that picks a guardrail rejected with No DB Connected. HTTP runs the same pick. The socket's team lookup let the no-database error escape, while HTTP treats it as no team. Base forwarded the selector to OpenAI, which answered Unknown parameter: 'guardrails'. Fixed in 3878bda: the socket uses the key's own team metadata when there is no database, the same fallback it already used for a deleted team row. At 3878bda the pick blocks confidential and a team with modify_guardrails: false gets the permission error

Backward incompatible

  • Socket frames no longer forward guardrails, policies, include_guardrail_response, or litellm_metadata upstream. On base OpenAI answered each with a 400 unknown_parameter. On head the frame completes or its guardrail runs, which is what the PR ships
  • A malformed selector (guardrails: 5, guardrails: "", policies: 5) now gets a proxy validation error and the frame is not sent. Base sent it and got the upstream 400. Both are errors with different text
  • The socket's rejection event now carries the guardrail's own detail and provider_specific_fields. Only clients of the v1.109.0-dev.1 pre-release saw the old text

Regression risk

  • Passthrough routes with a legacy v1 Lakera or Aporia callback now run it like every other route does. Base failed those calls with a KeyError before reaching the vendor. Not driven, see below
  • When the same guardrail is picked twice with different settings, the first entry with settings wins, in key, team, project, request, model order. Covered by unit tests, not live
  • Dict-form picks are not added to the applied-guardrails response header. Low, unchanged from base for HTTP
  • Under custom auth without custom_auth_run_common_checks, HTTP skips the team permission check, so a locked team's HTTP pick answers 200 while its socket pick is refused. Low, the socket is the stricter side

Dependency graph

Dependent Entry State
Key and team string guardrails chat live, same
Model-level guardrails chat live, same
Policies plus key guardrails chat live, same
Duplicate pick chat live, same
Streaming responses /v1/responses live, same
Anthropic messages /v1/messages live, same
Passthrough guardrails pass-through live, same
Unknown guardrail pick chat, socket live, both 200
Socket frame picks socket live, intended diff
Socket hostile frames socket live, intended diff
Socket key guardrails socket live, same
Socket auth socket live, both 403
No-database team pick socket live, fixed
Merge of main all live, same
Legacy suites pytest 83 files, none caused
v1 Lakera, Aporia pass-through untested

Not verified

  • Legacy v1 Lakera and Aporia on a passthrough route. There is no LAKERA_API_KEY or APORIA_API_KEY_1 and APORIA_API_BASE_1 to drive them
  • The tests/e2e guardrail, passthrough and MCP guardrail suites, which need a deployed proxy. The live rig drove the same routes instead
  • 34 integration reds at 18d9635. Seven pass after the merge of main, five also fail on current main, and the other 22 are load timeouts, 15 of which pass alone

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Socket turns wait on during_call guardrails before going upstream, since a sent turn cannot be pulled back
  • A locked team's socket policies pick now fails, where Anthropic answered without the policy

Low

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
Bugbot summary

[!NOTE]
Medium Risk
Changes security-sensitive guardrail selection, team permission enforcement, and WebSocket request shaping across many proxy paths; behavior shifts are intentional but broad enough to warrant careful regression review.

Overview
Responses WebSocket response.create frames now follow the HTTP /v1/responses guardrail path: per-turn picks for guardrails, guardrail_config, and policies are merged onto the socket’s admitted list (never shrinking it), run through pre_call and during_call hooks with streaming classification, and are checked for team modify_guardrails permission. Proxy-only selectors are stripped before upstream forwarding; rejections return structured error events (including provider_specific_fields) and bill failed turns like HTTP.

Guardrail list merging is centralized in merged_guardrail_entries, which deduplicates by guardrail name and keeps the first {name: settings} entry over plain strings. That replaces set/dict.fromkeys merging across key/team/project metadata, policy engine resolution, model-level guardrails, and pass-through collection—fixing 500 “unhashable dict” when settings objects sit in the same list as other sources.

Auth refactors the guardrail-modification gate into public requests_guardrail_modification / guardrail_modification_check / require_guardrail_modification_permission, with JSON-string metadata coercion so guardrail toggles cannot bypass the check via smuggled string metadata. during_call_hook gains a guardrails_only flag so socket turns only run content-enforcing moderation hooks alongside guardrails.

Reviewed by Cursor Bugbot for commit ba70a3a. Bugbot is set up for automated code reviews on this repo. Configure here.

@codspeed

codspeed Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing tavren27:litellm_responses_ws_frame_guardrails (ba70a3a) with main (a44e44b)

Open in CodSpeed

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.48387% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/proxy/response_api_endpoints/endpoints.py 94.25% 5 Missing ⚠️
litellm/integrations/custom_guardrail.py 90.47% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

…got wrong

A frame whose guardrails or guardrail_config has the wrong shape now gets
an error event naming the field, instead of pydantic's raw validation dump
@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge, with no new blocking issues found

Summary

Responses WebSocket turns now run frame-selected guardrails and policies before reaching the provider

  • Responses WebSocket turns run selected guardrails before they reach a provider.
  • Guardrail lists keep each name once without dropping its settings.

No new actionable issues were found

Reviews (10) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

…bSocket frame sends, and run during_call guardrails

A frame can pick guardrails as a list of names or name-to-config dicts, inside metadata or litellm_metadata, with policies validated as a list of strings. during_call guardrails now run before the turn goes upstream, and selection fields sent next to a nested response are rejected instead of dropped
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

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.

Stale Bugbot comment from a previous run.

…frame_guardrails

# Conflicts:
#	tests/unit/proxy/response_api_endpoints/test_endpoints.py
…drails

A policy's guardrails were merged with dict.fromkeys, so a configured guardrail entry ({name: {extra_body}}) in key metadata raised unhashable type: 'dict' and every request naming policies failed with a 500 on HTTP and on the Responses WebSocket. The merge now keeps existing entries and skips policy guardrails already selected by name. Also types the WebSocket test guardrail's hooks.
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

1 similar comment
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

… guardrail fields on blocks, merge guardrail lists by name
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…frame_guardrails

# Conflicts:
#	litellm/proxy/response_api_endpoints/endpoints.py
…frame_guardrails

# Conflicts:
#	litellm/proxy/litellm_pre_call_utils.py
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…frame_guardrails

# Conflicts:
#	litellm/proxy/response_api_endpoints/endpoints.py
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit ba70a3a. Configure here.

@mateo-berri mateo-berri 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.

LGTM

@mateo-berri
mateo-berri merged commit 7e37c20 into BerriAI:main Oct 30, 2026
144 of 146 checks passed
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