Skip to content

Enforce strict JSON schemas across multi-step LLM workflows

Eliminate broken downstream API calls caused by markdown wrappers, missing keys, or drifted enums with multi-stage validation and self-repair loops.

Copy the prompt and paste it into the AI assistant you already use.

Engineering pattern for 100% parseable LLM outputs

  1. 01

    Use native structured outputs with flat schemas

    Define concise JSON Schemas with explicit enums, nullable types, and clear property descriptions.

  2. 02

    Include explicit nullability for absent facts

    Give every optional field a clean null path so the model never fabricates phone numbers or dates just to satisfy a required key.

  3. 03

    Run deterministic business-rule assertions

    Layer cross-field checks (such as start_date <= end_date or ISO country codes) right after syntactic schema parsing.

  4. 04

    Attach a bounded validation-repair loop

    If business assertions fail, retry once with the specific validator error message and route persistent edge cases to review.

How it works in practice

Messy inspection note -> Target JSON Schema

Note: 'Unit 4B HVAC compressor failed; replace part #CMP-77 urgently. Est cost ~$1,400.' Schema requires: unit_id (string), priority ('low'|'medium'|'urgent'), est_cost_usd (number), warranty_claim (boolean|null)

Validated schema-compliant output

{"unit_id":"4B","part_code":"CMP-77","priority":"urgent","est_cost_usd":1400,"warranty_claim":null,"schema_valid":true}

Downstream databases and APIs receive strictly typed payloads every single run without brittle regex cleanup.

Common questions

Why add business-rule validation if we already use structured output mode?

Structured output guarantees valid JSON syntax and types, while deterministic validators verify cross-field logic like valid dates, totals, and SKU existence.

Why does forcing required strings increase hallucination rates in pipelines?

When source text omits a value like a phone number, a non-nullable required field pressures the model to invent placeholders instead of returning null.

How many repair retries should run before escalating an item?

One targeted repair pass resolves almost all formatting slips; capping retries at one or two prevents runaway token spend on corrupt inputs.

Learn more

Related guides and background articles for this task