← TinyGPT · docs · devlog · roadmap · speedup
source: docs/recipes/cookbook-pydantic-ai.md · view on GitHub ↗

Cookbook - TinyGPT with Pydantic AI

What you get:

What this is not: automatic correctness. Pydantic validates shape; your eval still has to measure whether the values are right.

1. Pick The Specialist

Structured output works best when the model was trained on the schema family you care about. Function-calling and constrained-output data are the closest starting point:

export TINYGPT_MODEL=/tmp/tinygpt-structured-specialist.tinygpt

2. Serve TinyGPT

tinygpt serve "$TINYGPT_MODEL" --host 127.0.0.1 --port 8080

3. Configure Pydantic AI

Pydantic AI’s OpenAIProvider accepts a custom base_url, so TinyGPT can be used like any other OpenAI-compatible server:

from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

class TicketRoute(BaseModel):
    team: str
    priority: int
    summary: str

model = OpenAIChatModel(
    "tinygpt",
    provider=OpenAIProvider(
        base_url="http://127.0.0.1:8080/v1",
        api_key="not-needed",
    ),
)

agent = Agent(model, output_type=TicketRoute)
result = agent.run_sync(
    "Route this ticket: customer cannot log in after SSO migration."
)
print(result.output)

Runnable example:

examples/pydantic-ai-tinygpt/run.sh

4. Benchmark

Measure schema compliance and semantic accuracy separately:

tinygpt run-bench \
  --model "$TINYGPT_MODEL" \
  --tasks json-schema-routing \
  --limit 50 \
  --out docs/artifacts/pydantic-ai-specialist-bench.jsonl

Record:

ModelValid schemaCorrect routeMean latency
TinyGPT specialistfill after runfill after runfill after run
General baselinefill after runfill after runfill after run

Honest Limitations

See also: smolagents and personal code specialist.