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>

public class BeanOutputConverter<T> extends Object implements StructuredOutputConverter<T>
An implementation of StructuredOutputConverter that transforms the LLM output to a specific object type using JSON schema. This parser 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
  • Constructor Summary

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
     
    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

    Methods inherited from interface org.springframework.ai.converter.StructuredOutputConverter

    parse
  • Constructor Details

    • BeanOutputConverter

      public BeanOutputConverter(Class<T> clazz)
      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

      public BeanOutputConverter(org.springframework.core.ParameterizedTypeReference<T> typeRef)
      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

      public T convert(@NonNull String text)
      Specified by:
      convert in interface org.springframework.core.convert.converter.Converter<String,T>
    • getObjectMapper

      protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Configures and returns an object mapper for JSON operations.
      Returns:
      Configured object mapper.
    • getFormat

      public String getFormat()
      Provides the expected format of the response, instructing that it should adhere to the generated JSON schema.
      Specified by:
      getFormat in interface FormatProvider
      Returns:
      The instruction format string.
    • getJsonSchema

      public String getJsonSchema()
      Provides the generated JSON schema for the target type.
      Returns:
      The generated JSON schema.