mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-07 02:59:05 +00:00
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:
parent
8579332f44
commit
afe8abc768
3 changed files with 16 additions and 2 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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."
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue