Installation
pip install pydantic-ai-guardrailsor with uv:
uv add pydantic-ai-guardrailsRequires Python 3.11+ and Pydantic AI 2.66+. The core package has no extra dependencies: every built-in detector works out of the box with regex.
Optional extras
Section titled “Optional extras”| Extra | Installs | Enables |
|---|---|---|
pii-detection |
Presidio | DetectPII(backend='presidio') |
toxicity-detection |
Detoxify | DetectToxicity(backend='detoxify'), FilterToxicity(backend='detoxify') |
prompt-injection |
transformers with torch | DetectPromptInjection(backend='transformers') |
llm-guard |
llm-guard (Python < 3.13) | adapters.LlmGuardInput / LlmGuardOutput. Not part of all, since llm-guard pins its own transformers and presidio versions |
openai |
openai | adapters.OpenAIModerationInput / OpenAIModerationOutput |
json-schema |
jsonschema | Full JSON Schema validation in ValidateJson(schema=...) |
yaml |
PyYAML | YAML files in load_config() |
tokens |
tiktoken | Exact token counts in LimitInputLength(max_tokens=...) |
telemetry |
opentelemetry-sdk | An OTel SDK to export guardrail spans |
evals |
pydantic-evals | The pydantic_ai_guardrails.evals adapters |
all |
everything above except llm-guard |
pip install 'pydantic-ai-guardrails[pii-detection,json-schema,yaml]'ML threshold and score_threshold values must be in [0, 1]; anything else raises ValueError.
If you ask for a backend whose dependency is missing, the guardrail raises ImportError with the pip install command when you construct it, not halfway through a run.
Verify
Section titled “Verify”from pydantic_ai import Agentfrom pydantic_ai.models.test import TestModelfrom pydantic_ai_guardrails import DetectPII, InputBlocked
agent = Agent(TestModel(), capabilities=[DetectPII()])
try: agent.run_sync('my SSN is 123-45-6789')except InputBlocked as e: print(e.guardrail, e.reason) # DetectPII detected PII: ssn