mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-29 01:42:19 +00:00
fix(azure): drop tool_choice without tools
DEVX-829
This commit is contained in:
parent
a8979fe054
commit
e254377049
3 changed files with 133 additions and 2 deletions
|
|
@ -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),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -333,3 +333,126 @@ class TestAzureToolSchemaCombinatorFlattening:
|
|||
)
|
||||
assert "tools" not in request
|
||||
assert request["temperature"] == 0.2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("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
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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():
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue