0PricingLogin
AI Agents · Lesson

Repair Loops for Malformed Output

If the JSON doesn't parse, send the error back to the model and ask it to fix the output.

Even Strict Modes Fail Sometimes

Older models, edge cases, or non-strict providers occasionally return malformed JSON. Robust agents recover by asking the model to repair its output.

Basic Repair Loop

from pydantic import ValidationError
import json

def call_with_repair(messages, schema, max_attempts=3):
    for attempt in range(max_attempts):
        response = client.chat.completions.create(
            model='gpt-4o-mini',
            messages=messages,
            response_format={'type': 'json_object'}
        )
        raw = response.choices[0].message.content
        try:
            return schema.model_validate_json(raw)
        except (json.JSONDecodeError, ValidationError) as e:
            messages.append({'role': 'assistant', 'content': raw})
            messages.append({
                'role': 'user',
                'content': f'That output failed to validate: {e}.\nReturn ONLY valid JSON that matches the schema. No prose, no markdown fences.'
            })
    raise RuntimeError('Could not get valid output after repair attempts.')

All lessons in this course

  1. JSON Mode and Tool-Call Outputs
  2. Pydantic Schema Validation
  3. Repair Loops for Malformed Output
  4. Instructor / Outlines for Guaranteed Structure
← Back to AI Agents