Handling Schema Validation Failures
What you'll learn: How to gracefully recover when an LLM's output doesn't match your expected schema instead of simply crashing.
Why Validation Fails
Even with well-designed schemas and clear prompts, LLMs occasionally produce output that violates validation rules. Maybe a required field is missing, a number falls outside constraints, or a nested object has the wrong structure. This is normal—LLMs are probabilistic, not deterministic.
Three Recovery Strategies
1. Retry with Error Feedback
When validation fails, send the error message back to the LLM in a follow-up prompt: "Your previous response failed validation: field 'age' must be >= 0. Please try again." This gives the model a chance to self-correct. Limit retries (typically 2-3 attempts) to avoid infinite loops.
2. Repair Strategies For minor violations, automatically fix the output before failing completely. If an optional field has an invalid value, set it to its default. If a string is too long, truncate it. Think of this as "sanitizing" imperfect but salvageable output.
3. Fallback Responses When all else fails, have a graceful degradation plan. Return a partial result with a flag indicating validation issues, log the failure for analysis, or fall back to unstructured text output. Never let validation failures crash user-facing applications.
Implementation Pattern
max_retries = 3
for attempt in range(max_retries):
response = llm.generate(prompt)
try:
validated = MySchema.parse(response)
return validated # Success!
except ValidationError as e:
if attempt < max_retries - 1:
prompt = f"Previous output invalid: {e}. Try again."
else:
return apply_repair_strategy(response, e)
Key Takeaway: Validation failures are inevitable with LLMs—build resilient systems using retries, repair logic, and fallbacks rather than treating validation errors as fatal.