Guardrails server
Guardrails are an enterprise feature. Reach out to the Opik team at sales@comet.com to enable them for your account.
The guardrails server is a standalone backend that runs the machine learning models behind Opik’s built-in guardrails. The Python SDK sends text to this service over HTTP, and the service returns whether each check passed along with detailed scores.
It powers these validation types:
- Topic — a zero-shot classifier (
facebook/bart-large-mnli) that checks whether text is on or off topic. - PII — named entity recognition (Microsoft Presidio with the spaCy
en_core_web_lgmodel) that detects sensitive information such as names, emails, and credit card numbers. - Prompt injection — a fine-tuned classifier (a Qwen LoRA adapter) that detects prompt injection and jailbreak attempts. Its model lives in a private Hugging Face repository, so the server needs a Hugging Face token (see Configuration).
- Custom guardrails — binary classifiers you train on your own labeled examples. The server loads them by name from its adapters directory.
The server is optional and disabled by default. You need it to run the built-in Topic, PII, or Prompt injection guardrails, or any custom guardrail. The LLM judge guardrail does not use this server: it calls the LLM provider configured in your workspace.
Installation and startup
Run alongside Opik (recommended)
If you self-host Opik with Docker Compose, start the guardrails backend by passing the --guardrails flag:
This enables the guardrails profile, starts the guardrails-backend container, and configures the Opik frontend to proxy requests to it. When Opik is started this way, the Python SDK reaches the server automatically with no extra configuration.
Run as a standalone container
You can also run the guardrails backend on its own, which is useful when you want to run it on a dedicated GPU node.
The image is not published to a registry, so build it locally from apps/opik-guardrails-backend (only needed once):
Run it with GPU acceleration:
Or run it CPU-only:
The server listens on port 5000. Confirm it is up with the health check:
To use GPU acceleration you need the NVIDIA Container Toolkit installed on the host. The models are baked into the image at build time, so no model download happens at startup.
Configuration
The server reads the following environment variables:
The prompt injection model is distributed privately by Comet. To use the prompt injection guardrail, reach out to
Comet to get access: they will provide the model repository, its base model, and a Hugging Face token to set as
HF_TOKEN.
The model is downloaded lazily the first time the prompt injection guardrail is used, so the server starts without these variables set; only the prompt injection guardrail fails (closed) until they are provided. Pass them to the container with -e HF_TOKEN=<token> -e OPIK_GUARDRAILS_PROMPT_INJECTION_MODEL=<repo> -e OPIK_GUARDRAILS_PROMPT_INJECTION_BASE_MODEL=<base-model>.
For example, to pin inference to the second GPU:
The service detects at startup whether CUDA is available. If no GPU is present, or the NVIDIA Container Toolkit is not configured, it automatically falls back to CPU mode without any configuration changes.
If you run the server at a custom address (for example, on a separate node), point the Python SDK at it with the OPIK_GUARDRAILS_URL_OVERRIDE environment variable:
The Topic guardrail accepts a maximum input of 1024 tokens, and both the Topic and PII guardrails support English only. Running on CPU works but is significantly slower; a GPU node is strongly recommended for production.
Calling the server
Most users never call the server directly — the Python SDK handles the request, response parsing, and trace logging for you. Call the API directly only when integrating from a language or environment without the SDK.
Send a POST request to /api/v1/guardrails/validations with the text to check and a list of validations to run:
Request fields:
text(required) — the text to validate.validations(required) — a list of validations to run. Each has atype(TOPICorPII) and aconfig:TOPICconfig:topics(required) — the list of topics to check against.threshold(default0.5) — confidence threshold for topic detection.mode(required) —restrictpasses when none of the topics match (content filtering);allowpasses when at least one matches (content classification).
PIIconfig:entities(optional) — entity types to detect. Defaults toIP_ADDRESS,PHONE_NUMBER,PERSON,MEDICAL_LICENSE,URL,EMAIL_ADDRESS,IBAN_CODE. See the Presidio supported entities for the full list.language(defaulten) — language of the text.threshold(default0.5) — confidence threshold for PII detection.
The response reports an overall validation_passed flag plus per-validation results with the scores that drove each decision: