Spring
Follow
Self-Correcting Structured Output in Spring AI 2.0
Large language models, while powerful, struggle with structured data for software integration. Spring AI's structured output bridges this gap by allowing models to produce text conforming to a defined schema, which is then parsed into typed objects. Spring AI 2.0 introduces two key enhancements: provider-native structured output and self-correcting schema validation. Existing code remains compatible due to unchanged defaults.To generate structured output, developers define a Java record and then use the .entity() method to specify the target type. This process internally generates a JSON schema from the record, appends it to the prompt, and parses the model's JSON response back into the desired object. Prior to Spring AI 2.0, this process lacked guarantees, leading to potential parsing errors if models deviated from the schema.The .validateSchema() switch activates a self-correcting retry loop, automatically validating the model's response against the schema and re-issuing the prompt with specific error feedback if validation fails. For stronger guarantees, .useProviderStructuredOutput() leverages provider-specific API features to enforce schema conformance at the source. This minimizes malformed output by preventing invalid responses from being emitted in the first place.These two features can be combined for maximum resilience, with provider-native output acting as a primary constraint and schema validation as a fallback. For generic types like Lists and Maps, ParameterizedTypeReference is used instead of a simple Class. The .responseEntity() method allows access to both the parsed entity and the raw ChatResponse for metadata.When built-in converters are insufficient, custom StructuredOutputConverter implementations can handle non-standard JSON wrapping or entirely different formats like YAML or CSV. This provides further flexibility for diverse integration needs.