Iteration Layer
Products
Use Cases
Resources
Pricing
Documentation navigation

Pydantic AI

Pydantic AI is a strong match for typed, production-grade agents. Iteration Layer pairs well with it because document extraction already returns structured JSON that can be validated, routed, and stored with Pydantic models.

Register Iteration Layer SDK calls as Pydantic AI tools, then validate the agent’s final output with Pydantic models.

Install

pip install pydantic-ai iterationlayer

Example Tool

from pydantic import BaseModel
from pydantic_ai import Agent, RunContext
from iterationlayer import IterationLayer


class Dependencies(BaseModel):
    iteration_layer_api_key: str


class InvoiceSummary(BaseModel):
    vendor: str
    total: str
    should_route_to_review: bool


agent = Agent(
    "openai:gpt-5.2",
    deps_type=Dependencies,
    output_type=InvoiceSummary,
    instructions="Extract invoice fields and route uncertain results to review.",
)


@agent.tool
async def extract_invoice(ctx: RunContext[Dependencies], file_url: str) -> dict:
    client = IterationLayer(api_key=ctx.deps.iteration_layer_api_key)

    return client.extract_document(
        files=[
            {
                "type": "url",
                "name": "invoice.pdf",
                "url": file_url,
            }
        ],
        schema={
            "fields": [
                {"name": "vendor", "type": "TEXT", "description": "Vendor name"},
                {"name": "total", "type": "CURRENCY_AMOUNT", "description": "Total amount"},
            ]
        },
    )

Why This Works Well

  • Iteration Layer handles messy file parsing, OCR, extraction, and generation.
  • Pydantic AI validates the agent result before your application trusts it.
  • Dependencies keep API keys outside prompts.
  • Typed outputs make review routing and persistence explicit.

Production Guidance

Use Iteration Layer confidence scores and citations as inputs to your output model. For sensitive workflows, make should_route_to_review true when required fields are missing or confidence is below your threshold.