Pass-through guardrails
Guardrails on a custom pass-through endpoint check the request before it is forwarded and the response before it reaches the client. On a pass-through route a guardrail can only block: a guardrail that masks or rewrites text, such as a PII masking guardrail, still decides whether to block, but its masked text is not written back, so the request and the response pass unchanged when it lets them through. Because a pass-through route forwards an arbitrary payload, the gateway cannot guess where the user text is, so no guardrail runs on a pass-through route until you configure one on that route. Once any guardrail is configured there, the guardrails attached to the calling key, team and organization also run. Field-level targeting tells each guardrail which fields to read, so ids, URLs and scores next to the text do not trigger false positives.
A real-world scenario
A platform team forwards an internal rerank service through the custom endpoint /v1/rerank-internal. The documents sent for reranking are customer tickets and sometimes contain phone numbers and card numbers. The team attaches a pii-block guardrail that blocks card numbers to the endpoint and points it at query and documents[*].text on the way in and results[*].text on the way out. A ticket with a card number is refused before it leaves the gateway, while the document ids and relevance scores are never inspected, so they cause no false blocks.
Before you start
The guardrail must exist in the top-level guardrails section of the config file or on the Guardrails page of the Admin UI, see guardrails. Pre-call guardrails (mode: pre_call) can block the request, and post-call guardrails (mode: post_call) can block the response. Use a guardrail that blocks on what it finds. Masking is only applied on the unified endpoints such as /v1/chat/completions.
guardrails:
- guardrail_name: pii-block
spanway_params:
guardrail: presidio
mode: pre_call
pii_entities_config:
CREDIT_CARD: BLOCK
Steps
-
In the Admin UI open
/ui/models-and-endpoints, click the Pass-Through Endpoints tab and either click + Add Pass-Through Endpoint or open an existing endpoint and click Edit Settings. Under Select Guardrails pickpii-block. For the guardrail, fill Request Fields (pre_call) withqueryanddocuments[*].text, using the + query and + documents[*] shortcuts, and Response Fields (post_call) withresults[*].text. Save with Add Pass-Through Endpoint or Save Changes. The gateway does not check the guardrail name when you save, so pick it from the list rather than typing it. -
Or set the same over the API with a proxy admin key. The simple form, a guardrail name with
null, runs the guardrail on the whole payload.curl -X POST http://localhost:4000/config/pass_through_endpoint/9d2f6b1e-4c7a-4f0e-9a1b-2c3d4e5f6a7b \-H "Authorization: Bearer $SPANWAY_MASTER_KEY" \-H "Content-Type: application/json" \-d '{"guardrails": {"pii-block": {"request_fields": ["query", "documents[*].text"], "response_fields": ["results[*].text"]}}}'Or in the config file.
general_settings:pass_through_endpoints:- path: /v1/rerank-internaltarget: https://rerank.internal.example.com/v1/rerankguardrails:pii-block:request_fields: ["query", "documents[*].text"]response_fields: ["results[*].text"] -
Send a request with a card number in a document.
curl -X POST http://localhost:4000/v1/rerank-internal \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"query": "refund for a duplicate charge", "documents": [{"id": "t-4411", "text": "My card 4111 1111 1111 1111 was charged twice."}]}' -
Open
/ui/logs, click the row and open the Guardrails section of the detail drawer to see which guardrail ran, on which fields, and that it blocked.
Expected result
The request is refused with 400 and the guardrail's message, for example Blocked entity detected: CREDIT_CARD by Guardrail: pii-block, and nothing is forwarded. A request without a card number reaches the upstream unchanged. The endpoint detail view shows the guardrail under Guardrails with its request and response fields, or Uses entire payload when no fields were set.
| Setting | Applies to | Meaning |
|---|---|---|
request_fields | pre_call | Fields of the request body the guardrail reads. |
response_fields | post_call | Fields of the response body the guardrail reads. |
Supported expressions are plain keys such as query, nested keys such as input.text, and array wildcards such as documents[*].text or messages[*].content, which collect a field from every element of an array. The values of all matched fields are joined into one text for the guardrail. When a setting is left out, the guardrail reads the entire payload for that direction.
Target the fields that carry free text written by people or returned by a model, and leave out structural fields such as ids, model names and scores. For OpenAI-style bodies that is usually messages[*].content, for rerank style bodies query and documents[*].text, and for search style responses results[*].text or results[*].snippet.
Guardrail settings apply to custom endpoints only. For the built-in provider routes such as /anthropic or /vertex_ai, prefer the unified endpoints when you need guardrails, because the gateway knows the structure of those requests and runs every configured guardrail automatically. The apply guardrail endpoint is an alternative when you want to check text yourself before calling a pass-through route.
Errors
| Status | Message | What it means and how to fix it |
|---|---|---|
400 | The guardrail's own violation message, for example Violated guardrail policy | A pre-call guardrail blocked the request, or a post-call guardrail blocked the response. The payload was not forwarded, or the response was not returned. |
422 | A validation error on guardrails | The value is not a mapping of guardrail name to settings. Use {"name": null} or {"name": {"request_fields": [...]}}. |
500 | The guardrail provider's error | The guardrail service failed. Check the provider's credentials and the gateway logs. |
Logs
Guardrail runs on pass-through routes are logged with the same guardrail_information block as on the unified endpoints, which the Logs page shows in the detail drawer, and a block writes the WARNING pass_through_endpoint: request blocked by guardrail - <message>. When none of the configured request_fields or response_fields matches anything in the body, the guardrail is skipped for that direction and only a DEBUG line, PassThroughEndpointHandler: No text to check, skipping guardrail, says so. A typo in an expression therefore turns the check off without an error, so test each expression against a real payload and confirm in the detail drawer that the guardrail ran. The gateway logs only the number of characters it extracted, never the text.
Related
Pass-through endpoints explains custom endpoints and the Admin UI tab. Guardrails covers every guardrail provider and mode, PII masking covers the masking guardrails, and guardrail policies attaches guardrails to keys, teams and organizations.