apps/opik-documentation/documentation/fern/docs-v2/guardrails/guardrails.mdx
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.
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.
from opik.guardrails import Guardrail
from opik import exceptions
guardrail = Guardrail.from_stored_policies(names=["no-financial-advice"])
llm_response = "You should buy some NVIDIA stocks!"
try:
guardrail.validate(llm_response)
except exceptions.GuardrailValidationFailed as e:
print(e)
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.
<Tip> See [Policies](/guardrails/policies) for creating a policy, combining several of them, and enforcing one across every application in the workspace. </Tip> <Note> 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](/guardrails/server) page for GPU and deployment options. The LLM judge guard does not need it. </Note>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.
Detects sensitive personal information such as names, addresses, email addresses, phone numbers, and credit card details.
What you configure
The method used here leverages traditional NLP models for tokenization and named entity recognition.
Keeps inputs and outputs on topic.
What you configure
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.
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
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.
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
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.
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
See Custom models for how to fine-tune one.
This guardrail relies on a small language model fine-tuned on your own labeled examples.
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.
<Warning> 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. </Warning>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.
for chunk in response:
try:
guardrail.validate(chunk)
except exceptions.GuardrailValidationFailed as e:
print(e)
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.
<Frame> </Frame>To see how often each guardrail is failing over time, open the Metrics section of the project.
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:
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.
<Warning> Guards defined this way are invisible to everyone else and can only be changed by editing and redeploying your application. Prefer a [policy](/guardrails/policies) for anything that runs in production. </Warning>from opik.guardrails import Guardrail, PII, Topic
from opik import exceptions
guardrail = Guardrail(
guards=[
Topic(restricted_topics=["finance", "health"], threshold=0.9),
PII(blocked_entities=["CREDIT_CARD", "PERSON"]),
]
)
try:
guardrail.validate("You should buy some NVIDIA stocks!")
except exceptions.GuardrailValidationFailed as e:
print(e)
Each guard type has a matching class, taking the same settings the UI exposes:
| Guard | Class | Arguments |
|---|---|---|
| PII | PII | blocked_entities, threshold, language |
| Topic | Topic | restricted_topics, allowed_topics, threshold |
| Prompt injection | PromptInjection | threshold |
| LLM judge | LLMJudge | name, instructions, model |
| Custom classifier | CustomGuardrail | model_name, threshold |
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.
<Tip> This is different from a [custom model](/guardrails/custom-guardrails), 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. </Tip> <AccordionGroup> <Accordion title="Example: blocking competitor mentions"> ```python import opik import opik.opik_context import traceback# Brand mention detection
competitor_brands = [
"OpenAI",
"Anthropic",
"Google AI",
"Microsoft Copilot",
"Amazon Bedrock",
"Hugging Face",
"Mistral AI",
"Meta AI",
]
opik_client = opik.Opik()
def check_competitor_mentions(generation: str, trace_id: str) -> str:
# Start the guardrail span first so the duration is accurately captured
guardrail_span = opik_client.span(name="Guardrail", input={"generation": generation}, type="guardrail", trace_id=trace_id)
# Manual guardrail logic - detect competitor brand mentions
found_brands = []
for brand in competitor_brands:
if brand.lower() in generation.lower():
found_brands.append(brand)
# The key `guardrail_result` is required by Opik guardrails and must be either "passed" or "failed"
if found_brands:
guardrail_result = "failed"
output = {"guardrail_result": guardrail_result, "found_brands": found_brands}
else:
guardrail_result = "passed"
output = {"guardrail_result": guardrail_result}
# Log the spans
guardrail_span.end(output=output)
# Upload the guardrail data for project-level metrics
guardrail_data = {
"project_name": opik_client._project_name,
"entity_id": trace_id,
"secondary_id": guardrail_span.id,
"name": "TOPIC", # Supports either "TOPIC" or "PII"
"result": guardrail_result,
"config": {"blocked_brands": competitor_brands},
"details": output,
}
try:
opik_client.rest_client.guardrails.create_guardrails(guardrails=[guardrail_data])
except Exception as e:
traceback.print_exc()
return generation
@opik.track
def main():
good_generation = "You should use our AI platform for your machine learning projects!"
check_competitor_mentions(good_generation, opik.opik_context.get_current_trace_data().id)
bad_generation = "You might want to try OpenAI or Google AI for your project instead."
check_competitor_mentions(bad_generation, opik.opik_context.get_current_trace_data().id)
if __name__ == "__main__":
main()
```
The guardrail spans appear alongside your traces just like the built-in guards, showing what was detected and whether the check passed or failed.
<Frame> </Frame>