Repair and Validation Loops
Fixing malformed output.
Why Repair Loops Exist
Even with schemas, output can fail: truncated JSON from a token cap, hallucinated extra fields, a string where a number was required, or a violated business rule. A repair loop detects the failure and feeds it back to the model to correct.
It is the safety net that turns a 98% success rate into a 99.9% one.
The Validate-Repair Cycle
The canonical loop: generate, validate, and if invalid, re-prompt with the specific error, up to a bounded number of attempts.
def generate_valid(prompt, schema, max_tries=3):
msgs = [{'role': 'user', 'content': prompt}]
for _ in range(max_tries):
out = call_model(msgs)
ok, err = validate(out, schema)
if ok:
return out
msgs.append({'role': 'assistant', 'content': out})
msgs.append({'role': 'user',
'content': 'Invalid. Fix this error and resend JSON only: ' + err})
raise ValueError('repair budget exhausted')All lessons in this course
- Why Structured Output
- JSON Schema in Prompts
- Tool/Function Schemas
- Repair and Validation Loops