EMZETT.
Login

Structured outputs and JSON schema

In short: With structured outputs you force the model to answer in a fixed format, usually JSON following a schema. A program can then process the answer safely.

The problem

A language model produces text token by token. If you only ask nicely for JSON, sometimes a sentence comes first, a comma is missing or a field name is invented. For programs that is an error.

Solutions

  1. Describe it in the prompt and check: simple but unreliable. Always validate and ask again on errors.
  2. JSON mode: the model delivers valid JSON, but not necessarily the wanted schema.
  3. Schema-constrained decoding: while generating, only tokens that fit the schema are allowed (see Decoding). Fields and types are then correct.
  4. Tool calls (function calling): the model fills in the parameters of a function, see Tool use, agents and MCP.

Example schema

{ "type": "object",
  "properties": { "title": {"type":"string"}, "price": {"type":"number"} },
  "required": ["title","price"], "additionalProperties": false }

Notes

  • A valid format does not mean the content is right. Numbers and facts still need checking, see Hallucination (AI).
  • Simple schemas with clear field names and descriptions work best.

See also: Prompt injection