fix: ToolChoice.None 映射错误导致强制调用工具#2226
Closed
BG0527 wants to merge 4 commits into
Closed
Conversation
02fddba to
78e951e
Compare
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… fix-AnthropicTools-mapping
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
jujn
requested changes
Jul 15, 2026
Comment on lines
+58
to
+60
| // ToolChoice.None means "prevent the model from calling any tools". | ||
| // The Anthropic API has no native "none" value for tool_choice, | ||
| // so the only correct way to enforce this is to omit tools entirely. |
Collaborator
There was a problem hiding this comment.
当前项目中依赖的 anthropic-sdk(com.anthropic:anthropic-java:2.14.0)中有 ToolChoice.ofNone() 选项,是不是应该保留 tools,并在 applyToolChoice() 显示设置:builder.toolChoice(ofNone(ToolChoiceNone.builder().build()))
这样才正确表达“这些工具存在,但本轮禁止调用工具”?
Contributor
Author
There was a problem hiding this comment.
感谢您的宝贵评论!我之前没有区分两种禁用工具的实现逻辑,忽略了 SDK 自带的ToolChoice.ofNone()标准写法。
现已按照您的建议修改代码:保留tools字段,通过显式配置toolChoice.ofNone()实现 “存在工具、但本轮禁止调用” 的语义,原有防御日志也做保留。相关修改已提交新 PR #2232
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.
关联 issue
AgentScope-Java Version
2.0.1-SNAPSHOT
Description
Background
ToolChoice抽象定义了四种工具调用策略:Auto(模型自主决定)、None(禁止工具调用,仅返回文本)、Required(强制调用工具)和Specific(强制调用指定工具)。其中ToolChoice.None的语义明确为"阻止模型调用任何工具"。OpenAI API 原生支持
tool_choice: "none"值,可直接表达此语义。但 Anthropic API 的tool_choice参数仅支持auto、any和tool三种类型,不存在none值。Root Cause
AnthropicToolsHelper.applyToolChoice()在处理ToolChoice.None时,将其映射为 Anthropic SDK 的ToolChoiceAny(即 API 值"any")。"any"的语义是强制模型必须调用某个工具,与None的预期语义"禁止工具调用"完全相反。原代码注释
// Anthropic doesn't have None, use Any instead表明这是一个有意识的 fallback 决策,但选择了语义错误的映射方向。影响: 当调用方指定
ToolChoice.None试图获取纯文本响应时,模型反而被强制进行工具调用,导致非预期行为。Fix
在
AnthropicToolsHelper.applyTools()方法中,检测到ToolChoice.None时直接返回,不向MessageCreateParams.Builder添加任何工具定义。Anthropic API 在请求中不包含tools字段时,模型不会进行工具调用,自然产生纯文本响应——这是 Anthropic 平台上表达"禁用工具"的唯一正确方式。applyToolChoice()移除了None → any的错误映射,添加防御性日志以防此方法被直接调用时误传None。Changed files:
1.
agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicToolsHelper.javaapplyTools(): 在工具添加逻辑之前增加ToolChoice.None检查,若匹配则记录 debug 日志并直接返回,不向请求添加任何工具applyToolChoice(): 移除ToolChoice.None → ToolChoiceAny的错误映射,增加防御性 warn 日志2.
agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicToolsHelperTest.javatestApplyToolChoiceNone(): 修正断言,验证 None 时 tools 为空且 toolChoice 为空(原错误断言isAny())testApplyToolChoiceNoneWithMultipleTools(): 验证多个工具场景下 None 同样抑制所有工具testApplyToolChoiceNoneWithNullOptions(): 验证 options 为 null 时工具正常应用testApplyToolChoiceNoneWithEmptyOptions(): 验证无 toolChoice 的空 options 时工具正常应用Checklist
mvn spotless:applymvn test)