Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(anthropic): handle tool_choice type 'none' in messages API (#24457)

BillionToken947931858eb7
diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
index e5d2b4ce782..20fa4f125de 100644
--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
@@ -782,6 +782,8 @@ class LiteLLMAnthropicMessagesAdapter:
return ChatCompletionToolChoiceObjectParam(
type="function", function=tc_function_param
)
+ elif tool_choice["type"] == "none":
+ return "none"
else:
raise ValueError(
"Incompatible tool choice param submitted - {}".format(tool_choice)
diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
index e6e96868f33..670388b7c03 100644
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
@@ -2162,16 +2162,19 @@ class TestTranslateAnthropicOutputFormatToOpenAI:
assert sorted(schema["required"]) == ["age", "email", "name"]
def test_invalid_output_format_returns_none(self):
- assert (
- self.adapter.translate_anthropic_output_format_to_openai("invalid") is None
- )
- assert (
- self.adapter.translate_anthropic_output_format_to_openai({"type": "text"})
- is None
- )
- assert (
- self.adapter.translate_anthropic_output_format_to_openai(
- {"type": "json_schema"}
- )
- is None
- )
+ assert self.adapter.translate_anthropic_output_format_to_openai("invalid") is None
+ assert self.adapter.translate_anthropic_output_format_to_openai({"type": "text"}) is None
+ assert self.adapter.translate_anthropic_output_format_to_openai({"type": "json_schema"}) is None
+
+
+def test_translate_anthropic_tool_choice_none():
+ """
+ Regression test for issue #24443.
+
+ tool_choice={"type": "none"} should be translated to "none" for OpenAI format,
+ not raise a ValueError.
+ """
+ adapter = LiteLLMAnthropicMessagesAdapter()
+
+ result = adapter.translate_anthropic_tool_choice_to_openai({"type": "none"})
+ assert result == "none"