Skip to content

Commit 8374c20

Browse files
committed
feat: enhance adaptive thinking handling in messages API to prevent errors with tool choices
1 parent e69e6a8 commit 8374c20

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/routes/messages/handler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,12 @@ const handleWithMessagesApi = async (
345345
}
346346
}
347347

348-
if (selectedModel?.capabilities.supports.adaptive_thinking) {
348+
// https://platform.claude.com/docs/en/build-with-claude/extended-thinking#extended-thinking-with-tool-use
349+
// Using tool_choice: {"type": "any"} or tool_choice: {"type": "tool", "name": "..."} will result in an error because these options force tool use, which is incompatible with extended thinking.
350+
const toolChoice = anthropicPayload.tool_choice
351+
const disableThink = toolChoice?.type === "any" || toolChoice?.type === "tool"
352+
353+
if (selectedModel?.capabilities.supports.adaptive_thinking && !disableThink) {
349354
anthropicPayload.thinking = {
350355
type: "adaptive",
351356
}

0 commit comments

Comments
 (0)