Pydantic AI Native
Follows Pydantic AI’s patterns and integrates seamlessly with your existing agents. Uses the same dependency injection and context patterns you already know.
Pydantic AI Native
Follows Pydantic AI’s patterns and integrates seamlessly with your existing agents. Uses the same dependency injection and context patterns you already know.
16 Built-in Guardrails
Ready-to-use guardrails for PII detection, prompt injection, secrets, toxicity, LLM-as-a-judge, tool validation, and more.
Auto-Retry with Feedback
When output guardrails fail, automatically retry with structured feedback so the LLM can self-correct.
Observable
Built-in OpenTelemetry support for Logfire and other observability platforms. Trace every guardrail execution.
from pydantic_ai import Agentfrom pydantic_ai_guardrails import GuardedAgentfrom pydantic_ai_guardrails.guardrails.input import pii_detector, prompt_injectionfrom pydantic_ai_guardrails.guardrails.output import secret_redaction
# Create an agent with guardrailsagent = Agent('openai:gpt-4o')guarded_agent = GuardedAgent( agent, input_guardrails=[pii_detector(), prompt_injection()], output_guardrails=[secret_redaction()],)
result = await guarded_agent.run('Your prompt here')Input Validation
Block harmful prompts before they reach your model. Detect PII, prompt injection attacks, toxicity, and more.
Output Protection
Validate model responses before returning to users. Catch leaked secrets, hallucinations, and policy violations.
Custom Guardrails
Write your own guardrails with simple async functions. Full access to dependencies and context.
Parallel Execution
Run multiple guardrails concurrently for better performance. Just set parallel=True.
Config-Based Setup
Load guardrails from JSON or YAML files. Compatible with OpenAI’s guardrails config format.
Testing Utilities
MockAgent, assertion helpers, and test case generators for thorough guardrail testing.
| Feature | pydantic-ai-guardrails | OpenAI Guardrails | LangChain Middleware |
|---|---|---|---|
| Pydantic AI native | Yes | No | No |
| Auto-retry with feedback | Yes | No | No |
| Parallel execution | Yes | Yes | Partial |
| Config file loading | Yes | Yes | No |
| LLM-as-a-judge | Yes | No | Custom |
| Tool validation | Yes | No | No |
| OpenTelemetry | Yes | No | Via LangSmith |
Installation
Get up and running with pip install. Install now
Quick Start
Build your first guarded agent in 5 minutes. Start building
Built-in Guardrails
Explore all 16 ready-to-use guardrails. View guardrails
Custom Guardrails
Learn to write your own validation logic. Create custom