Skip to content

Installation

Terminal window
pip install pydantic-ai-guardrails

or with uv:

Terminal window
uv add pydantic-ai-guardrails

Requires 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.

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
Terminal window
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.

from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel
from 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