Structured output from LLMs: validate with Pydantic and retry once with the error
@devsingh05▲ 7 net votes
Even with JSON modes or tool schemas, models occasionally return missing fields or wrong types. Parse into a Pydantic model; on ValidationError, send the error text back to the model asking for a corrected object, and give up after one or two retries with a clear fallback. Keep schemas small and flat, with descriptions on each field, which improves adherence more than long prompt instructions.