Skip to content

Spring AI Anthropic Client fails to parse "thinking" content block type from Claude 3.7 response #2793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dinodanger opened this issue Apr 18, 2025 · 0 comments · May be fixed by #2800
Open

Comments

@dinodanger
Copy link

dinodanger commented Apr 18, 2025

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

  1. Configure Spring AI with Anthropic client using Claude 3.7 model
  2. Enable the "thinking" parameter in AnthropicChatOptions using optionsBuilder.thinking(AnthropicApi.ThinkingType.ENABLED, 16000)
  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant