Package org.springframework.ai.converter
Class BeanOutputConverter<T>
java.lang.Object
org.springframework.ai.converter.BeanOutputConverter<T>
- Type Parameters:
T
- The target type to which the output will be converted.
- All Implemented Interfaces:
FormatProvider
,StructuredOutputConverter<T>
,org.springframework.core.convert.converter.Converter<String,
T>
An implementation of
StructuredOutputConverter
that transforms the LLM output
to a specific object type using JSON schema. This converter works by generating a JSON
schema based on a given Java class or parameterized type reference, which is then used
to validate and transform the LLM output into the desired type.- Author:
- Mark Pollack, Christian Tzolov, Sebastian Ullrich, Kirk Lund, Josh Long, Sebastien Deleuze, Soby Chacko
-
Constructor Summary
ConstructorDescriptionBeanOutputConverter
(Class<T> clazz) Constructor to initialize with the target type's class.BeanOutputConverter
(Class<T> clazz, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Constructor to initialize with the target type's class, a custom object mapper, and a line endings normalizer to ensure consistent line endings on any platform.BeanOutputConverter
(org.springframework.core.ParameterizedTypeReference<T> typeRef) Constructor to initialize with the target class type reference.BeanOutputConverter
(org.springframework.core.ParameterizedTypeReference<T> typeRef, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Constructor to initialize with the target class type reference, a custom object mapper, and a line endings normalizer to ensure consistent line endings on any platform. -
Method Summary
Modifier and TypeMethodDescriptionParses the given text to transform it to the desired target type.Provides the expected format of the response, instructing that it should adhere to the generated JSON schema.Provides the generated JSON schema for the target type.protected com.fasterxml.jackson.databind.ObjectMapper
Configures and returns an object mapper for JSON operations.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.core.convert.converter.Converter
andThen
-
Constructor Details
-
BeanOutputConverter
Constructor to initialize with the target type's class.- Parameters:
clazz
- The target type's class.
-
BeanOutputConverter
public BeanOutputConverter(Class<T> clazz, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Constructor to initialize with the target type's class, a custom object mapper, and a line endings normalizer to ensure consistent line endings on any platform.- Parameters:
clazz
- The target type's class.objectMapper
- Custom object mapper for JSON operations. endings.
-
BeanOutputConverter
Constructor to initialize with the target class type reference.- Parameters:
typeRef
- The target class type reference.
-
BeanOutputConverter
public BeanOutputConverter(org.springframework.core.ParameterizedTypeReference<T> typeRef, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Constructor to initialize with the target class type reference, a custom object mapper, and a line endings normalizer to ensure consistent line endings on any platform.- Parameters:
typeRef
- The target class type reference.objectMapper
- Custom object mapper for JSON operations. endings.
-
-
Method Details
-
convert
Parses the given text to transform it to the desired target type. -
getObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Configures and returns an object mapper for JSON operations.- Returns:
- Configured object mapper.
-
getFormat
Provides the expected format of the response, instructing that it should adhere to the generated JSON schema.- Specified by:
getFormat
in interfaceFormatProvider
- Returns:
- The instruction format string.
-
getJsonSchema
Provides the generated JSON schema for the target type.- Returns:
- The generated JSON schema.
-