Guardrails AI is an output validation framework. The positioning is different from content classifiers or injection detectors: it’s designed to ensure that LLM outputs conform to specified constraints — structural, semantic, and safety-related — before they’re returned to users or downstream systems.
The core concept is validators: composable functions that check an LLM output against a constraint and either pass it, raise a failure, or trigger a reask (send the output back to the LLM with a correction request).
How it works
You define a guard that wraps your LLM call with a set of validators:
from guardrails import Guard, OnFailAction
from guardrails.hub import ToxicLanguage, DetectPII, ValidLength
guard = Guard().use(
ToxicLanguage(on_fail=OnFailAction.EXCEPTION),
DetectPII(pii_entities=["EMAIL", "PHONE"], on_fail=OnFailAction.FIX),
ValidLength(max=1000, on_fail=OnFailAction.REASK)
)
response = guard(
llm_api=openai.chat.completions.create,
model="gpt-4",
messages=[{"role": "user", "content": user_message}]
)
The validator behavior on failure is configurable:
EXCEPTION: Raise an errorFIX: Apply a correction (for validators that can auto-fix, like PII redaction)REASK: Send the output back to the LLM with a correction requestNOOP: Log but don’t blockFILTER: Remove the failing element (for array outputs)
The validator library
The Guardrails Hub contains 60+ validators (65 at time of writing). Security-relevant ones:
ToxicLanguage: Uses a classifier (Unitary’s toxic-bert or configurable alternatives) to detect toxic content. Not as specialized as Llama Guard but integrated into the validation pipeline.
DetectPII: Uses a NER model to identify and optionally redact PII entities (names, emails, phone numbers, SSNs, etc.). Useful for preventing the model from reproducing PII from training data or context.
RegexMatch / BanList: RegexMatch fails an output that does not match a required pattern; BanList fails one that contains a term from a configured ban list. Simple but surprisingly useful for preventing specific prohibited terms.
SensitiveTopic: Classifies the output topic and fails on a configurable list of sensitive topics.
The reask mechanism
The reask behavior is the most distinctive feature. When an output fails validation, instead of just blocking it, you can automatically resend to the LLM with a correction prompt:
Your previous response failed validation:
- The response contains PII (email address detected)
Please regenerate your response without including any personal email addresses.
This is useful for content constraints where you want the model to try again rather than failing hard. It adds latency (another LLM round trip) and has a success rate that varies with the constraint — structural constraints reask well; subtle content constraints reask with mixed success.
The pattern the documentation points toward is to reserve REASK for format constraints (response too long, wrong JSON structure) and use EXCEPTION for hard content constraints (PII, toxic content). Each reask is a second generation call, so its cost is roughly a full extra LLM round trip on top of the original request. That makes it a poor fit for a constraint that fails often, and it is worth measuring the reask rate on your own traffic before enabling it on a synchronous path.
Performance overhead
The documentation does not publish per-validator latency figures, and the numbers depend on your hardware and model choices, so treat the classes below as relative cost rather than benchmarks and measure them in your own environment:
- String-matching validators (RegexMatch, BanList, ValidLength): a local string or schema comparison with no model call.
- Model-based validators (ToxicLanguage, DetectPII): one classifier inference per check, priced by whatever hosts that model.
- LLM-based validators (anything using a judge model): a full additional LLM round trip per validation, plus its token cost.
For interactive applications, that ordering is what drives validator selection. String-matching and lightweight model validators compose cheaply; judge-model validators should be used sparingly in synchronous paths, and a failing reask adds another generation on top.
Where it fits in the stack
Guardrails AI is most valuable for:
- Structured output enforcement: Ensuring JSON outputs conform to a schema, required fields are present, optional fields have correct types. This is reliability as much as security.
- PII prevention: Catching and redacting personal information before it reaches users.
- Domain-specific constraints: Custom validators for your specific use case (“response must not mention competitor names,” “response must be in formal English”).
It’s not a replacement for:
- Prompt injection detection (use Lakera Guard or Rebuff; the detection tiers themselves are covered in how prompt injection detection works)
- Jailbreak resistance (model-level hardening)
- Comprehensive content moderation (use a dedicated classifier)
Improper Output Handling is one entry in a ten-category risk list, and the controls for the other nine are set out in the OWASP LLM Top 10 mitigation guide. Where Guardrails AI sits against NeMo Guardrails, Bedrock Guardrails and the cloud-native filters is mapped in LLM guardrail frameworks compared.
To rank Guardrails AI against the other reviewed tools on weights of your own choosing, use the interactive AI-Sec tool scorecard; for the wider field of scanners and red-team platforms, see best AI security testing tools 2026.
The composite picture of Guardrails AI alongside other tools in the AI security stack is at bestaisecuritytools.com.