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(azure): drop tool_choice without tools

David Steelee254377049ca
diff --git a/litellm/llms/azure/chat/gpt_transformation.py b/litellm/llms/azure/chat/gpt_transformation.py
index 6d17a1359bc..0debbe4f74d 100644
--- a/litellm/llms/azure/chat/gpt_transformation.py
+++ b/litellm/llms/azure/chat/gpt_transformation.py
@@ -280,10 +280,17 @@ class AzureOpenAIConfig(BaseConfig):
ordered_messages: Final = system_messages_first(messages) if litellm.openai_system_messages_first else messages
stripped_messages: Final = drop_tool_reference_parts_from_tool_messages(ordered_messages)
azure_messages: Final = convert_to_azure_openai_messages(hoist_images_from_tool_messages(stripped_messages))
+ request_params: Final = {
+ key: value
+ for key, value in optional_params.items()
+ if key != "tool_choice"
+ or optional_params.get("tools")
+ or optional_params.get("functions")
+ }
return {
"model": model,
"messages": azure_messages,
- **optional_params,
+ **request_params,
**sanitized_tools_update(optional_params),
}
diff --git a/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py b/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py
index e8b98c696e1..92a8124d8a9 100644
--- a/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py
+++ b/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py
@@ -333,3 +333,126 @@ class TestAzureToolSchemaCombinatorFlattening:
)
assert "tools" not in request
assert request["temperature"] == 0.2
+
+
[email protected]("tool_choice", ["none", "auto"])
+def test_azure_drops_tool_choice_without_tools_or_functions(tool_choice: str) -> None:
+ optional_params = {"tool_choice": tool_choice, "temperature": 0.2}
+ request = AzureOpenAIConfig().transform_request(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params=optional_params,
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert "tool_choice" not in request
+ assert request["temperature"] == 0.2
+ assert optional_params["tool_choice"] == tool_choice
+
+
+def test_azure_tools_empty_drops_tool_choice() -> None:
+ request = AzureOpenAIConfig().transform_request(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"tools": [], "tool_choice": "auto"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert request["tools"] == []
+ assert "tool_choice" not in request
+
+
+def test_azure_functions_empty_drops_tool_choice() -> None:
+ request = AzureOpenAIConfig().transform_request(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"functions": [], "tool_choice": "none"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert request["functions"] == []
+ assert "tool_choice" not in request
+
+
+def test_azure_preserves_tool_choice_with_tools() -> None:
+ tools = [{"type": "function", "function": {"name": "get_weather", "parameters": {}}}]
+ request = AzureOpenAIConfig().transform_request(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"tools": tools, "tool_choice": "auto"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert request["tools"] == tools
+ assert request["tool_choice"] == "auto"
+
+
+def test_azure_preserves_tool_choice_with_legacy_functions() -> None:
+ functions = [{"name": "get_weather", "parameters": {}}]
+ request = AzureOpenAIConfig().transform_request(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"functions": functions, "tool_choice": "auto"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert request["functions"] == functions
+ assert request["tool_choice"] == "auto"
+
+
+def test_azure_preserves_function_call_without_tools() -> None:
+ request = AzureOpenAIConfig().transform_request(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"function_call": "none", "tool_choice": "auto"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert request["function_call"] == "none"
+ assert "tool_choice" not in request
+
+
+def test_azure_gpt5_drops_tool_choice_without_tools() -> None:
+ request = AzureOpenAIGPT5Config().transform_request(
+ model="gpt5_series/gpt-5.6-sol",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"tool_choice": "none"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert request["model"] == "gpt-5.6-sol"
+ assert "tool_choice" not in request
+
+
+async def test_azure_async_transform_drops_tool_choice_without_tools() -> None:
+ request = await AzureOpenAIConfig().async_transform_request(
+ model="gpt-4o",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"tool_choice": "none"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert "tool_choice" not in request
+
+
+async def test_azure_gpt5_async_transform_drops_tool_choice_without_tools() -> None:
+ request = await AzureOpenAIGPT5Config().async_transform_request(
+ model="gpt5_series/gpt-5.6-sol",
+ messages=[{"role": "user", "content": "hi"}],
+ optional_params={"tool_choice": "auto"},
+ litellm_params={"custom_llm_provider": "azure"},
+ headers={},
+ )
+
+ assert request["model"] == "gpt-5.6-sol"
+ assert "tool_choice" not in request
diff --git a/tests/test_litellm/llms/azure/chat/test_azure_chat_o_series_transformation.py b/tests/test_litellm/llms/azure/chat/test_azure_chat_o_series_transformation.py
index 9db9ab971a0..57d60df3a11 100644
--- a/tests/test_litellm/llms/azure/chat/test_azure_chat_o_series_transformation.py
+++ b/tests/test_litellm/llms/azure/chat/test_azure_chat_o_series_transformation.py
@@ -14,7 +14,7 @@ async def test_azure_chat_o_series_transformation():
provider_config = AzureOpenAIO1Config()
model = "o_series/web-interface-o1-mini"
messages = [{"role": "user", "content": "Hello, how are you?"}]
- optional_params = {}
+ optional_params = {"tool_choice": "none"}
litellm_params = {}
headers = {}
@@ -23,6 +23,7 @@ async def test_azure_chat_o_series_transformation():
)
print(response)
assert response["model"] == "web-interface-o1-mini"
+ assert "tool_choice" not in response
def test_azure_o_series_transform_request_flattens_top_level_anyof():