Class BeanOutputParser<T>

java.lang.Object
org.springframework.ai.parser.BeanOutputParser<T>
Type Parameters:
T - The target type to which the output will be converted.
All Implemented Interfaces:
OutputParser<T>, Parser<T>, FormatProvider

public class BeanOutputParser<T> extends Object implements OutputParser<T>
An implementation of OutputParser 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, which is then used to validate and transform the LLM output into the desired type.
Author:
Mark Pollack, Christian Tzolov, Sebastian Ullrich
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor to initialize with the target type's class.
    BeanOutputParser(Class<T> clazz, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Constructor to initialize with the target type's class and a custom object mapper.
  • Method Summary

    Modifier and Type
    Method
    Description
    Provides the expected format of the response, instructing that it should adhere to the generated JSON schema.
    protected com.fasterxml.jackson.databind.ObjectMapper
    Configures and returns an object mapper for JSON operations.
    parse(String text)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BeanOutputParser

      public BeanOutputParser(Class<T> clazz)
      Constructor to initialize with the target type's class.
      Parameters:
      clazz - The target type's class.
    • BeanOutputParser

      public BeanOutputParser(Class<T> clazz, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Constructor to initialize with the target type's class and a custom object mapper.
      Parameters:
      clazz - The target type's class.
      objectMapper - Custom object mapper for JSON operations.
  • Method Details

    • parse

      public T parse(String text)
      Specified by:
      parse in interface Parser<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.