When Validation Fails
Explore how to handle AI model output that fails schema validation by using a targeted repair loop. Learn to feed validation errors back to the model for corrections, limit retry attempts, and implement safe fallbacks. This lesson ensures your backend service remains reliable, never crashes, and always returns valid structured data despite AI unpredictability.
The previous lesson ended with ValidationError being raised the moment a response didn't match our schema, and nothing after that point. That's honest, but it's not yet a complete system; a real service can't just stop the moment one response doesn't validate. This lesson builds what happens next: a repair loop that gives the model one or two genuine chances to fix its own mistake, using the actual validation error as the correction signal, before giving up in a defined, safe way.
Feeding the error back to the model
The single most useful piece of information we have after a failed validation is the error message itself; it names exactly which field was wrong and why. Instead of discarding that and just asking again with the same prompt, we can hand the error straight back to the model as part of a second attempt.
Lines 10–17:
build_repair_promptdoesn't throw away the context of what went wrong, it includes the original instructions, the exact bad response the model produced, and the exact validation error, all in one follow-up prompt. This gives the model something far more specific to correct than a bare "try again" would.Line 14: Passing
errordirectly into the f-string relies on Pydantic'sValidationErrorhaving a clear, readable string ...