Structured Outputs

13 min read

Your code cannot read a friendly paragraph. It needs JSON with the right keys and types, every single time. Structured outputs make the model give data that matches a schema. Then the next line of your program can use it directly.

New words

JSON
A text format for data, made of keys and values.
Schema
A description of the exact shape the data must have.
Enum
A field that can take only a few fixed values.
Token
A word or a piece of a word that the model writes.
Constrained decoding
Blocking every token that would break the schema, at each step.
Pydantic
A Python library that checks data against a schema.
Validation
Checking that the data follows the shape and the rules.

What you will learn

  1. The problem: code needs data, models write text
  2. A ladder of approaches
  3. How constrained decoding works
  4. Try it: mask the bad tokens
  5. Trade-offs and failure modes
  6. Production practices
  7. In code
  8. In a real system
  9. Interview tip
  10. Key takeaways

10 quiz questions at the end.

Start this lesson

The full lesson, with interactive demos and the quiz, opens after you sign in with Google. It is free.

All lessons · Back to the roadmap