spring-ai
spring-ai copied to clipboard
Spring AI Anthropic Client fails to parse "thinking" content block type from Claude 3.7 response
trafficstars
Description
The application is failing to process responses from Claude 3.7 Sonnet when using the thinking parameter. We're encountering a JSON parsing exception because Spring AI's Anthropic client doesn't recognize the thinking content block type in the streaming response.
Error Details
java.lang.RuntimeException: Failed to json: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":"","signature":""}}
Root cause:
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'thinking' as a subtype of `org.springframework.ai.anthropic.api.AnthropicApi$ContentBlockStartEvent$ContentBlockBody`: known type ids = [text, tool_use]
My Code
val optionsBuilder = AnthropicChatOptions.builder().model(model).temperature(temperature)
if (thinking) {
optionsBuilder.thinking(AnthropicApi.ThinkingType.ENABLED, 16000)
optionsBuilder.maxTokens(24192)
optionsBuilder.temperature(1.0)
}
val prompt = Prompt(messages, optionsBuilder.build())
return this.chatClient.prompt(prompt).stream().content().collectList().awaitSingle()
.joinToString(separator = "")
Where client is
private val chatClient: ChatClient = ChatClient.create(chatModel)
Steps to Reproduce
- Configure Spring AI with Anthropic client using Claude 3.7 model
- Enable the "thinking" parameter in
AnthropicChatOptionsusingoptionsBuilder.thinking(AnthropicApi.ThinkingType.ENABLED, 16000) - Send a request to the model
Expected Behavior
The Spring AI Anthropic client should properly parse and handle the "thinking" content block type in the streaming response.
Environment
- Claude model: claude-3-7-sonnet-20250219
- Java version: 21
Possible Solution
Update the type registry in Spring AI's Anthropic client to include the "thinking" content block type as a recognized subtype of ContentBlockBody. This likely requires adding a class to handle this block type and updating the Jackson deserializer configuration.