Guardrails AI: Add Safety Rails and Output Validation to Any LLM

Guardrails AI wraps LLM calls with validators for PII detection, toxicity, JSON schema, and custom rules — with automatic reask-and-retry when validation fails.

Mahmudul Haque Qudrati — CEO & ML Engineer at Pristren

Mahmudul Haque Qudrati

CEO & ML Engineer

May 12, 2026
7 min read
Guardrails AI: Add Safety Rails and Output Validation to Any LLM

The Problem With Raw LLM Outputs

One AI engineering post, weekly

LLM benchmarks, prompt techniques, and token-cost breakdowns — not another AI news roundup.

LLMs produce text that can be malformed, unsafe, or structurally incorrect. In production apps you need to handle: outputs that are not valid JSON when you expected JSON, responses that contain PII (names, emails, phone numbers), toxic language in customer-facing chatbots, and hallucinated content that violates business rules. Guardrails AI centralises this validation logic.

Installation

bash
pip install guardrails-ai
guardrails hub install hub://guardrails/valid_python  # Example hub validator

Team workspace

Ship faster with chat, meetings, and projects in one place — Zlyqor.

Start free

Core Concepts

  • Guard: wraps an LLM call with validators
  • Validator: a function that checks output and returns pass/fail
  • on_fail: action when validation fails (reask, refrain, fix, exception)
  • RAIL spec: XML-based schema (legacy) — Pydantic is preferred in 2026

Basic Guard With Pydantic

python
from guardrails import Guard
from guardrails.hub import TwoWords
from pydantic import BaseModel

class ProductSummary(BaseModel):
    name: str
    one_line_description: str

guard = Guard.from_pydantic(ProductSummary)

response = guard(
    openai.chat.completions.create,
    prompt="Describe vLLM in one line.",
    model="gpt-4o-mini",
    max_tokens=128,
)
print(response.validated_output)  # ProductSummary(name='vLLM', one_line_description='...')

If the model returns malformed output, Guard sends the Pydantic validation error back as a reask prompt and retries.

PII Detection Validator

bash
guardrails hub install hub://guardrails/detect_pii
python
from guardrails import Guard
from guardrails.hub import DetectPII

guard = Guard().use(DetectPII(pii_entities=["EMAIL_ADDRESS", "PHONE_NUMBER"], on_fail="refrain"))

response = guard(
    openai.chat.completions.create,
    prompt="Summarise this support ticket: John called from john@acme.com about billing.",
    model="gpt-4o-mini",
)
# If PII is detected in the output, response.validated_output = None (refrain)

Toxicity Validator

bash
guardrails hub install hub://guardrails/toxic_language
python
from guardrails.hub import ToxicLanguage

guard = Guard().use(ToxicLanguage(threshold=0.5, on_fail="exception"))

try:
    response = guard(openai.chat.completions.create, prompt="...", model="gpt-4o-mini")
except Exception as e:
    print(f"Toxicity detected: {e}")

Custom Validator

python
from guardrails.validators import Validator, register_validator, PassResult, FailResult

@register_validator(name="no-code-snippets", data_type="string")
class NoCodeSnippets(Validator):
    def validate(self, value: str, metadata: dict):
        if "```" in value or "def " in value:
            return FailResult(
                error_message="Response must not contain code snippets.",
                fix_value=value.split("```")[0].strip(),
            )
        return PassResult()

guard = Guard().use(NoCodeSnippets(on_fail="fix"))

on_fail Actions

ActionBehaviour
reaskSend validation error back to LLM and retry
refrainReturn None (silently skip bad output)
fixApply the fix_value from the validator
exceptionRaise ValidationError
noopLog but return the invalid output

Server Mode for Microservices

bash
guardrails start --config config.py --port 8000

Call the validation server from any language via HTTP — useful when your LLM app is not Python.

Full documentation at guardrailsai.com/docs and the Hub validator marketplace.

#guardrails-ai#safety#validation#output#production

// discussion

Comments

0/4000
Mahmudul Haque Qudrati — CEO & ML Engineer at Pristren

Mahmudul Haque Qudrati

CEO & ML Engineer

Visionary technologist, software engineer, and machine learning specialist. Founder and CEO of Pristren, directing engineering teams that ship production-grade AI/ML pipelines, mission-critical full-stack applications, and developer tooling. Creator of Zlyqor, the unified team workspace platform. Author of 540+ technical guides and benchmark research reports on large language models, agentic workflows, Model Context Protocol (MCP), and modern web stacks.

PristrenZlyqor