Skip to content

Guarantee Schema-Valid Structured Outputs for Downstream APIs

Eliminate production `JSON.parse` crashes, invented enum values, and truncated payloads when wiring LLMs into databases and UI components.

The button copies the prompt and opens Michał Jaskólski’s LinkedIn profile: paste it into your message to him.

Bulletproofing structured LLM outputs in production

  1. 01

    Use grammar-constrained decoding where supported

    Leverage native strict JSON-Schema modes alongside runtime Zod validation so syntax errors and unknown keys become impossible.

  2. 02

    Order reasoning fields before classification enums

    Because autoregressive tokens condition left-to-right, placing a brief `evidence_quote` field ahead of `decision_category` lifts accuracy sharply.

  3. 03

    Include explicit nullable and 'unknown' escape hatches

    Never force a required string or number when source text omits it—nullable fields prevent the model from fabricating plausible filler data.

  4. 04

    Keep schemas shallow and split overloaded mega-objects

    Deeply nested 80-key schemas degrade extraction recall; split independent concerns into two fast parallel typed calls.

Frequently Asked Questions

Why did accuracy drop when we turned on strict JSON mode without changing our prompt?

Removing free-text scratchpad space forces the model to commit to a label on token one; adding a short preceding `rationale` field restores reasoning quality.

How do you handle output truncation when extracting huge line-item arrays?

Paginate extraction by document page range, cap max items per call, and check `finish_reason === 'length'` explicitly before parsing.

Can we keep one single Zod schema source of truth across frontend and LLM calls?

Yes—shared TypeScript Zod definitions compile cleanly to OpenAI, Anthropic tool-input, and Gemini response schemas while typing React props automatically.