Guardrails — Validate & Secure LLM Outputs
Guardrails is a Python framework for validating LLM inputs/outputs to detect risks and generate structured data. 6.6K+ GitHub stars. Pre-built validators, Pydantic models. Apache 2.0.
What it is
Guardrails is a Python framework that wraps LLM calls with validation logic. It intercepts inputs and outputs, runs them through configurable validators, and ensures the LLM produces structured, safe, and correct responses. The library ships with pre-built validators for common risks like PII detection, toxic content, JSON schema compliance, and hallucination checks. You define your output schema using Pydantic models, and Guardrails enforces it.
Developers building production LLM applications who need reliable, structured outputs benefit from Guardrails. It is particularly useful for applications where incorrect or unsafe LLM responses carry real consequences.
How it saves time or tokens
Without validation, developers manually inspect and retry LLM outputs when they fail to meet requirements. Guardrails automates this loop: if an output fails validation, it re-prompts the LLM with correction instructions. This automatic retry mechanism reduces manual debugging time and avoids wasted tokens on outputs that would be discarded anyway.
How to use
- Install Guardrails via pip and define your output schema with Pydantic
- Wrap your LLM call with a Guardrails guard that applies your chosen validators
- Call the guard instead of the LLM directly; it returns validated, structured output
Example
from guardrails import Guard
from guardrails.hub import DetectPII, ToxicLanguage
from pydantic import BaseModel
class UserResponse(BaseModel):
answer: str
confidence: float
guard = Guard().use_many(
DetectPII(pii_entities=['EMAIL', 'PHONE']),
ToxicLanguage(threshold=0.8)
)
result = guard(
model='gpt-4o',
messages=[{'role': 'user', 'content': 'Summarize this document.'}],
output_class=UserResponse
)
print(result.validated_output)
Related on TokRepo
- AI tools for security — Explore security-focused AI tooling
- AI tools for testing — Browse testing and validation frameworks
Common pitfalls
- Stacking too many validators increases latency and token cost per call; validate only what matters for your use case
- Some validators require external models or APIs (e.g., PII detection); check dependencies before deploying
- Automatic retries can loop indefinitely if the LLM consistently fails validation; always set a max retry count
Frequently Asked Questions
Guardrails Hub offers validators for PII detection, toxic language filtering, JSON schema compliance, regex matching, competitor mention detection, and more. You can also write custom validators as Python functions.
Yes. Guardrails wraps LLM calls and supports OpenAI, Anthropic, Cohere, and any provider accessible through LiteLLM. You pass the model name and Guardrails handles the API call with validation.
When an LLM output fails validation, Guardrails sends a corrective prompt explaining what went wrong and asks for a new response. You configure the maximum number of retries. Each retry consumes additional tokens.
Yes. Guards can validate both inputs and outputs. Input validation is useful for filtering user prompts that contain PII, injection attempts, or other risks before they reach the LLM.
Yes. Guardrails is designed for production with features like async support, streaming validation, telemetry, and caching. The Apache 2.0 license allows commercial use without restrictions.
Citations (3)
- Guardrails GitHub— Python framework for validating LLM inputs/outputs with pre-built validators
- Guardrails Documentation— Guardrails Hub for community validators
- Guardrails Hub— Pydantic-based output schema enforcement
Related on TokRepo
Source & Thanks
Created by Guardrails AI. Licensed under Apache 2.0. guardrails-ai/guardrails — 6,600+ GitHub stars
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.