← Lessons

quiz vs the machine

Gold1410

Machine Learning

The Format Constraints And Schemas

Forcing structured output so downstream code can parse the answer reliably.

5 min read · core · beat Gold to climb

Answers a program can read

When a model output feeds code, free prose is fragile to parse. Format constraints tell the model to emit a fixed structure, such as JSON with named fields, so a program can read it without guessing.

Ways to constrain

  • Describe the schema in words, listing each field and its type.
  • Show a template with the exact shape and placeholders to fill.
  • Give an example of a valid filled output to imitate.
  • Use enforced decoding where the runtime restricts tokens to only valid continuations.

Why a schema beats prose

A named schema removes ambiguity about field names and nesting, makes outputs comparable across runs, and lets you validate them automatically. Validation catches a malformed answer before it breaks downstream code.

Common failures

Models may add chatter around the structure, invent extra fields, or drop required ones. Asking for only the structure with no preamble, plus a strict validator with a retry, keeps the pipeline robust.

Key idea

Format constraints make the model emit a fixed schema so code can parse it, and pairing a clear schema with validation and retries keeps structured outputs reliable across runs.

Check yourself

Answer to earn rating on the learn ladder.

1. Why constrain output to a named schema?

2. What does enforced decoding do?

3. How do you guard against malformed structured output?