fix(anthropic): support Claude 4.x models and mixed-content tool calls#1500
Open
ming-hsien wants to merge 1 commit into
Open
fix(anthropic): support Claude 4.x models and mixed-content tool calls#1500ming-hsien wants to merge 1 commit into
ming-hsien wants to merge 1 commit into
Conversation
- Add omitempty to temperature field in messagePayload and MessageRequest so that unset temperature (0.0) is not serialized into the request body. Claude 4.x models (e.g. claude-opus-4-7) have deprecated the temperature parameter and return HTTP 400 when it is present. - Rewrite handleAIMessage to iterate over all Parts instead of only Parts[0]. When Claude returns a response containing both a text block and tool_use blocks, processAnthropicResponse produces one ContentChoice per block. The caller reassembles them into a single MessageContent with mixed Parts (TextContent followed by ToolCall). The original implementation only inspected Parts[0], so ToolCall entries were silently dropped from the assistant message written to history. On the next turn Anthropic rejected the request with "tool_result block has no corresponding tool_use block".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two bugs that prevent langchaingo from working correctly with Claude 4.x models (e.g.
claude-opus-4-7,claude-sonnet-4-6):1.
temperaturefield causes HTTP 400 on Claude 4.xmessagePayloadandMessageRequestboth declareTemperature float64 \json:"temperature"`withoutomitempty. This means every API request serializes"temperature": 0even when the caller never set a temperature. Claude 4.x models have deprecated thetemperature` parameter and return:400: temperature is deprecated for this model.
2. Mixed text + tool_use responses break conversation history
When Claude returns a response that contains both a text block and one or more
tool_useblocks,processAnthropicResponsecorrectly produces oneContentChoiceper content block. However,handleAIMessageonly inspectedParts[0]of the reassembledMessageContent. If the first part wasTextContent, all subsequentToolCallparts were silently dropped when writing the assistant message back to history.On the next turn, the
tool_resultblock referenced atool_useID that no longer existed in the assistant message, causing Anthropic to reject the request with:400: Each tool_result block must have a corresponding tool_use block in the previous message.
Fix
omitemptytoTemperatureinmessagePayloadandMessageRequestso the field is omitted when not explicitly set.handleAIMessageto iterate over allPartsinstead of onlyParts[0], correctly emitting bothtextandtool_usecontent blocks in a single assistant message.Tested with
claude-opus-4-7claude-sonnet-4-6