WhoBuilt

Pydantic is STILL all you need

Sep 6, 2024· 15 min· 23K views·Watch on YouTube

Speaker

Jason Liu presents a talk for AI Engineer about using Pydantic and structured outputs to make language model applications behave more like reliable, typed software.

Why structured outputs matter

Liu argues that relying on strings and `json.loads` makes APIs brittle. Schemas and structured responses help preserve compatibility, composability, and reliability when software interacts with external systems, including LLMs.

A small, stable API

Liu says the core approach has barely changed: define a Pydantic response model and pass it to a client. The API offers `create`, `create_iterable`, and `create_partial` for regular, streaming, and partial results. He says the library reached version 1.0, supports several languages and model providers, and has seen about 40% month-over-month growth in Python.

Validators make outputs useful

Validators let developers define what counts as correct and return errors that a model can use to fix its output on a retry. Liu demonstrates checking that names are uppercase and verifying that receipt prices and quantities add up to the total. He says one retry is often enough for OpenAI and Anthropic models when the error messages are informative.

Structured models for RAG and extraction

Liu shows how response models can return follow-up questions, validate links, and turn a question into one or more search requests with dates, limits, and a selected source. Those requests can run in parallel before the results are passed to an answer function. He also describes models for spam classification, meeting summaries and action items, and extracting tables into a DataFrame.

Bring LLMs back to familiar programming

Liu frames structured output as a way to define the types, objects, and control flow that an application owns, while treating prompting and constrained sampling as implementation details. For enterprise RAG, he points to report generation; for agents, he describes generating workflows or DAGs for an execution engine instead of relying on an open-ended ReAct loop.

Takeaways

  • Define response schemas so downstream code can rely on typed data instead of parsing arbitrary text.
  • Use validators to encode business rules and provide clear error messages that models can act on during retries.
  • Model search requests as structured objects so an application can validate and parallelize retrieval.
  • Treat LLMs as producers of data structures, while keeping application control flow in ordinary code.

Mentioned in this talk

Search inside this talk

What was claimed 14

Show all 14 facts

Related talks