Skip to content

Conversation

ghdcksgml1
Copy link
Contributor

Summary

Fix missing 'none' tool choice handling in ChatAnthropic

Bug Example

import {ChatAnthropic} from "@langchain/anthropic";
import {z} from "zod";
import {tool} from "@langchain/core/tools";

const adderTool = tool(
  async ({a, b}) => {
    return a + b;
  },
  {
    name: "add",
    description: "Adds a and b",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);

const runner = async () => {
  const tools = [adderTool];

  const llm = new ChatAnthropic({
    model: "claude-4-sonnet-20250514",
  });
  const llmWithTools = llm.bindTools(tools, {tool_choice: "none"});

  let result = await llmWithTools.invoke([
    {
      role: "user",
      content: "please 2 + 3"
    }
  ]);

  console.log("tool_calls: " + result.tool_calls);
};
runner().catch(console.error);

// expect => tool_calls: []
// actual => BadRequestError: 400 {"type":"error","error":{"type":"invalid_request_error","message":"Tool 'none' not found in provided tools"},"request_id":"req_011CT..."}

Details

The Anthropic integration didn't handle tool_choice: "none" option. Users couldn't disable tool calling when needed.

Added the missing case in handleToolChoice() function to map "none" to { type: "none" } format that Anthropic's API expects.

reference: https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/implement-tool-use

Impact

✅ tool_choice: "none" now works
✅ No tools called when set to "none"
✅ Test added to verify behavior

@changeset-bot
Copy link

changeset-bot bot commented Sep 11, 2025

🦋 Changeset detected

Latest commit: 417dd42

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@langchain/anthropic Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Sep 11, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
langchainjs-docs Ready Ready Preview Sep 11, 2025 7:59pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Sep 11, 2025 7:59pm

Copy link
Member

@hntrl hntrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks @ghdcksgml1!

@hntrl hntrl merged commit 9ed7dfa into langchain-ai:main Sep 11, 2025
35 checks passed
hntrl added a commit that referenced this pull request Sep 11, 2025
Co-authored-by: Hunter Lovell <[email protected]>
Co-authored-by: Hunter Lovell <[email protected]>
hntrl added a commit that referenced this pull request Oct 11, 2025
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 this pull request may close these issues.

2 participants