
Most of the AI Models are Large Language Models or LLM, i.e., they respond in Plain Text Format. This makes sense for a Chat Application, but an Application communicating with an AI Model expects standard Serialized Output, such as Java Bean, JSON, List, or Map format, for easy response parsing. The API also responds in JSON, XML, or Java Bean classes. The Spring AI Structured Output Converter helps convert the Plain Text Response into a more Structured Format.
Before the LLM AI Model call, the Spring AI Structured Output Converter appends the desired format instructions in the Prompt, it provides detailed guidance to the AI Model about the generated response Output Format.
After the LLM AI Model call, the Spring AI Structured Output Converter takes the Model output and transforms it into the Desired Structure type.
The Spring AI Structured Output Converter tries its best to convert the AI Model output into a desired structure but is not guaranteed to return the structured output as requested.
The Spring AI Structured Output Converter currently supports the below converters

-
BeanOutputConverter - The
Bean Output Converterinstructs theAI Modelto produce aJSON responsewhich should be compliant withDRAFT_2020_12 JSON Schemaand derived from the requestedJava class, then it uses anObjectMapperto deserialize theJSON outputinto aJava objectinstance. -
MapOutputConverter - The
Map Output Converterinstructs theAI Modelto produce aMAP responsewhich should be compliant withRFC8259 JSON schema, then it translates theJSON payloadinto ajava.util.Mapinstance. -
ListOutputConverter - The
List Output Converterinstructs theAI Modelto produce aComma-Delimited Listresponse, and then it translates the response into ajava.util.List.