Validation & Repair
Typing the model's output like a door check
Parse, validate, and repair on failure: a typed guard rail around generative output. Strict validation with retry-with-errors keeps structured calls honest under noise.
What you will learn
- Parse + validate model JSON strictly
- Implement retry-with-error-reply
- Prevent repair loops from growing unbounded
What is this?
Validation is where generative doubt meets typed certainty: parse the model's JSON, validate against the schema (types, enums, constraints), and on failure either repair (send errors back for a corrected generation) or fail honestly. A repair loop with a hard cap keeps latency and tokens bounded while raising successful-parse rates.
Why do you need it?
- Every unvalidated model output is a future prod incident.
- Repair-with-errors converts most failures into recoveries.
- Captured failure rates give you a quality metric for prompt/schema changes.
- Type-safe consumers extend from validated data — no runtime guessing.
Mental model
Airline check-in with rebooking
Validation + repair
Boarding pass not matching the seat? The desk re-issues one with the error corrected (retry with feedback) — but only a few times before you miss the aircraft. A capped, message-explicit repair process keeps everyone moving.
How it works
- Parse the blob; JSON.parse failure → first repair pass.
- Validate against schema: types, required, enums, bounds.
- On violation, craft a concise error list honestly.
- Re-prompt with 'correct these issues: …' and re-validate.
- Cap retries (1-2); log; fail honestly with the raw blob.
Parse-validate-repair.
Examples
Repair loop sketch (Zod-like)
const schema = z.object({ intent: z.enum(["refund", "complaint"]).optional(), amount: z.number().min(0).max(100000),}); let raw = await model(prompt);for (let i = 0; i < 2; i++) { const parsed = schema.safeParse(JSON.parse(raw)); if (parsed.success) return parsed.data; raw = await model( prompt + "\nYour last reply failed: " + JSON.stringify(parsed.error.issues) + "\nReturn only corrected JSON." );}throw new Error("structured output failed after repair cap");Two repair passes with concrete errors, then an honest exception.
In production
- Every chat-JSON endpoint validates before DB or routing use.
- Repair rates feed an alert: climbing = schema or prompt decay.
- Typed DTOs derived from the schema serve UI and integrations.
Best practices
- Validate the SHAPE, not just 'it's JSON'.
- Always cap the repair loop; latency is money.
- Log failure blobs for schema/enum tuning.
- Alert when typed-parse success drops (schema + model health).
Common mistakes
- Validating with permissive typecasts instead of the schema.
- Unbounded retry loops that spike cost.
- Throwing away the error messages when re-prompting (repair is blind).
Common issues
Repair orders keep failingSchema + model mismatch
Re-validate the schema against the true output distribution; relax or re-bin.
Flaky success across seedsSampling variance vs tight schema
Lower temperature, tighten schema for the noisy field, keep the retry loop.
Debugging tips
- Repeated enum violations → inject the enum list into the prompt.
- Repair makes it 'worse' → error list confusing; be concise and specific.
- High failure + low repair → schema unrealistic; check actual model outputs.
Interview checkpoint
Hands-on lab
- Parse+validate a simulated model output
- Implement a capped repair loop
- Measure before/after repair success
Repair-Rate Tuning
35 min · intermediate
Quick quiz
Repair loops should be…
Feeding validation errors back to the model helps recovery.