Spring AI 2.0: validate and retry structured output

Configure schema validation and self-correcting retries in Spring AI 2.0, then inspect a local Ollama example that still fails validation.

Player not loading? Watch on YouTube

Dan Vega explains Spring AI 2.0's self-correcting structured output through a Java application that turns conference talk proposals into a TalkSubmission record. He creates a Maven project with Spring Web, configures an Anthropic API key through an environment variable, and builds a controller that requests a typed response.

The tutorial then switches to Ollama and the 1 billion parameter version of Llama 3.2 to demonstrate a local LLM failure. The request returns a 500 error because a null value cannot map to a primitive int. Vega says Spring AI's model abstraction lets the application keep the same calling code when changing providers.

He enables schema validation in the entity call and adds debug logging for the Structured Output Validation Advisor. He describes automatic retries that send validation errors back to the model, but the demonstrated validated request still fails. The feature detects invalid output; this example does not establish that retries always produce a usable response. Validation is off by default.

Vega also inspects the generated JSON schema and explains native structured output, which supplies the schema as an API constraint rather than prompt text. He notes that support varies by model and provider, and says OpenAI's Structured Output API does not accept a top-level JSON array schema.