Chapter 1 · Practice 2 of 4
Use schema-enforced structured output when code reads the response
Why it matters
If your code parses the answer, free-form text is a bug waiting to happen. Ask for JSON that matches a schema, and use your provider's structured output feature when it has one.
The OpenAI docs draw a clear line between the two modes:
| Feature | Valid JSON | Matches your schema |
|---|---|---|
| JSON mode | Yes | No |
| Structured Outputs | Yes | Yes |
Then validate on your side anyway. A refusal, or a response cut off by the token limit, may not follow the schema even when the feature is on.
Design the schema so the model has an easy job:
- Keep it small and flat.
- Use enums for categories.
- Allow null for fields that can be missing, so the model is not pushed to invent a value.
Example
const Ticket = z.object({
category: z.enum(['billing', 'bug', 'account', 'other']),
summary: z.string().max(200),
orderId: z.string().nullable(),
});
const parsed = Ticket.safeParse(JSON.parse(modelText));
if (!parsed.success) return handleBadOutput(parsed.error);