Skip to main content

Lesson 4 · 12 min

Validation, retries, and error recovery

Build a validation loop that catches and corrects malformed outputs automatically, with a fallback hierarchy for graceful degradation.

The validation loop pattern

Even with JSON mode and good schemas, models occasionally produce invalid output — wrong enum value, float out of range, required field missing. A retry loop with the error message fed back into the conversation recovers most failures without human intervention.

instructor implements this by default. Here's what it looks like under the hood: