Back to library

🧱Get Structured Output with Pydantic and JSON Schema

Replace markdown-fenced near-JSON and regex band-aids with a Pydantic schema the API enforces for you. By the end you can convert any ad-hoc prompt to typed output and measure how many parse failures you just deleted.

Applied14 drops~2-week path Β· 5–8 min/daytechnology

Phase 1Why 'Return JSON' Fails and What Schemas Fix

See why 'return JSON' fails and what schemas fix

4 drops
  1. 'Return only JSON' is a wish, not a contract

    6 min

    Prompt-only JSON works most of the time, which is exactly why it's dangerous in production.

  2. JSON mode fixes the syntax, not the shape

    6 min

    JSON mode guarantees parseable JSON but says nothing about whether the keys, types, or values match what your code expects.

  3. A Pydantic model is a contract the API enforces for you

    7 min

    Defining a Pydantic model and passing it to a structured-output API moves validation from your code to the API boundary β€” and gives the model a typed target to aim for.

  4. Validation retry turns a soft failure into a hard fix

    6 min

    When a model output fails Pydantic validation, modern libraries automatically re-prompt the model with the validation error β€” turning a hidden bug into a self-correcting loop.

Phase 2Wiring Pydantic Into Structured Output

Wire Pydantic into JSON mode and structured-output retry

5 drops
  1. Define your first Pydantic model and watch the schema appear

    7 min

    Field types and `Field(...)` constraints translate one-to-one into JSON Schema β€” what you write in Python is what the model sees.

  2. Make your first structured-output API call

    7 min

    One library call (`client.beta.chat.completions.parse(...)` or `client.messages.create(...)` with a tool) replaces all your manual parsing β€” you get a typed object back, not a string.

  3. Trigger a validation failure on purpose

    7 min

    You can't trust a system you've never seen fail β€” forcing a validation error is how you confirm the safety net actually catches things.

  4. Steer the model with field descriptions, not extra prompts

    6 min

    Field descriptions in the schema beat instructions in the prompt β€” the model sees the schema as part of the conversation, and a per-field description targets exactly the right place.

  5. Nest models for compound outputs without losing types

    7 min

    Pydantic models nest cleanly β€” a `list[ChildModel]` field gives you typed children, validated end-to-end, with no manual parsing per element.

Phase 3Composing Typed Output With Tools and Agents

Compose typed output with tools, agents, and downstream code

4 drops
  1. Your LLM-powered classifier is leaking strings into a downstream pipeline

    7 min

    Structured output is the cleanest place to put a typed contract between your LLM step and the rest of your system β€” without it, type drift propagates silently.

  2. Tool use is structured output wearing a different hat

    7 min

    When you give an LLM a tool to call, you're asking it to produce a typed structured output (the tool args) β€” every tool definition is a schema, and every schema can be a tool.

  3. Schemas double as your test suite for prompt drift

    7 min

    When you upgrade the model, change the prompt, or refactor the schema, the validation pass-rate is your regression test β€” drops in pass-rate localize regressions to the field that started failing.

  4. The schema is the spec β€” version it like one

    7 min

    When your Pydantic models go to production, they become a versioned API contract β€” between LLM and code, between teams, and between today's data and tomorrow's.

Phase 4Migrating a Real Prompt to Pydantic

Migrate one real prompt to Pydantic and measure the win

1 drop
  1. Convert one ad-hoc prompt to a Pydantic schema and measure error reduction

    18 min

    Convert one ad-hoc prompt to a Pydantic schema and measure error reduction

Frequently asked questions

What's the difference between JSON mode and structured output with Pydantic?
This is covered in the β€œGet Structured Output with Pydantic and JSON Schema” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Why does 'return only JSON' in a prompt still fail sometimes?
This is covered in the β€œGet Structured Output with Pydantic and JSON Schema” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Can I use Pydantic with Anthropic Claude or only with OpenAI?
This is covered in the β€œGet Structured Output with Pydantic and JSON Schema” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
What happens when the model output fails Pydantic validation?
This is covered in the β€œGet Structured Output with Pydantic and JSON Schema” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
How do tool calls relate to structured output schemas?
This is covered in the β€œGet Structured Output with Pydantic and JSON Schema” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.