Spring AI Structured Output Converter

Spring AI Structured Output Converter

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

Spring AI Structured Output Converter

  • BeanOutputConverter - The Bean Output Converter instructs the AI Model to produce a JSON response which should be compliant with DRAFT_2020_12 JSON Schema and derived from the requested Java class, then it uses an ObjectMapper to deserialize the JSON output into a Java object instance.

  • MapOutputConverter - The Map Output Converter instructs the AI Model to produce a MAP response which should be compliant with RFC8259 JSON schema, then it translates the JSON payload into a java.util.Map instance.

  • ListOutputConverter - The List Output Converter instructs the AI Model to produce a Comma-Delimited List response, and then it translates the response into a java.util.List.

follow us on