Intermediate level · Module 5: Structured Outputs and Validation · Lesson 2 of 3
Retry with Error Feedback and Provider Features
Learners implement a validate-retry-fallback loop and compare it with providers' structured-output features that constrain responses to a schema.
In this lesson you will
- Implement validate-retry-fallback
- Explain provider structured-output features and their limits
Key concepts
retry with errorsfallbackprovider structured outputsschema-constrained generationstill validate
The lesson
When validation fails, send the errors back to the model ('confidence must be ≤ 1; needs_human is missing') and ask for a corrected JSON once or twice. If it still fails, return a safe fallback and log the case for review.
Some providers offer structured-output modes that constrain responses to a supplied JSON Schema. These greatly reduce format errors, but options differ by provider and model, and local models may not support them.
Always validate anyway: a response can match the schema and still be wrong (an invented source ID), and provider features can change. Validation in your own code is the reliable control.
Step by step
- Implement the validate-retry-fallback loop with a maximum of two retries.
- Test it with scripted invalid outputs.
- Read one provider's structured-output documentation and note its limits.
- Add a check that every source ID exists in the approved collection.
Practice activity
Add validate-retry-fallback and source-ID checking to the project.
Expected result: A loop that recovers from format errors and rejects invented sources.
Check your understanding
-
Why still validate when using provider structured outputs?
Show answer
A response can match the schema but still be wrong, and features differ between providers and models.
-
What happens after the maximum number of retries?
Show answer
A safe fallback is returned and the case is logged.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.