Apply guardrail
The apply guardrail endpoint runs one of the gateway's guardrails on text you send and answers with the text as the guardrail left it, without calling any model. It is the quickest way to see what a guardrail does, and it lets systems that never call a model through the gateway still use its guardrails: a form handler that wants to scrub personal data before saving a ticket, a batch job that checks documents before indexing them, or a test that proves a new guardrail configuration behaves before anyone attaches it to a team.
A real-world scenario
A team runs a ticketing system that stores customer messages for years. Before a message is written to the database, the ticketing service sends it to POST /guardrails/apply_guardrail with the gateway's pii-mask guardrail. Phone numbers and email addresses come back as placeholders, the service stores the masked text, and the raw message never lands in long term storage. When the platform team later changes the guardrail to also mask card numbers, the ticketing service needs no change, because it only ever names the guardrail.
Run a guardrail on text
-
Find the guardrail name. In the Admin UI open Guardrails under AI Gateway and read the Name column, or call
GET /guardrails/list. -
Run it. In the Admin UI open the Test Playground tab, select the guardrail on the left, type the text under Input Text and click Test 1 guardrail, which makes exactly this call once per selected guardrail. Over the API:
curl -X POST http://localhost:4000/guardrails/apply_guardrail \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"guardrail_name": "pii-mask", "text": "Call me at 415-555-0100 or email jane.doe@example.com", "language": "en"}'Expected result for a masking guardrail:
{"response_text": "Call me at <PHONE_NUMBER> or email <EMAIL_ADDRESS>"}A guardrail that only allows or blocks returns the original text when it allows, and when it blocks the endpoint answers the same
400a model request would get, with the guardrail's reason in the message. -
Check the log. The call is recorded under the calling key with the route
/apply_guardrail, so it appears on the Logs page with the guardrail result, and it counts in the Guardrails Monitor figures. Guardrails that call a remote service count towards that service's usage exactly like checks on model traffic.
Request
| Method | Path | Summary |
|---|---|---|
| POST | /guardrails/apply_guardrail | Run one guardrail on a piece of text |
| Field | Required | Meaning |
|---|---|---|
guardrail_name | Yes | Name of a guardrail from the config file or the database. |
text | Yes | The text to check. |
input_type | No | request (the default) runs the guardrail's pre-call behaviour, response runs its post-call behaviour. |
language | No | The language of the text for Presidio, for example de. It replaces the guardrail's presidio_language for this call. Other guardrails ignore it. |
entities | No | The entity types Presidio looks for, for example ["EMAIL_ADDRESS", "PHONE_NUMBER"]. It applies only when the guardrail has no pii_entities_config, so it can never switch off an entity an admin set to BLOCK. Other guardrails ignore it. |
messages | No | Full chat messages, for guardrails that need the conversation. |
metadata | No | Extra data passed to the guardrail as request_data["metadata"] and recorded in the log. language and entities reach Presidio as metadata.guardrail_config. |
The route is also served at /apply_guardrail. Any key that may call models may use /guardrails/apply_guardrail, while the shorter /apply_guardrail path is for proxy admins only.
Response
response_text is the text after the guardrail ran. For masking guardrails it is the rewritten text, and for guardrails that only allow or block it is the original text.
Supported guardrails
Any guardrail whose integration supports a direct text check can be applied, including Presidio, Bedrock Guardrails, Lakera, the Spanway content filter, secret detection, custom code guardrails and generic guardrail API services. Guardrails that only work with full conversation context return an error that says so, and messages gives them the context when you have it.
Errors
| Status | Meaning | Fix |
|---|---|---|
400 Blocked entity detected: CREDIT_CARD by Guardrail: pii-mask. This entity is not allowed to be used in this request. | The guardrail blocked the text. The message carries the guardrail's reason. Other guardrails word it as Violated guardrail policy followed by the reason. | This is the expected answer for content the guardrail is meant to stop. To mask instead of block, change the entity's action on the guardrail. |
404 Guardrail 'pii-mask' not found. Please ensure the guardrail is configured in your Spanway proxy. | No guardrail has this name. | Copy the name from the Name column of the Guardrails page or from GET /guardrails/list. |
| 422 | guardrail_name or text is missing, or entities holds an unknown entity type. | Send both required fields and check the entity names against GET /guardrails/ui/add_guardrail_settings. |
| 401 | The key is missing or invalid. | Send a valid virtual key or the master key in Authorization: Bearer. |
401 Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. Route=/apply_guardrail. ... | A key that is not a proxy admin called the short /apply_guardrail path. | Call /guardrails/apply_guardrail instead. |
| 500 or a timeout | The guardrail's remote service failed or could not be reached. | Check the service address and credentials on the guardrail, and the gateway log for the provider's error. |
Logs
| Level | Line | What it means | Action |
|---|---|---|---|
| ERROR | apply_guardrail: async_failure_handler failed (or failure_handler submit failed, post_call_failure_hook failed) | The check itself failed and recording the failure also failed. The caller still got the error. | Check the logging integrations named in the stack trace. |
Related
- Guardrails for configuring guardrails and deciding which requests run them.
- Guardrails quick start for a walkthrough that uses this endpoint to test a new guardrail.
- PII masking with Presidio for the entity types and the
languagehint. - Validate and simulate policies to run several guardrails or a whole policy on sample inputs at once.
- Guardrails page for the Test Playground that calls this route.