HomeConnectPydantic AI

Integration Guide

Pydantic AI

Connect Pydantic AI to DashClaw and get your first governed action into /decisions in under 20 minutes.

Instance URL detected: https://your-dashclaw-instance.example.com

1

Deploy DashClaw

Get a running instance. Click the Vercel deploy button or run locally.

Already have an instance? Skip to Step 2.

2

Install the DashClaw Python SDK and Pydantic AI

Create a virtual environment and install the required packages.

Terminal

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install dashclaw pydantic-ai python-dotenv
3

Set environment variables

Create a .env file with your DashClaw connection details. No LLM API key required for the example.

.env

DASHCLAW_BASE_URL=https://your-dashclaw-instance.example.com
DASHCLAW_API_KEY=oc_live_...
4

Wrap the migration effect with run_governed

The helper binds the exact migration command to one persisted action, waits when required, claims one execution attempt, runs the callback, and reports the outcome.

main.py

import os
from dotenv import load_dotenv
from dashclaw import DashClaw

load_dotenv()

claw = DashClaw(
    base_url=os.environ["DASHCLAW_BASE_URL"],
    api_key=os.environ["DASHCLAW_API_KEY"],
    agent_id="pydantic-ai-db-agent",
)


def governed_run_migration(migration_name: str, production: bool) -> str:
    """Run a database migration. Governed by DashClaw policies."""
    risk = 75 if production else 25
    environment = "production" if production else "staging"
    goal = f"Run migration {migration_name} on {environment}"
    command = f"db-migrate --name {migration_name} --environment {environment}"

    return claw.run_governed(
        {"kind": "shell", "command": command},
        {
            "action_type": "database_migration",
            "declared_goal": goal,
            "risk_score": risk,
            "systems_touched": ["postgres"],
            "reversible": not production,
        },
        lambda: f"Migration {migration_name} applied successfully.",
    )

The callback runs only after DashClaw confirms protocol-1 execution authority for this action, agent, and scrubbed act.

5

Register the governed tool on your Pydantic AI agent

Pass the governed function in the tools list: Pydantic AI builds the tool schema from the signature and docstring; the governance loop runs on every model-initiated call.

agent.py

# Register the governed function as a Pydantic AI tool — the governance
# loop runs identically when the model invokes it.
from pydantic_ai import Agent

agent = Agent(
    'anthropic:claude-sonnet-4-6',
    tools=[governed_run_migration],
    instructions='You manage database migrations. Use the tool to run them.',
)

result = agent.run_sync('Apply the add-indexes migration to staging')
print(result.output)

# For tests: override the model with TestModel — it exercises the full
# agent loop, tools included, without an LLM API key.
#
#   from pydantic_ai.models.test import TestModel
#   with agent.override(model=TestModel()):
#       agent.run_sync('Apply the add-indexes migration to staging')
6

Run the governed example

Execute the example and watch the governance flow: a staging migration (allowed) and a production migration (may require approval).

Terminal

python main.py

No LLM API key needed: the example runs the governance flow directly. Only the DashClaw SDK calls are real.

7

Clone the full example

The complete runnable example is in the DashClaw repo.

Terminal

git clone https://github.com/ucsandman/DashClaw.git
cd DashClaw/examples/pydantic-ai-governed
pip install -r requirements.txt
python main.py

What success looks like

Go to /decisions: you should see two actions in the ledger with action_type 'database_migration', agent_id 'pydantic-ai-db-agent': the staging migration completed, and the production migration either completed (after approval) or pending.

Navigate to /decisions in your DashClaw instance. Your action should appear in the ledger within seconds of the agent run.

Governance as Code

guardrails.yml is a policy-as-code template. Import it into your instance — POST the YAML to /api/policies/import or call the Python SDK's import_policies — and DashClaw evaluates these rules at the guard step before any action executes.

guardrails.yml

version: 1
project: my-pydantic-ai-agent
description: >
  Governance policy for a Pydantic AI database agent.
  Production migrations require approval.
  Staging migrations are auto-allowed.

policies:
  - id: approve_production_migrations
    description: Production database migrations require human approval
    applies_to:
      action_types:
        - database_migration
      systems:
        - postgres
    rule:
      require: approval

  - id: allow_staging
    description: Staging migrations are low risk
    applies_to:
      action_types:
        - database_migration
    rule:
      allow: true