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 Converter
instructs theAI Model
to produce aJSON response
which should be compliant withDRAFT_2020_12 JSON Schema
and derived from the requestedJava class
, then it uses anObjectMapper
to deserialize theJSON output
into aJava object
instance. -
MapOutputConverter - The
Map Output Converter
instructs theAI Model
to produce aMAP response
which should be compliant withRFC8259 JSON schema
, then it translates theJSON payload
into ajava.util.Map
instance. -
ListOutputConverter - The
List Output Converter
instructs theAI Model
to produce aComma-Delimited List
response, and then it translates the response into ajava.util.List
.