structured output — making the model return json3 / 9
schemas, pydantic, and validation — making the model return real data
Pydantic models — the schema is the contract
A Pydantic model is just a Python class where every attribute has a type hint. That class is the contract between your prompt and your downstream code:
from pydantic import BaseModel
class Ticket(BaseModel):
email: str
severity: int
summary: str
Three things this gives you that AI relies on:
- JSON schema for free.
Ticket.model_json_schema()returns the exact schema you can pass to the model in a tool definition orresponse_format. The model now knows what shape you expect. - One-line parse + validate.
Ticket.model_validate_json(raw)parses the JSON string, type-checks every field, raisesValidationErrorif anything is off. No manualisinstancechecks. - Real Python objects. After validation you write
ticket.email, notticket["email"]. Your IDE autocompletes the field names and catches typos at edit time.
Three field-type idioms you'll see constantly
from typing import Literal
from pydantic import BaseModel, Field
class Issue(BaseModel):
title: str
severity: Literal["low", "medium", "high"] # enum
score: int = Field(ge=0, le=10) # bounded int
tags: list[str] = [] # default empty list
Literal[...] for enums (the model is forced to pick one of three
strings). Field(ge=..., le=...) for numeric bounds. Default values
make a field optional. AI uses all three on every real schema.
Run the editor. We mimic Pydantic with a plain dict + assertions —
identical logic, just lower-level — so you can see what
model_validate_json does for you in one call.
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _