WorkflowsApr 8, 2026·2 min read

Guardrails AI — Validate LLM Outputs in Production

Add validation and guardrails to any LLM output. Guardrails AI checks for hallucination, toxicity, PII leakage, and format compliance with 50+ built-in validators.

AG
Agent Toolkit · Community
Quick Use

Use it first, then decide how deep to go

This block should tell both the user and the agent what to copy, install, and apply first.

pip install guardrails-ai
guardrails hub install hub://guardrails/regex_match
from guardrails import Guard
from guardrails.hub import RegexMatch

guard = Guard().use(RegexMatch(regex=r"^\d{3}-\d{2}-\d{4}$"))

result = guard.validate("123-45-6789")
print(result.validation_passed)  # True

result = guard.validate("not-a-ssn")
print(result.validation_passed)  # False

What is Guardrails AI?

Guardrails AI is a framework for adding validation, safety checks, and structural constraints to LLM outputs. It provides 50+ pre-built validators from the Guardrails Hub — covering hallucination detection, PII filtering, toxicity checking, format validation, and more. Wrap any LLM call with a Guard to automatically validate and fix outputs before they reach users.

Answer-Ready: Guardrails AI validates LLM outputs with 50+ validators from Guardrails Hub. Checks hallucination, PII, toxicity, and format compliance. Auto-retries on validation failure. Works with OpenAI, Claude, any LLM. Production-ready with Guardrails Server. 4k+ GitHub stars.

Best for: AI teams deploying LLMs in production needing output safety. Works with: OpenAI, Anthropic Claude, LangChain, any LLM. Setup time: Under 5 minutes.

Core Features

1. Guardrails Hub (50+ Validators)

Category Validators
Safety ToxicLanguage, NSFWText, ProfanityFree
Privacy DetectPII, AnonymizePII
Accuracy FactualConsistency, NoHallucination
Format ValidJSON, ValidURL, RegexMatch
Quality ReadingLevel, Conciseness, Relevancy
Code ValidSQL, ValidPython, BugFreePython

2. LLM Integration

from guardrails import Guard
from guardrails.hub import ToxicLanguage, DetectPII

guard = Guard().use_many(
    ToxicLanguage(on_fail="filter"),
    DetectPII(on_fail="fix"),
)

# Wrap any LLM call
result = guard(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Summarize this customer complaint"}],
)
print(result.validated_output)  # PII removed, toxicity filtered

3. Auto-Retry on Failure

guard = Guard().use(ValidJSON(on_fail="reask"))

# If LLM returns invalid JSON, automatically retries with corrective prompt
result = guard(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Return user data as JSON"}],
    max_reasks=3,
)

4. Structured Output

from pydantic import BaseModel

class UserProfile(BaseModel):
    name: str
    age: int
    email: str

guard = Guard.for_pydantic(output_class=UserProfile)
result = guard(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Extract user info from: John, 30, john@example.com"}],
)
print(result.validated_output)  # UserProfile(name="John", age=30, ...)

5. Guardrails Server

# Deploy as API server for production
guardrails start --config guard_config.py
# POST /guards/{guard_name}/validate

FAQ

Q: Does it work with Claude? A: Yes, pass model="anthropic/claude-sonnet-4-20250514" to the guard call.

Q: What happens when validation fails? A: Configurable per validator — filter (remove), fix (correct), reask (retry with LLM), or raise (throw error).

Q: Can I write custom validators? A: Yes, extend the Validator base class. Custom validators can use LLMs, APIs, or rule-based logic.

🙏

Source & Thanks

Created by Guardrails AI. Licensed under Apache 2.0.

guardrails-ai/guardrails — 4k+ stars

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets