Guards
A guard is one check, configured. This page covers the five guard types Opik offers: what each one detects, what you configure on it, and what it needs in order to run.
You put guards into a policy in the Opik UI, and your application runs that policy by name. Configuration lives in Opik; your code only calls validate.

Quickstart
Create a policy in the UI with the guards you want, then build a guardrail from it and validate your text. A failing check raises, so the protected path only continues when validate returns.
The call is blocking, since the whole point is to stop your application before it returns a response you would not want a user to see.
See Policies for creating a policy, combining several of them, and enforcing one across every application in the workspace.
If you self-host Opik, the PII, Topic, prompt injection, and custom classifier guards need the guardrails server
running. Start it alongside the rest of the stack with ./opik.sh --guardrails, and see the
Guardrails server page for GPU and deployment options. The LLM judge guard does not need it.
The five guard types
Providers like OpenAI or Anthropic already block the most universal risks, such as harmful or malicious content. Opik’s guards cover the residual risks, which tend to be specific to your product and need configuring in more detail.
Each guard picks the method that gives the best balance of accuracy, latency, and cost for its problem — traditional NLP for personal data, small language models for topic and injection detection, a full LLM for nuanced judgement.
All five are configured the same way: switch the guard on in a policy and fill in its fields.
PII
Detects sensitive personal information such as names, addresses, email addresses, phone numbers, and credit card details.
What you configure
- Personal data to block — which categories to look for. The UI offers the categories that come up most often; the underlying detector supports a much longer list.
- Threshold — the confidence at or above which a detection counts. Lower is stricter.
The method used here leverages traditional NLP models for tokenization and named entity recognition.
Topic
Keeps inputs and outputs on topic.
What you configure
- Restricted topics — text must not match any of these.
- Allowed topics — text must match at least one of these.
- Threshold — the score at or above which a topic counts as detected.
You can set either list or both. Restricted topics are the common case: name what your application must stay away from.
This guardrail relies on a small language model, specifically a zero-shot classifier.
Prompt injection
Detects attempts to make your LLM ignore, override, or reveal its instructions, along with other malicious queries. Use it on user inputs to block jailbreaks and injection attacks before they reach your model.
What you configure
- Threshold — the injection probability at or above which the guard fails. Lower is stricter.
The model is fixed: this guard runs Opik Guard, a classifier we trained for this job. On a self-hosted deployment the server needs a Hugging Face token to fetch it — see the server configuration.
This guardrail relies on a small language model fine-tuned for prompt-injection detection.
LLM judge
Checks text against a rule you describe in plain language. Because a full LLM does the judging, it catches nuanced issues that heuristics and classifiers miss — giving regulated advice, drifting from your brand voice.
What you configure
- Instructions — the rule, in your own words. For example: “The text must not provide medical advice, diagnoses, or dosage recommendations.”
- Model — which model judges. It must be available through an LLM provider configured under AI Providers.
The judge runs inline and adds latency to every call it guards, so prefer a fast, small model from your provider’s
latest generation rather than a large reasoning model. Good choices are the low-latency tiers such as OpenAI
gpt-4o-mini, a Google Gemini Flash model, or an Anthropic Claude Haiku model.
This is the one guard that does not use the guardrails server. Its model call is recorded as a nested LLM span under the guardrail span, so you can inspect the exact prompt, response, and token usage in the trace.
This guardrail relies on a large language model, and is best suited to complex checks where accuracy matters more than latency.
Custom classifier
Runs a model you fine-tuned yourself — a check that answers one yes-or-no question specific to your product, such as whether a reply is off-brand. Opik trains it on your labeled examples and serves it, so there is nothing to deploy.
What you configure
- Model — which of your models to run. Only models that are ready can be selected.
- Threshold — the score at or above which the guard fails.
See Custom models for how to fine-tune one.
This guardrail relies on a small language model fine-tuned on your own labeled examples.
Handling a failure
A failing guardrail raises an exception, and your application code needs to handle it — retry, fall back to a safe default, or return an error, whichever suits the situation.
Guardrails fail closed. If a check cannot be evaluated (for example the guardrails server is unreachable, the
request times out, or an LLM judge provider call fails), validate raises opik.exceptions.GuardrailValidationError
rather than letting the text through. This exception subclasses GuardrailValidationFailed, so an existing
except GuardrailValidationFailed block also stops the request. Structure your code so the protected path only
proceeds when validate returns successfully.
Streaming responses and long inputs
Call guardrail.validate repeatedly to check a response chunk by chunk, or to check parts of a long input separately. The results are added as additional spans on the same trace.
Working with the results
When a guardrail fails on an LLM call, Opik adds the information to the trace automatically. You can filter the traces in your project down to those that failed a guardrail.

To see how often each guardrail is failing over time, open the Metrics section of the project.
Performance and limits
The guardrails backend uses a GPU automatically if one is available. For production use, running it on a GPU node is strongly recommended.
Current limits:
- Topic guardrail: the maximum input size is 1024 tokens
- Both Topic and PII guardrails support English language
Defining guards in code
Guards can also be listed inline instead of stored in a policy. This is handy for a quick experiment or a check that only one script cares about.
Guards defined this way are invisible to everyone else and can only be changed by editing and redeploying your application. Prefer a policy for anything that runs in production.
Each guard type has a matching class, taking the same settings the UI exposes:
Manual guardrails
For a check that fits none of the five guard types — a one-off heuristic, or a business rule that is genuinely just code — you can run it yourself and log the result to Opik directly. No guard class and no guardrails server are involved.
This is different from a custom model, which Opik fine-tunes and serves for you. Reach for a manual guardrail for a one-off heuristic; fine-tune a model once that same check needs to run fast and consistently at high volume.
Example: blocking competitor mentions
The guardrail spans appear alongside your traces just like the built-in guards, showing what was detected and whether the check passed or failed.
