Skip to content

Pydantic AI Shields

Pydantic AI Shields

Drop-in guardrails for your AI agents.

PyPI version PyPI Downloads GitHub Stars Python 3.10+ License: MIT CI Pydantic AI


Upstreamed to pydantic-ai-harness

Working together with the Pydantic team, we are moving this library's functionality into the official pydantic-ai-harness, where it lives as pydantic_ai_harness/guardrails.

For new projects, use the harness. InputGuard / OutputGuard / ToolGuard are upstream today as InputGuardrail / OutputGuardrail / ToolGuardrail (#249, #470); the content shields (#478) and cost budgets (#474) are in review. This library stays on PyPI and keeps working for everyone already depending on it. The README has the full mapping.

Python
from pydantic_ai import Agent
from pydantic_ai_harness.guardrails import InputGuardrail, ToolGuardrail

agent = Agent(
    "openai:gpt-5.4",
    capabilities=[
        InputGuardrail(guard=no_secrets),
        ToolGuardrail(guard=stay_in_the_workspace, result_guard=scrub_secrets),
    ],
)

Part of Pydantic Deep Agents

Pydantic AI Shields is one library in Pydantic Deep Agents — the open-source Claude Code alternative & Python agent framework. Use it standalone, or get every library wired together in a single create_deep_agent() call.

Pydantic AI Shields provides ready-to-use guardrail capabilities for Pydantic AI agents. Drop them into any agent for cost control, tool permissions, content safety, and more.

Quick Start

Python
from pydantic_ai import Agent
from pydantic_ai_shields import (
    CostTracking, PromptInjection, PiiDetector, SecretRedaction,
)

agent = Agent(
    "openai:gpt-4.1",
    capabilities=[
        CostTracking(budget_usd=5.0),
        PromptInjection(sensitivity="high"),
        PiiDetector(),
        SecretRedaction(),
    ],
)

Available Shields

Infrastructure Shields

Shield Description
CostTracking Token/USD tracking with budget enforcement
ToolGuard Block tools or require human approval
InputGuard Custom input validation (pluggable function)
OutputGuard Custom output validation (pluggable function)
AsyncGuardrail Run guard concurrently with LLM call

Content Shields

Shield Description
PromptInjection Detect prompt injection / jailbreak (6 categories, 3 sensitivity levels)
PiiDetector Detect PII — email, phone, SSN, credit card, IP
SecretRedaction Block API keys, tokens, credentials in output
BlockedKeywords Block forbidden keywords/phrases
NoRefusals Block LLM refusals ("I cannot help with that")

Next Steps