Claude API 400 tool_choice: type "tool" and "any" are not supported for this model:原因与修复
向 POST /v1/messages(或 token 计数端点)发送请求时,若对不支持强制工具调用的模型传入了 tool_choice: {"type": "any"} 或 tool_choice: {"type": "tool", "name": "..."},API 会直接返回 HTTP 400 invalid_request_error。
一句话结论
Claude Fable 5.2、Claude Mythos 5.2、Claude Opus 5.5、Claude Sonnet 5.5、Claude Fable 5.1 与 Claude Mythos 5.1 均拒绝强制工具调用(tool_choice 的 type 为 "any" 或 "tool")。请将 tool_choice 改为 {"type": "auto"}(或直接省略),在工具定义上开启 "strict": true(配合 input_schema 里的 additionalProperties: false 与 required),并在 system 提示词中明确点名要调用的工具;如果原本只是借虚拟工具提取固定 JSON,请直接改用结构化输出(output_config.format)。
最后核验:2026-11-14,对照 Anthropic API Errors — Common validation errors。
报错现象与原文
向受影响的模型(包括其 token 计数接口)发送 tool_choice: {"type": "any"} 或 tool_choice: {"type": "tool", "name": "..."} 时,API 会返回如下原文:
tool_choice: type "tool" and "any" are not supported for this model.完整 JSON 响应体如下:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "tool_choice: type \"tool\" and \"any\" are not supported for this model."
}
}同一次升级模型时常见的其他 400 参数校验报错:
- 末尾 assistant 预填被拒 —— 见 assistant prefill 不支持。
- 关闭思考被拒 —— 见
thinking.type.disabled不支持。 - 多轮工具调用漏传
tool_result—— 见tool_use缺少对应tool_result。 - 最大输出或思考预算冲突 —— 见
max_tokens超限报错。
受影响的模型对照表
| 模型 | 强制工具调用("any"、"tool") | 允许的 tool_choice 取值 |
|---|---|---|
| Claude Fable 5.2 | 不支持(400) | "auto"(默认)、"none" |
| Claude Mythos 5.2 | 不支持(400) | "auto"(默认)、"none" |
Claude Opus 5.5(claude-opus-5-5) | 不支持(400) | "auto"(默认)、"none" |
Claude Sonnet 5.5(claude-sonnet-5-5) | 不支持(400) | "auto"(默认)、"none" |
| Claude Fable 5.1 | 不支持(400) | "auto"(默认)、"none" |
| Claude Mythos 5.1 | 不支持(400) | "auto"(默认)、"none" |
早期模型(如 claude-opus-5、claude-opus-4-8、claude-sonnet-5) | 支持 | "auto"、"any"、"tool"、"none" |
常见触发原因
- 直接替换了模型 ID: 旧代码在
claude-opus-5、claude-opus-4-8或claude-sonnet-5上传{"type": "tool", "name": "..."}正常跑通,一旦把模型名改成claude-opus-5-5或claude-sonnet-5-5立刻报 400。 - 用虚拟工具“曲线提取 JSON”: 老项目常定义一个单工具并强制
"type": "tool",借工具入参拿到符合 Schema 的 JSON。 - Token 预检复用了同一份请求体: 把带强制
tool_choice的字典原封不动传给 token 计数接口,也会触发同一条校验拦截。
修复 1:改用 tool_choice: {"type": "auto"} + 严格工具模式(Strict Tool Use)
把 tool_choice 换成 {"type": "auto"}(或直接删掉该字段走默认值),开启严格工具校验保证参数结构合规,并在 system 提示词中明确指示模型调用目标工具。
开启严格工具模式的两个要点:
- 在工具定义顶层(与
name、description、input_schema平级)加上"strict": true。 - 在
input_schema内显式声明"additionalProperties": false与"required"。
错误请求(POST /v1/messages)
{
"model": "claude-sonnet-5-5",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather for a city.",
"input_schema": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}
}
],
"tool_choice": {
"type": "tool",
"name": "get_weather"
},
"messages": [
{
"role": "user",
"content": "What is the weather in Tokyo?"
}
]
}修复后的请求(POST /v1/messages)
{
"model": "claude-sonnet-5-5",
"max_tokens": 1024,
"system": "Always call the get_weather tool to answer weather questions.",
"tools": [
{
"name": "get_weather",
"description": "Get the current weather for a city.",
"strict": true,
"input_schema": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"],
"additionalProperties": false
}
}
],
"tool_choice": {
"type": "auto"
},
"messages": [
{
"role": "user",
"content": "What is the weather in Tokyo?"
}
]
}修复 2:只为拿固定 JSON 时,直接改用结构化输出
按 Anthropic 官方建议,如果你当初配置强制工具调用只是为了让最终回复按固定 JSON Schema 输出,无需再绕道 tools + tool_choice,直接改用 output_config.format 结构化输出即可(写法示例见 assistant prefill 修复指南)。
通过网关调用时的说明
这条 HTTP 400 来自上游 API 的请求体校验,像半页酥(byesu)这样的 AI API 网关会原样返回报错。无论你是直连上游还是通过 https://byesu.com 调用 claude-opus-5-5、claude-sonnet-5-5 或 claude-fable-5-2,只要把 "tool" 和 "any" 替换为 {"type": "auto"} 或 {"type": "none"} 即可修复。若使用 OpenAI 兼容接口(/v1/chat/completions)并传了 tool_choice: "required" 或指定函数名,底层映射到上述模型时同样会触发该限制,处理方式一致。更多排查可参考 常见错误总览、prompt is too long 排查 与 Claude Code 自定义 Base URL 报错。
常见问题
哪些 Claude 模型会拒绝 tool_choice 的 "tool" 和 "any"?
Claude Fable 5.2、Claude Mythos 5.2、Claude Opus 5.5、Claude Sonnet 5.5、Claude Fable 5.1 与 Claude Mythos 5.1 均不支持强制工具调用,传入 type 为 "tool" 或 "any" 的 tool_choice 时会返回 HTTP 400 invalid_request_error。
Claude Opus 5.5 和 Sonnet 5.5 允许哪些 tool_choice 取值?
这两款模型仅接受 {"type": "auto"}(默认值)和 {"type": "none"}。也可以直接省略 tool_choice 字段以使用默认的 "auto" 行为。
不能强制 tool_choice 时,怎么保证稳定调工具或返回固定 JSON?
将 tool_choice 设为 {"type": "auto"},同时开启严格工具模式(在工具定义顶层设 "strict": true,并在 input_schema 中补齐 additionalProperties: false 与 required),再在 system 提示词里点名要求调用该工具。如果当初强制调工具只是为了拿固定格式的 JSON,请直接改用 output_config.format 结构化输出。
Token 计数接口(count_tokens)也会报同样的 400 吗?
会。在这些模型上向 token 计数端点传入 {"type": "any"} 或 {"type": "tool", "name": "..."},同样会触发完全一致的 HTTP 400 invalid_request_error。