Best Practices for Building LLM Apps

Chapter 1 · Practice 2 of 4

Use schema-enforced structured output when code reads the response

From Prompt design and structured output

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:

FeatureValid JSONMatches your schema
JSON modeYesNo
Structured OutputsYesYes

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

TypeScript
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);