requests. System Guardrails cover PII and secret detection without an external service.
Create and attach
The code defines a function named
evaluate that receives log and returns the verdict; the runner calls evaluate(log) by name, so the entry point cannot be renamed. Helper functions can be defined anywhere in the code.
Attach the check:
Full object: Python Guardrail. Output checks, including external ones, do not run on streaming responses. See Execution behavior.
Pass condition
The verdict is compared against the pass condition configured with the guardrail:
A failing guardrail blocks the request. The status depends on the surface:
400 on the Responses endpoint and on the OpenAI-compatible chat completions endpoint, and 422 on the Anthropic- and Google-compatible endpoints and on the Agents and Deployments surfaces. A guardrail that times out returns 408 on the AI Gateway endpoints and on Deployments; on the Agents surface a guardrail that cannot run, including a timeout, returns 502 guardrail_execution_failed. See Guardrail Error Response.
Requirements
Open Policy Agent
Open Policy Agent (OPA) is a general-purpose policy engine: rules are written in Rego, and the server returns a decision for the input it receives over HTTP. See the OPA documentation for installation and policy authoring. Start the server and load the policy. The data API listens on port 8181 by default:opa run --server accepts unauthenticated writes to /v1/policies and /v1/data, so a policy engine the sandbox can reach directly can also be rewritten by anyone who can reach it, including other guardrail authors, whose code runs in the same sandbox. Run it behind a proxy that accepts sandbox traffic only and forwards just the decision query (POST /v1/data/orq/guardrail/allowed), rejecting every other path and verb, in particular writes to /v1/policies and /v1/data, as described in Requirements; the guardrail calls the proxy, which holds the credential.
The policy denies the text the guardrail sends when it matches an injection pattern. is_string makes a query without text a denial, and default keeps the query answerable when the rule body is undefined.
orq/guardrail.rego
- Package and rule names map to the query path:
data.orq.guardrail.allowedis served at/v1/data/orq/guardrail/allowed. - The rule uses the OPA 1.0 syntax. On an earlier version, add
import future.keywords.if.
Python
False.
Any engine that answers over HTTP follows the same shape. Swap the URL, the request body, and the field the verdict is read from.
Third-party guardrail providers
Map the provider response onto the pass condition. A number guardrail passes at or above the threshold, so a risk score, where a higher value means riskier, needs normalizing to a 0 to 1 range and then inverting; a safety score, where a higher value means safer, needs only normalizing. Check the provider’s score direction before deciding whether to invert. Call the provider through the proxy described in Requirements, and swap the proxy host, the request body, and the score field for the provider’s own.Python
-1.0 failure value blocks when the provider cannot be reached. A provider that returns True or False in its own field can be returned directly with a Boolean pass condition.
Named providers
Each framework runs as a service, and the check reaches it over HTTP. Expand a framework for what runs where, the endpoint the proxy must expose, the guardrail code, and the pass condition to configure.Guardrails AI, self-hosted with its validation server
Guardrails AI, self-hosted with its validation server
What runs where. The framework validates text against a Guard, and the Guardrails server hosts guards behind an HTTP API. Run that server, or reuse one already deployed, wherever the Orq.ai sandbox can reach it, and the guardrail calls it.What the proxy must expose. The server’s own validate endpoint, with the guard name in the path and the text in Failure modes. A guard that cannot be reached, a response missing
llmOutput. The server answers with validationPassed, a boolean:The proxy holds the server’s credential, so the guardrail sends none. See the Guardrails server REST API for the full schema.Guardrail code. Set the pass condition to Boolean with True.
Python
validationPassed, and a validationPassed that is not a boolean all return False. The guard name is part of the URL, so a renamed guard turns every call into a blocking failure rather than a silent pass.Lakera, hosted Guard API
Lakera, hosted Guard API
What runs where. Nothing to deploy: the Guard API is hosted, and the proxy in front of it holds the API key. The project the request names selects the policy that screens it.Before the example runs. Two project settings decide whether the guardrail can block at all. With the Failure modes. A project that is not in
detect action, Lakera reports detections in breakdown and returns flagged as false for every request, so no guardrail can act on it. The project must screen with the enforce action. The role on the message decides which detectors apply, so model output is sent as assistant and input as user.See the Guard API reference for the full schema and the project settings.Guardrail code. Set the pass condition to Boolean with True.
Python
enforce mode, a missing flagged field, a non-boolean flagged, and an unreachable proxy all block. The check on action is what keeps the guardrail honest: without it, a project switched to detect would silently allow everything the provider detects.Test
- Test the code in the Playground panel of the guardrail.
- Click Publish.
- Attach the guardrail on the target surface.
- Send traffic and inspect the results in Traces.