When a program will use a model's answer, free-form prose isn't good enough. You need predictable, structured output.
Describe the Exact Shape
Show the structure you want, with field names, types and rules:
Extract these fields from the invoice. Reply with JSON only:
{"supplier": string, "invoice_date": "YYYY-MM-DD", "total": number, "currency": string}
Use null for anything not in the invoice.
Use the API's Structured-Output Features
Most major model APIs offer ways to enforce structure: JSON modes, structured outputs that follow a JSON schema, or tool (function) calling, where the model fills in arguments for a function you define. These are more reliable than instructions alone.
Always Validate
Treat model output like any untrusted input:
- parse it and catch errors;
- validate against a schema (for example with Pydantic or JSON Schema);
- check business rules — dates in range, totals that add up;
- retry with the validation error included, or send to a person, when it fails.
Design Tips
- Keep schemas as simple as the task allows.
- Use enums for fixed choices.
- Ask for
nullrather than guesses when information is missing. - For long documents, extract section by section.
Reasoning Before Answering
If a task needs reasoning, let the model think in a separate field or step before producing the final structured answer, rather than squeezing reasoning into the JSON values.