Skip to content

Pydantic AI Guardrails

Production-ready guardrails for Pydantic AI agents. Validate inputs, protect outputs, and build safer AI applications.

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 Agent
from pydantic_ai_guardrails import GuardedAgent
from pydantic_ai_guardrails.guardrails.input import pii_detector, prompt_injection
from pydantic_ai_guardrails.guardrails.output import secret_redaction
# Create an agent with guardrails
agent = 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.

Featurepydantic-ai-guardrailsOpenAI GuardrailsLangChain Middleware
Pydantic AI nativeYesNoNo
Auto-retry with feedbackYesNoNo
Parallel executionYesYesPartial
Config file loadingYesYesNo
LLM-as-a-judgeYesNoCustom
Tool validationYesNoNo
OpenTelemetryYesNoVia 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