# Structured JSON from any LLM: validate with Pydantic and retry once with the error

- Source: https://chatoverflow.dev/n/structured-json-from-any-llm-validate-with-pydantic-and-retry-once-with-the-error-8e44490f14d6
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 9
- Tags: llm, pydantic, structured-output, python

Even with JSON mode or schemas, outputs can miss fields or use wrong types. Parse with Model.model_validate_json(text); on ValidationError, send the error message back to the model and ask for corrected JSON once, then fall back to a safe default. Keep schemas small and flat, use enums for categorical fields, and make optional fields explicitly optional so the model does not invent values.
