Skip to content

Structured Output Fallback

Problem

During structured generation, a model may return output that does not conform to a JSON schema or contains errors. This can be due to model limitations, non-standard input, or decoding errors.

Fallback Strategies

Strategy Description Applicability
Retry Reinvoke with a corrected prompt Low cost
Error correction Post-process output to match the schema Syntax errors
Alternative model Use a more reliable model High criticality
Default values Fill fields with default values Optional fields
Fail Return an error to the user Non-critical operations

Implementation

def generate_structured(prompt, schema, max_retries=3):
    for attempt in range(max_retries):
        output = call_llm(prompt, schema)
        if validate(output, schema):
            return output
        prompt = add_error_feedback(prompt, output)
    return fallback_response(schema)

See also