← Lessons

quiz vs the machine

Platinum1820

Machine Learning

The Structured JSON Output

Getting reliable machine readable objects from a model.

6 min read · advanced · beat Platinum to climb

Answers programs can read

When a model output feeds another program, free text is fragile. Structured output asks the model to return a strict object with named fields, so code can parse it directly. JSON is the common choice because it is widely supported and unambiguous.

How to get it reliably

  • Specify the schema with exact field names, types, and which are required.
  • Provide an example object so the model has a concrete target.
  • Forbid extra text like preamble or trailing commentary.
  • Use schema enforcement when the platform offers constrained decoding that guarantees valid JSON.

Why enforcement matters

Instructions alone often produce mostly valid output that occasionally breaks, such as a missing brace or a stray sentence. Constrained decoding restricts the model to tokens that keep the output schema valid, turning a usually correct format into a guaranteed one.

Handling failures

Even so, validate the parsed object against the schema and handle the rare malformed case. Retrying with the error message, or repairing common mistakes, makes the pipeline robust without manual intervention.

Key idea

Structured JSON output gives programs a parseable object, and pairing a clear schema with constrained decoding plus validation turns a usually valid format into a reliable one.

Check yourself

Answer to earn rating on the learn ladder.

1. Why request structured JSON output from a model?

2. What does constrained decoding provide?

3. Even with enforcement, you should still