Add anthropic 'none' tool choice param support (#11695)

* docs(index.md): add stable pip package

* fix(anthropic/chat/transformation.py): add 'none' tool choice mapping

Allows disabling anthropic tool calling

Maintain parity

* fix(transformation.py): if tool_choice="none" ignore 'disable_parallel_Tool_use'

unsupported param from anthropic - makes sense as the 'none' implies no tool calls are being made
This commit is contained in:
Krish Dholakia 2025-06-13 21:22:03 -07:00 • committed by GitHub
parent 8579332f44
commit afe8abc768
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 2 deletions

View file

@ -157,6 +157,8 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
)
elif tool_choice == "required":
_tool_choice = AnthropicMessagesToolChoice(type="any")
elif tool_choice == "none":
_tool_choice = AnthropicMessagesToolChoice(type="none")
elif isinstance(tool_choice, dict):
_tool_name = tool_choice.get("function", {}).get("name")
_tool_choice = AnthropicMessagesToolChoice(type="tool")
@ -166,7 +168,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
if parallel_tool_use is not None:
# Anthropic uses 'disable_parallel_tool_use' flag to determine if parallel tool use is allowed
# this is the inverse of the openai flag.
if _tool_choice is not None:
if tool_choice == "none":
pass
elif _tool_choice is not None:
_tool_choice["disable_parallel_tool_use"] = not parallel_tool_use
else: # use anthropic defaults and make sure to send the disable_parallel_tool_use flag
_tool_choice = AnthropicMessagesToolChoice(

View file

@ -7,7 +7,7 @@ from .openai import ChatCompletionCachedContent, ChatCompletionThinkingBlock
class AnthropicMessagesToolChoice(TypedDict, total=False):
type: Required[Literal["auto", "any", "tool"]]
type: Required[Literal["auto", "any", "tool", "none"]]
name: str
disable_parallel_tool_use: bool # default is false

View file

@ -209,6 +209,15 @@ def test_add_code_execution_tool():
assert tools[0]["type"] == "code_execution_20250522"
def test_map_tool_choice():
config = AnthropicConfig()
tool_choice = "none"
result = config._map_tool_choice(tool_choice=tool_choice, parallel_tool_use=True)
assert result is not None
assert result["type"] == "none"
print(result)
def test_transform_response_with_prefix_prompt():
import httpx
@ -252,3 +261,4 @@ def test_transform_response_with_prefix_prompt():
result.choices[0].message.content
== "You are a helpful assistant. The grass is green."
)