DEV Community

Anton Illarionov
Anton Illarionov

Posted on

PydanticAI + ODEI: Type-Safe AI Agents with Constitutional Validation

PydanticAI + ODEI: Type-Safe Agent Actions with Constitutional Validation

PydanticAI brings type safety to AI agents. ODEI brings constitutional governance. Together, they make the most reliable agent stack in 2026.

Why Type Safety + Constitutional Validation

PydanticAI validates that agent outputs match your schema. ODEI validates that agent actions are constitutionally safe. Different layers, both essential.

Layer What It Prevents
Pydantic schemas Wrong data types, missing fields
ODEI guardrails Unauthorized actions, duplicates, hallucinated refs

Integration

from pydantic import BaseModel
from pydantic_ai import Agent
import requests

class TransferResult(BaseModel):
    success: bool
    tx_hash: str
    amount: float

ODEI_TOKEN = "your-token"

def constitutional_check(action: str, severity: str = "medium") -> bool:
    r = requests.post(
        "https://api.odei.ai/api/v2/guardrail/check",
        headers={"Authorization": f"Bearer {ODEI_TOKEN}"},
        json={"action": action, "severity": severity}
    ).json()
    return r["verdict"] == "APPROVED"

agent = Agent(
    "openai:gpt-4",
    result_type=TransferResult,  # Pydantic type validation
)

@agent.tool_plain
def execute_transfer(amount: float, wallet: str) -> TransferResult:
    # Constitutional check BEFORE execution
    if not constitutional_check(f"transfer {amount} USDC to {wallet}", "high"):
        return TransferResult(success=False, tx_hash="", amount=0)

    # Pydantic validated execution
    return do_transfer(amount, wallet)
Enter fullscreen mode Exit fullscreen mode

The Combined Defense

  1. Pydantic validates: Is the data well-formed?
  2. ODEI validates: Is the action safe?

Neither alone is sufficient. Together they catch both data errors and governance violations.

Production Stack

For the most reliable autonomous agents in 2026:

  • PydanticAI for structured outputs and type safety
  • ODEI for constitutional validation and persistent memory
  • Neo4j (via ODEI) for graph-native persistent world model

Resources

Top comments (0)