Class SuppressThinkingConverter

  • All Implemented Interfaces:
    org.springframework.ai.converter.FormatProvider , org.springframework.ai.converter.StructuredOutputConverter , org.springframework.core.convert.converter.Converter

    
    public final class SuppressThinkingConverter<T extends Object>
     implements StructuredOutputConverter<T>
                        

    A decorator for Spring AI's StructuredOutputConverter that cleans up LLM outputs by removing "thinking" blocks.

    Spring AI's StructuredOutputConverter is designed to parse structured formats (like JSON) from LLM outputs, but it can fail if the output contains additional text like reasoning blocks. For example, if an LLM returns:

    <think>
    Let me think about what information to include in this person object.
    The name should be "John Doe" and the age should be 30.
    </think>
    {"name": "John Doe", "age": 30}

    A standard converter would fail to parse this as valid JSON.

    This decorator sanitizes the input by removing any content enclosed in <think> tags before passing it to the delegate converter, allowing reasoning models to be used with Spring AI's structured output functionality.

    Wrap any existing StructuredOutputConverter with this class:

    val originalConverter = BeanOutputConverter(Person::class.java)
    val thinkingAwareConverter = SuppressThinkingConverter(originalConverter)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      T convert(String source) Converts the source string to the target type after removing any thinking blocks.
      String getFormat() Returns the format description from the delegate converter.
      • Methods inherited from class org.springframework.core.convert.converter.Converter

        andThen
      • Methods inherited from class java.lang.Object

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

      • SuppressThinkingConverter

        SuppressThinkingConverter(StructuredOutputConverter<T> delegate, List<Function1<String, String>> thinkBlockFinders)
    • Method Detail

      • convert

         T convert(String source)

        Converts the source string to the target type after removing any thinking blocks.

        This method performs the following steps:

        • Calls identifyThinkBlock to sanitize the input by removing thinking blocks

        • Logs any detected thinking blocks (for debugging/analysis purposes)

        • Delegates the actual conversion to the wrapped converter

        Parameters:
        source - The raw string output from the LLM, potentially containing thinking blocks
        Returns:

        The converted object of type T, or null if conversion fails

      • getFormat

         String getFormat()

        Returns the format description from the delegate converter.

        This method is part of the StructuredOutputConverter interface's org.springframework.ai.converter.FormatProvider functionality. The format string provides instructions to the LLM about how to structure its response. This implementation simply forwards to the delegate's format, maintaining the decorator pattern.

        Returns:

        The format description string from the delegate, or null if the delegate doesn't provide one