GuidesAI development
Tutorial: getting reliable JSON from an AI model
1. Describe the expected result
import { z } from 'zod';
const Invoice = z.object({
supplier: z.string(),
total: z.number(),
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
});
2. Constrain the model
- 1Instruction and exampleThe format described in the prompt, with an example.
- 2Forced toolA tool whose parameters follow the schema, which the model must call.
- 3Structured outputThe provider's mode that forces the output to follow the schema, where it exists.
3. Always validate
const parsed = Invoice.safeParse(JSON.parse(modelText));
if (!parsed.success) {
// send parsed.error.issues back to the model and ask again, once or twice
} else {
save(parsed.data);
}
The traps
- Numbers written as text (“1,234.50 €”): state the expected format.
- Fields missing from the document: allow
nullrather than forcing an invention. - Dates: enforce a single format.
See also hallucinations and tool use.
Frequently asked questions
- Is writing “answer in JSON” in the prompt enough?
- Often, not always. Structured output or forced-tool mechanisms make deviations much rarer. Validation in code is still mandatory.
- What if validation fails?
- Send the model the exact error and ask again, once or twice. Beyond that, set the case aside for a human.
- Is valid JSON necessarily correct?
- No. Well-formed JSON can hold a wrong value. Critical values are cross-checked against your data.