Back to feed
Dev.to
Dev.to
7/18/2026
Stop Begging Your LLM for Valid JSON: Self-Correcting Structured Output in Spring AI 2.0

Stop Begging Your LLM for Valid JSON: Self-Correcting Structured Output in Spring AI 2.0

Short summary

Spring AI 2.0 adds self-correcting schema validation for LLM structured output, solving the common problem of models returning invalid JSON. When deserialization fails, the framework feeds the specific validation error back to the model and retries up to 3 times by default. The feature is enabled with a single method call — spec.validateSchema() — and is especially useful for smaller open-source models like Llama 3.2 1B that frequently produce malformed JSON.

  • Spring AI 2.0 introduces self-correcting schema validation via StructuredOutputValidationAdvisor
  • On validation failure, the specific error is appended to the prompt and the call is re-issued automatically
  • Retry count is customizable; default is 3 attempts, powered by a recursive advisor pattern

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more