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)
-
-
Constructor Summary
Constructors Constructor Description SuppressThinkingConverter(StructuredOutputConverter<T> delegate, List<Function1<String, String>> thinkBlockFinders)
-
Method Summary
-
-
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
-
-
-
-