Structured output (JSON)

AI & LLM fundamentals · Practical · 12 min

A spreadsheet row, a database record or the next automation step needs fields, not a paragraph, and loose text breaks pipelines.

Often you do not want prose back. You want fields: the customer's name, the order number, a category, a yes/no. Text like "Sure! The order number appears to be 4471" is hard for the next step of an automation to use. JSON with fixed field names is easy.

There are three levels of reliability:

  • Ask in the prompt. "Reply only with JSON with the keys name and order_id." Combined with an example this usually works, but occasionally the model adds a sentence before the JSON or misses a field.
  • JSON mode. A setting that guarantees the reply is valid JSON, but not which fields it contains.
  • Structured outputs. You give the model a JSON Schema: a description, itself written in JSON, of the fields, their types (text, number, true/false, list) and which are required. The provider then forces the reply to match it. OpenAI, Anthropic and Google all support this, usually with a "strict" option.

Prefer structured outputs when your model offers them. Many automation tools expose this as an "output fields" or "structured data" option where you list the fields you want.

Design tips:

  • Use enums (a fixed list of allowed values) for categories, such as "billing", "bug", "other".
  • Allow an empty or "unknown" value where data may be missing, so the model is not pushed to invent something.
  • Give fields clear names and short descriptions; the model reads them.

Even with a schema, the values can still be wrong. A schema guarantees the shape, not the truth. Your code or automation should still handle refusals (the model declining to answer), empty values and errors.

Example

{
  "type": "object",
  "properties": {
    "customer_name": {"type": "string"},
    "order_id":      {"type": ["string", "null"], "description": "null if not mentioned"},
    "category":      {"type": "string", "enum": ["billing", "bug", "feature-request", "other"]},
    "urgent":        {"type": "boolean"}
  },
  "required": ["customer_name", "order_id", "category", "urgent"],
  "additionalProperties": false
}

Remember

  • Structured outputs force the reply to match a JSON Schema you provide
  • Use enums for categories and allow 'unknown' for missing data
  • A schema guarantees shape, not correctness

Watch out for

  • Making every field required with no 'unknown' or null option, which pressures the model to invent values.

Check yourself

  1. What does structured output with a strict schema guarantee?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: developers.openai.com/api/docs/guides/structured-outputs