From 0076be531d33cc07355d455a613da9c255a8b849 Mon Sep 17 00:00:00 2001 From: Bee <68532117+abeatrix@users.noreply.github.com> Date: Wed, 5 Nov 2025 20:10:09 -0800 Subject: [PATCH] fix: removes reasoning_details field from Anthropic and Gemini providers (#7282) * fix: sanitize Anthropic message and removes reasoning_details field - Extract duplicate Anthropic message formatting code into sanitizeAnthropicMessages function - Remove code duplication between AnthropicHandler and VertexHandler - Add support for claude-sonnet-4-5-20250929:1m model - Remove unused MessageParam import from anthropic provider * add changeset * Create thinking content block for gemini --- .changeset/orange-ants-change.md | 5 ++ src/core/api/providers/anthropic.ts | 36 ++-------- src/core/api/providers/vertex.ts | 55 +------------- src/core/api/transform/anthropic-format.ts | 84 ++++++++++++++++++---- src/core/api/transform/gemini-format.ts | 70 +++++++++--------- 5 files changed, 121 insertions(+), 129 deletions(-) create mode 100644 .changeset/orange-ants-change.md diff --git a/.changeset/orange-ants-change.md b/.changeset/orange-ants-change.md new file mode 100644 index 00000000..344c1c6f --- /dev/null +++ b/.changeset/orange-ants-change.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Removes reasoning_details content field from Anthropic providers diff --git a/src/core/api/providers/anthropic.ts b/src/core/api/providers/anthropic.ts index 5de99b1d..a157542c 100644 --- a/src/core/api/providers/anthropic.ts +++ b/src/core/api/providers/anthropic.ts @@ -1,11 +1,12 @@ import { Anthropic } from "@anthropic-ai/sdk" -import { Tool as AnthropicTool, MessageParam } from "@anthropic-ai/sdk/resources/index" +import { Tool as AnthropicTool } from "@anthropic-ai/sdk/resources/index" import { Stream as AnthropicStream } from "@anthropic-ai/sdk/streaming" import { AnthropicModelId, anthropicDefaultModelId, anthropicModels, CLAUDE_SONNET_1M_SUFFIX, ModelInfo } from "@shared/api" import { fetch } from "@/shared/net" import { ClineTool } from "@/shared/tools" import { ApiHandler, CommonApiHandlerOptions } from "../index" import { withRetry } from "../retry" +import { sanitizeAnthropicMessages } from "../transform/anthropic-format" import { ApiStream } from "../transform/stream" interface AnthropicHandlerOptions extends CommonApiHandlerOptions { @@ -63,6 +64,7 @@ export class AnthropicHandler implements ApiHandler { switch (modelId) { // 'latest' alias does not support cache_control case "claude-haiku-4-5-20251001": + case "claude-sonnet-4-5-20250929:1m": case "claude-sonnet-4-5-20250929": case "claude-sonnet-4-20250514": case "claude-3-7-sonnet-20250219": @@ -84,35 +86,7 @@ export class AnthropicHandler implements ApiHandler { const lastUserMsgIndex = userMsgIndices[userMsgIndices.length - 1] ?? -1 const secondLastMsgUserIndex = userMsgIndices[userMsgIndices.length - 2] ?? -1 - const anthropicMessages: Array = messages.map((message, index) => { - if (index === lastUserMsgIndex || index === secondLastMsgUserIndex) { - return { - ...message, - content: - typeof message.content === "string" - ? [ - { - type: "text", - text: message.content, - cache_control: { - type: "ephemeral", - }, - }, - ] - : message.content.map((content, contentIndex) => - contentIndex === message.content.length - 1 - ? { - ...content, - cache_control: { - type: "ephemeral", - }, - } - : content, - ), - } - } - return message - }) + const anthropicMessages = sanitizeAnthropicMessages(messages, lastUserMsgIndex, secondLastMsgUserIndex) stream = await client.messages.create( { @@ -161,7 +135,7 @@ export class AnthropicHandler implements ApiHandler { max_tokens: model.info.maxTokens || 8192, temperature: 0, system: [{ text: systemPrompt, type: "text" }], - messages, + messages: sanitizeAnthropicMessages(messages), // tools, // tool_choice: { type: "auto" }, stream: true, diff --git a/src/core/api/providers/vertex.ts b/src/core/api/providers/vertex.ts index 1232966e..d392091e 100644 --- a/src/core/api/providers/vertex.ts +++ b/src/core/api/providers/vertex.ts @@ -6,6 +6,7 @@ import { ModelInfo, VertexModelId, vertexDefaultModelId, vertexModels } from "@s import { ClineTool } from "@/shared/tools" import { ApiHandler, CommonApiHandlerOptions } from "../" import { withRetry } from "../retry" +import { sanitizeAnthropicMessages } from "../transform/anthropic-format" import { ApiStream } from "../transform/stream" import { GeminiHandler } from "./gemini" @@ -122,46 +123,7 @@ export class VertexHandler implements ApiHandler { cache_control: { type: "ephemeral" }, }, ], - messages: messages.map((message, index) => { - if (index === lastUserMsgIndex || index === secondLastMsgUserIndex) { - return { - ...message, - content: - typeof message.content === "string" - ? [ - { - type: "text", - text: message.content, - cache_control: { - type: "ephemeral", - }, - }, - ] - : message.content.map((content, contentIndex) => - contentIndex === message.content.length - 1 - ? { - ...content, - cache_control: { - type: "ephemeral", - }, - } - : content, - ), - } - } - return { - ...message, - content: - typeof message.content === "string" - ? [ - { - type: "text", - text: message.content, - }, - ] - : message.content, - } - }), + messages: sanitizeAnthropicMessages(messages, lastUserMsgIndex, secondLastMsgUserIndex), stream: true, tools: tools?.length ? (tools as AnthropicTool[]) : undefined, // tool_choice options: @@ -187,18 +149,7 @@ export class VertexHandler implements ApiHandler { type: "text", }, ], - messages: messages.map((message) => ({ - ...message, - content: - typeof message.content === "string" - ? [ - { - type: "text", - text: message.content, - }, - ] - : message.content, - })), + messages: sanitizeAnthropicMessages(messages), stream: true, tools: tools?.length ? (tools as AnthropicTool[]) : undefined, // tool_choice options: diff --git a/src/core/api/transform/anthropic-format.ts b/src/core/api/transform/anthropic-format.ts index 3b73c557..116c0a2e 100644 --- a/src/core/api/transform/anthropic-format.ts +++ b/src/core/api/transform/anthropic-format.ts @@ -1,19 +1,77 @@ -import { Tool as AnthropicTool } from "@anthropic-ai/sdk/resources/index" -import { ChatCompletionTool as OpenAITool } from "openai/resources/chat/completions" +import { MessageParam } from "@anthropic-ai/sdk/resources/index" /** - * Converts an OpenAI ChatCompletionTool into an Anthropic Tool definition + * Sanitize Anthropic messages by removing reasoning details and adding ephemeral cache control + * to the last two user messages to prevent them from being stored in Anthropic's cache. */ -export function openAIToolToAnthropic(openAITool: OpenAITool): AnthropicTool { - const func = openAITool.function +export function sanitizeAnthropicMessages( + messages: Array, + lastUserMsgIndex?: number, + secondLastMsgUserIndex?: number, +): Array { + return messages.map((_message, index) => { + const message = removeReasoningDetails(_message) + const addCacheControl = lastUserMsgIndex !== undefined && secondLastMsgUserIndex !== undefined - return { - name: func.name, - description: func.description || "", - input_schema: { - type: "object", - properties: func.parameters?.properties || {}, - required: func.parameters?.required || [], - }, + if (addCacheControl && (index === lastUserMsgIndex || index === secondLastMsgUserIndex)) { + return { + ...message, + content: + typeof message.content === "string" + ? [ + { + type: "text", + text: message.content, + cache_control: { + type: "ephemeral", + }, + }, + ] + : message.content.map((content, contentIndex) => + contentIndex === message.content.length - 1 + ? { + ...content, + cache_control: { + type: "ephemeral", + }, + } + : content, + ), + } + } + + return { + ...message, + content: + typeof message.content === "string" + ? [ + { + type: "text", + text: message.content, + }, + ] + : message.content, + } + }) +} + +/** + * Remove reasoning details from a single Anthropic message parameter + */ +function removeReasoningDetails(param: MessageParam): MessageParam { + if (Array.isArray(param.content)) { + return { + ...param, + content: param.content.map((item) => { + if (item.type === "text") { + return { + ...item, + reasoning_details: undefined, + } + } + return item + }), + } } + return param } diff --git a/src/core/api/transform/gemini-format.ts b/src/core/api/transform/gemini-format.ts index f81a4c38..473b9baf 100644 --- a/src/core/api/transform/gemini-format.ts +++ b/src/core/api/transform/gemini-format.ts @@ -5,40 +5,44 @@ export function convertAnthropicContentToGemini(content: string | Anthropic.Cont if (typeof content === "string") { return [{ text: content }] } - return content.flatMap((block): Part => { - switch (block.type) { - case "text": - return { text: block.text } - case "image": - if (block.source.type !== "base64") { - throw new Error("Unsupported image source type") - } - return { - inlineData: { - data: block.source.data, - mimeType: block.source.media_type, - }, - } - case "tool_use": - return { - functionCall: { - name: block.name, - args: block.input as Record, - }, - } - case "tool_result": - return { - functionResponse: { - name: block.tool_use_id, - response: { - result: block.content, + return content + .flatMap((block): Part | undefined => { + switch (block.type) { + case "text": + return { text: block.text } + case "image": + if (block.source.type !== "base64") { + throw new Error("Unsupported image source type") + } + return { + inlineData: { + data: block.source.data, + mimeType: block.source.media_type, }, - }, - } - default: - throw new Error(`Unsupported content block type: ${block.type}`) - } - }) + } + case "tool_use": + return { + functionCall: { + name: block.name, + args: block.input as Record, + }, + } + case "tool_result": + return { + functionResponse: { + name: block.tool_use_id, + response: { + result: block.content, + }, + }, + } + case "thinking": + return { text: block.thinking, thought: true, thoughtSignature: block.signature } + default: + return undefined + } + }) + .filter((part): part is Part => part !== undefined) // Filter out unsupported blocks } export function convertAnthropicMessageToGemini(message: Anthropic.Messages.MessageParam): Content {