Structured output · 2026-11-08

You need reliable JSON from the model to feed a downstream service.

You currently ask for JSON in the prompt and hope for the best.

  1. AAdd "return ONLY valid JSON" in bold and hope compliance improves
  2. BAsk for JSON, then have a second model rewrite it as JSON
  3. CUse the API’s structured-output / JSON-schema mode so the shape is enforced
  4. DRaise max tokens and add a closing «end of JSON» marker to the prompt, so the object is never cut off and is always well-formed

Answer: C

Use the API’s structured-output / JSON-schema mode so the shape is enforced

Why

When JSON must be machine-parsable, constrain generation with the API’s schema/JSON mode rather than trusting a prose instruction. Enforcement at the decoding level guarantees the shape; a stern instruction only nudges probability.

More on this topic

More on Way of the Agent

Real situations from building agents - with the answer and why.

Solve a new one every day on the Path