fix: Align Anthropic ToolChoice.None mapping with disable tools intent#2232
Merged
Conversation
d21da59 to
e6bacff
Compare
5 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.None的语义是"禁止模型调用任何工具",即本轮只返回文本回复。Anthropic API 原生支持tool_choice: none选项(SDKcom.anthropic:anthropic-java:2.14.0中提供ToolChoice.ofNone(ToolChoiceNone)方法)。Root Cause
AnthropicToolsHelper.applyToolChoice()将ToolChoice.None映射为ToolChoice.ofAny(ToolChoiceAny),而any在 Anthropic API 中表示"必须调用工具",与None的"禁止调用工具"语义完全相反。这导致设置了ToolChoice.None时,模型反而被强制调用工具。原代码(L95-97):
注释错误地声称"Anthropic doesn't have None",实际上 Anthropic SDK 2.14.0 已提供
ToolChoiceNone和ToolChoice.ofNone()。Fix
ToolChoice.None映射从ofAny(ToolChoiceAny)改为ofNone(ToolChoiceNone),正确表达"工具存在但本轮禁止调用"的语义,与 OpenAI、Gemini、DashScope 等其他扩展保持一致。tools为空但options中设置了toolChoice时,增加 debug 日志提示,便于排查"设了 None 但没生效"的困惑场景。testApplyToolChoiceNone增加排他性断言(!isAny()、!isAuto())和 tools 列表完整性断言testApplyToolChoiceNoneWithEmptyToolsSkipsToolChoice边界测试Changed files:
1.
agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicToolsHelper.javaofNone、ToolChoiceNoneapplyToolChoice():ToolChoice.None分支从ofAny(ToolChoiceAny.builder().build())改为ofNone(ToolChoiceNone.builder().build())applyTools(): 空 tools 提前返回时增加 debug 日志2.
agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicToolsHelperTest.javatestApplyToolChoiceNone: 断言从isAny()改为isNone(),新增排他性断言和 tools 列表完整性断言testApplyToolChoiceNoneWithEmptyToolsSkipsToolChoice边界测试Checklist
mvn spotless:applymvn test)