Repository navigation
fix(responses): run the guardrails a Responses WebSocket frame selects - #48725
Conversation
Merging this PR will not alter performance1 warning
Comparing |
Codecov Report❌ Patch coverage is
📢 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
|
@litereview code-review |
[High impact] The PR appears safe to merge, with no new blocking issues found SummaryResponses WebSocket turns now run frame-selected guardrails and policies before reaching the provider
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
|
bugbot 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.
1 similar comment
… guardrail fields on blocks, merge guardrail lists by name
…on, honor picks beside a nested response, prefer a guardrail entry with settings
…ardrail test an enterprise license
…icks, skip moderation-only hooks on socket turns
…ata on socket turns
…frame_guardrails # Conflicts: # litellm/proxy/response_api_endpoints/endpoints.py
…frame_guardrails # Conflicts: # litellm/proxy/litellm_pre_call_utils.py
… a guardrail pick on socket turns
…the proxy has no database
…frame_guardrails # Conflicts: # litellm/proxy/response_api_endpoints/endpoints.py
|
bugbot run |
There was a problem hiding this comment.
✅ 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.
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:
How it solves it:
User Flow
Before: a guardrail picked per turn on the Responses WebSocket errors on OpenAI and is skipped elsewhere
pre_callguardrail withdefault_on: falsethat blocks "confidential"wss://litellm-domain/v1/responses?model=<OpenAI model>and sendsresponse.createwith"guardrails": ["<name>"]After: the same frame is blocked on every provider, and the socket stays usable
errorevent "Content blocked: keyword 'confidential' detected", and the next turn still answersRelevant issues
Refs #47927. Its handshake header and per-turn guardrail result stay open there
Design decisions
response, next to it, or asguardrailsinmetadataorlitellm_metadata, like an HTTP bodymetadataandlitellm_metadatacount, like on HTTP, and are stripped before forwarding, so OpenAI never sees a non-string metadata value. A string under those names inmetadatais the client's own OpenAI metadata, so it is forwarded as is, picks nothing, and needs no team permissionpoliciesapply to that turn only, never to later turns on the socketduring_callguardrails 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 thepre_calloutput and never aduring_callrewrite, like HTTP. That step runs only guardrails and content-enforcing hooks, like the socket'spre_callstep, so moderation-only loggers still skip socket turnsinclude_guardrail_responseis stripped and returns nothing for now, like HTTP streamingmodify_guardrailspermission, 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 authresponse, only a top-levelinput,instructions, ortoolsis rejected, since it would never reach the model. A top-levelguardrail_configorinclude_guardrail_responsethere picks nothing, so it is stripped and the turn goes onprovider_specific_fieldsHTTP returns, soguardrail_nameandguardrail_modestay readable instead of sitting in raw exception textstream_scope: streamingguardrails run on it andnon_streamingones 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 onepost_callguardrails on the socket are out of scopeLinear ticket
Part of LIT-10067
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
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-miniand Anthropicclaude-haiku-5-5, one socket per WS case. Setup and outputs are in the gistguardrailsguardrailsguardrailsguardrails, then socket closedpoliciespoliciespoliciesinclude_guardrail_responseinclude_guardrail_responseguardrailsguardrailsmetadatapickpoliciesduring_callpickduring_callguardrailspoliciespoliciesguardrailspolicies7 more WS OpenAI frame shapes that changed
modelguardrailsmodelguardrailsguardrailslitellm_metadatapicklitellm_metadataguardrailsguardrailsduring_callmask pickguardrails12 cases that answer the same before and after
guardrailspoliciespoliciespoliciesduring_callmask pickmetadata.guardrailsmetadata.guardrails, locked teamBefore (d884167)
WS OpenAI
guardrailsThe other 46 cases
WS Anthropic
guardrailsHTTP responses
guardrailsHTTP chat
policiesHTTP messages
policiesWS OpenAI, blocked then benign
WS Anthropic, benign then blocked
WS OpenAI
policiesWS Anthropic
policiesWS OpenAI nested frame
WS OpenAI
include_guardrail_responseWS OpenAI string
guardrailsWS OpenAI benign
HTTP responses
policiesHTTP chat benign
WS OpenAI dict
guardrailsWS OpenAI
litellm_metadatapickWS Anthropic
metadatapickWS Anthropic string
policiesWS Anthropic
during_callpickWS Anthropic default-on
during_callWS OpenAI masking pick
WS OpenAI stray
guardrailsWS OpenAI key guardrail + pick
Commands and output: full transcript
HTTP responses dict +
policiesHTTP chat dict +
policiesHTTP messages dict +
policiesWS Anthropic dict +
policiesWS OpenAI streaming-scoped pick
WS Anthropic streaming-scoped pick
HTTP responses stream, streaming-scoped
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
WS Anthropic nested + top-level pick
WS OpenAI pick, locked team
Commands and output: full transcript
HTTP responses pick, locked team
Commands and output: full transcript
WS OpenAI
during_callmask pickHTTP responses
during_callmask pickWS OpenAI policy pick, locked team
Commands and output: full transcript
HTTP chat dict pick, model guardrails
HTTP pass-through, key dict guardrail
Commands and output: full transcript
WS OpenAI string
metadata.guardrailsWS OpenAI string
metadata.guardrails, locked teamCommands and output: full transcript
After (18d9635)
WS OpenAI
guardrailsThe other 46 cases
WS Anthropic
guardrailsHTTP responses
guardrailsHTTP chat
policiesHTTP messages
policiesWS OpenAI, blocked then benign
WS Anthropic, benign then blocked
WS OpenAI
policiesWS Anthropic
policiesWS OpenAI nested frame
WS OpenAI
include_guardrail_responseWS OpenAI string
guardrailsWS OpenAI benign
HTTP responses
policiesHTTP chat benign
WS OpenAI dict
guardrailsWS OpenAI
litellm_metadatapickWS Anthropic
metadatapickWS Anthropic string
policiesWS Anthropic
during_callpickWS Anthropic default-on
during_callWS OpenAI masking pick
WS OpenAI stray
guardrailsWS OpenAI key guardrail + pick
Commands and output: full transcript
HTTP responses dict +
policiesHTTP chat dict +
policiesHTTP messages dict +
policiesWS Anthropic dict +
policiesWS OpenAI streaming-scoped pick
WS Anthropic streaming-scoped pick
HTTP responses stream, streaming-scoped
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
WS Anthropic nested + top-level pick
WS OpenAI pick, locked team
Commands and output: full transcript
HTTP responses pick, locked team
Commands and output: full transcript
WS OpenAI
during_callmask pickHTTP responses
during_callmask pickWS OpenAI policy pick, locked team
Commands and output: full transcript
HTTP chat dict pick, model guardrails
HTTP pass-through, key dict guardrail
Commands and output: full transcript
WS OpenAI string
metadata.guardrailsWS OpenAI string
metadata.guardrails, locked teamCommands and output: full transcript
Surprises
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
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 answeredUnknown 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 blocksconfidentialand a team withmodify_guardrails: falsegets the permission errorBackward incompatible
guardrails,policies,include_guardrail_response, orlitellm_metadataupstream. On base OpenAI answered each with a 400unknown_parameter. On head the frame completes or its guardrail runs, which is what the PR shipsguardrails: 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 textprovider_specific_fields. Only clients of the v1.109.0-dev.1 pre-release saw the old textRegression risk
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 sideDependency graph
Not verified
LAKERA_API_KEYorAPORIA_API_KEY_1andAPORIA_API_BASE_1to drive themType
🐛 Bug Fix
Caveats (if any)
Medium
during_callguardrails before going upstream, since a sent turn cannot be pulled backpoliciespick now fails, where Anthropic answered without the policyLow
non_streamingkey and team guardrails skip socket turns, as they did before fix(responses): run pre_call guardrails on every Responses WebSocket turn and bill each turn its own guardrail cost #47013include_guardrail_responsereturns no per-turn guardrail result yetguardrail_configon/v1/responses, as before this PRtagsnever select tag-attached policies on the socketinvalid_request_errorand no status, while HTTP sends a 403input,instructions, andtoolsosv-scanandimage-scanare red on main too, fixed by build(deps): bump langgraph and tornado and add dated scan ignores to clear osv-scan and image-scan #48302 and build(deps): bump filelock to clear osv-scan #48802Final Attestation
Bugbot summary