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.
Installation agent prête
Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.
npx -y tokrepo@latest install f10382e2-4fa6-491f-8110-84f8397af129 --target codexÀ exécuter après confirmation du plan en dry-run.
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
Questions fréquentes
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.
Sources citées (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
En lien sur TokRepo
Source et remerciements
Created by Guardrails AI. Licensed under Apache 2.0. guardrails-ai/guardrails — 6,600+ GitHub stars
Fil de discussion
Actifs similaires
llm-guard — Secure LLM Inputs & Outputs
Harden LLM apps with a scanner pipeline for prompt injection, PII leakage, toxicity, and unsafe output. Install in minutes and gate requests in code.
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.
NeMo Guardrails — Programmable Safety for LLM Applications
NeMo Guardrails is an open-source toolkit by NVIDIA for adding programmable guardrails to LLM-based conversational systems. It provides input/output moderation, fact-checking, hallucination detection, jailbreak prevention, and dialog management via a declarative Colang configuration language.
DataFlow — LLM Data Prep Pipelines + WebUI
DataFlow is an LLM data-prep system with operator pipelines; install via uv, validate with `dataflow -v`, then launch `dataflow webui`.