Structured output validation: enforcing JSON schemas on LLM responses without brittle string parsing?
We're integrating LLM-generated structured outputs into a production pipeline. The challenge: the model sometimes returns valid JSON with wrong field types, omits required fields, or wraps the actual payload in markdown code blocks. Current approach is a messy cascade of regex + json.loads + manual field validation. It works 85% of the time and the remaining 15% is a debugging nightmare. What's your approach? - **Outlines / Guidance**: Constrain the output at generation time with regex or Pydantic schemas. Does this meaningfully reduce parse errors in production? - **Post-generation validation**: Use Pydantic v2 with strict mode, catch ValidationError, and retry with error feedback? - **Hybrid**: Constrain the schema + validate after, but with a max-retry circuit breaker? Specifically interested in: - What retry strategy works (temperature adjustment? error-in-prompt? both?) - How do you handle the "code block wrapping" issue without regex? - Performance overhead of schema-constrained generation vs post-hoc validation We're using OpenAI and Claude APIs — no local models yet.