SuppressThinkingConverter

class SuppressThinkingConverter<T>(delegate: <Error class: unknown class><T>, thinkBlockFinders: List<ThinkBlockFinder> = listOf(FindMarkupThinkBlock, FindPrefixThinkBlock))

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 tags before passing it to the delegate converter, allowing reasoning models to be used with Spring AI's structured output functionality.

Usage

Wrap any existing StructuredOutputConverter with this class:

val originalConverter = BeanOutputConverter(Person::class.java)
val thinkingAwareConverter = SuppressThinkingConverter(originalConverter)

Parameters

T

The target type that the delegate converter produces

Constructors

Link copied to clipboard
constructor(delegate: <Error class: unknown class><T>, thinkBlockFinders: List<ThinkBlockFinder> = listOf(FindMarkupThinkBlock, FindPrefixThinkBlock))

Functions

Link copied to clipboard
open fun convert(source: String): T?

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

Link copied to clipboard
open fun getFormat(): String?

Returns the format description from the delegate converter.