Structure & Data

Structured Output

Constrain the model's output to conform to a JSON Schema (or similar typed shape).

Problem

When the model is asked to emit JSON via natural-language instructions alone, the output is close but not quite right in inventive ways: smart quotes instead of straight ones, a stray sentence of explanation before the opening brace, a trailing comma, an extra field the schema does not allow. Strict parsers reject this; permissive parsers smuggle bugs forward. Writing post-hoc fixers turns into a tar pit of regular expressions chasing each new failure mode, and the application picks up a class of "flaky model" bugs that are really shape bugs the team has no clean way to prevent at decode time.

Solution

Define a JSON Schema (or Pydantic / Zod / equivalent). Pass it to the model via the provider's structured-output mode. Validate the output. Reject and retry on validation failure. Cap retries.

When to use

  • Downstream code consumes typed data and free-form text would break parsers.
  • A JSON Schema or equivalent typed shape can be defined for the output.
  • The provider supports structured-output mode or function calling.

Open the full interactive page

Diagram, neighbourhood map, code examples, related patterns and full provenance.

Related