Getting Started with Pydantic for LLM Outputs
This is a sample post. Real, original write-ups are being cooked up right now, brewing slowly, but properly.
Getting Started with Pydantic for LLM Outputs
An LLM that returns raw text is an LLM your code can't trust. Pydantic gives you a typed contract the model's output has to satisfy.
Quick answer: define a Pydantic model for the shape you want, pass its schema to the model (via tool calling or structured output mode), and validate the response before touching it in code.
The problem with parsing free text
Regex-parsing an LLM's prose is fragile — a slightly different phrasing breaks the parser. Structured output sidesteps this entirely.
A minimal example
from pydantic import BaseModel
class Invoice(BaseModel):
vendor: str
amount_cents: int
due_date: str
# Pass Invoice.model_json_schema() to the model as the expected output
# shape, then validate the response:
invoice = Invoice.model_validate_json(model_response)
Key Takeaways
- Structured output beats prompt-engineered text parsing every time.
- Validation errors are actionable — you know exactly what field failed.
- This pattern works the same for agent tool arguments and final answers.
Frequently Asked Questions
Does this only work with OpenAI? No — most major providers support structured/tool-call output now.
Related Reading
Related reading
What Is RAG, and Why It Matters
Retrieval-Augmented Generation in plain terms — what it actually does, when it's worth the added complexity, and when it isn't.
Postgres for AI Apps: A Practical Primer
Why Postgres — with pgvector — is often the only database an AI app actually needs, and where it starts to strain.
Building a Typed MDX Content Engine for a Zero-Cost Blog
How this site reads blog posts straight from the filesystem at build time — no database, no CMS, fully typed.