Skip to main content
An external guardrail blocks a generation on a decision made outside Orq.ai: a policy engine (OPA, Cedar, OpenFGA), a third-party moderation API, or an internal compliance service. The decision runs as a Python Guardrail that calls the service with 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: 422 on the Responses endpoint and on the Agents and Deployments surfaces, and 400 on the OpenAI-compatible chat completions endpoint. On the AI Gateway endpoints a guardrail that times out returns 408; on the Agents surface a guardrail that cannot run, including a timeout, returns 502 guardrail_execution_failed. See Guardrail Error Response.
A guardrail that fails to execute blocks the request. Code that raises an exception, times out, or cannot reach the network ends the run with an execution error instead of passing the traffic. Catch request errors in the code and return a denial, so the caller sees the guardrail’s reason rather than a failed execution.

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.allowed is served at /v1/data/orq/guardrail/allowed.
  • The rule uses the OPA 1.0 syntax. On an earlier version, add import future.keywords.if.
Guardrail code, calling the policy engine through the authenticated proxy:
Python
Pass condition: Boolean with True. The generation is blocked when the policy returns 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
The proxy attaches the provider’s API key, so no credential lives in the guardrail code. Pass condition: Number with a threshold above zero, so the -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.

Test

  1. Test the code in the Playground panel of the guardrail.
  2. Click Publish.
  3. Attach the guardrail on the target surface.
  4. Send traffic and inspect the results in Traces.