Best practice L4 · Multi-AI systems informational

Get Structured Output You Can Trust, Not Just JSON-Shaped Text

What it is

A practice for getting machine-readable output: define the exact schema you need, ask Claude to produce only that, and then validate the result against the schema in your code — treating the model's output as untrusted input to be checked, not as guaranteed-valid data.

trust but verifyDefine schemaAsk for itValidateHandle misses
Shape it, then validate it — don't assume the shape held.

Why it works

Even when output is usually well-formed, 'usually' breaks a pipeline: an extra sentence of preamble, a trailing comment, a missing field, and your parser throws in production. Defining the schema up front reduces variance, and validating on your side turns the rare bad response into a caught, handleable case instead of a crash three layers downstream. It's the same discipline as validating any external input — the model is a helpful but non-deterministic source.

When to use it

Anything programmatic — extraction, classification, tool arguments, data feeding another system — and any automated flow where a malformed response has no human to notice it.

When not to use it

Conversational output a person reads, where strict structure adds nothing. Don't schema-constrain an answer that only ever lands in front of a human.

Prompt

Extract <fields> from the text below. Output ONLY valid JSON matching this schema — no prose, no markdown fence: <schema with types and which fields are required>. If a field isn't present in the text, use null; never invent a value. If you can't produce valid output, return {"error": "reason"}.

<text>

Example

Parsing receipts, you get {merchant, date, total, currency} and validate it; the one receipt where total comes back null is caught and routed to manual review, instead of a NaN silently flowing into the month's expense totals.

Advanced version

Have Claude include a confidence or a 'needs_review' flag per record, and gate low-confidence rows to human review. You get automation on the clear cases and a safety valve on the ambiguous ones, instead of uniform blind trust.

Common mistakes

  • Trusting the output shape because it's 'usually' right and skipping validation.
  • Letting the model fill missing fields with plausible guesses instead of null.
  • No error path for unparseable output, so one bad response takes down the batch.

Related