mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(tool_choice): drop an allowed_tools choice under drop_params and refuse conflicting flat entries
Backport of #50467 to lts/1.108.x.
Cherry-picked from 361aecae72 (litellm_allowed_tools_flat_tool_choice). Azure chat config import conflict resolved to the line's imports.
This commit is contained in:
parent
75b20375eb
commit
5b05913f16
8 changed files with 97 additions and 32 deletions
|
|
@ -64,21 +64,22 @@ def is_allowed_tools_tool_choice(tool_choice: object) -> bool:
|
|||
return False
|
||||
|
||||
|
||||
def raise_if_allowed_tools_tool_choice_unsupported(
|
||||
def drops_unsupported_allowed_tools_tool_choice(
|
||||
tool_choice: object,
|
||||
custom_llm_provider: str,
|
||||
model: str,
|
||||
) -> None:
|
||||
"""Providers outside the set would silently send the request with no tool restriction at all, so the call stops
|
||||
here with a 400. ``drop_params`` is deliberately not consulted: dropping a restriction widens what the model may call."""
|
||||
drop_params: bool,
|
||||
) -> bool:
|
||||
if not is_allowed_tools_tool_choice(tool_choice) or custom_llm_provider in ALLOWED_TOOLS_TOOL_CHOICE_PROVIDERS:
|
||||
return
|
||||
return False
|
||||
if drop_params:
|
||||
return True
|
||||
raise BadRequestError(
|
||||
message=(
|
||||
"tool_choice type 'allowed_tools' is only supported for "
|
||||
f"{', '.join(sorted(ALLOWED_TOOLS_TOOL_CHOICE_PROVIDERS))}, got custom_llm_provider={custom_llm_provider}. "
|
||||
"drop_params does not drop it, since that would let the model call tools the request excluded. "
|
||||
"Send only the allowed tools in `tools` with tool_choice='auto' or 'required' instead."
|
||||
"Send only the allowed tools in `tools` with tool_choice='auto' or 'required', or set drop_params=True "
|
||||
"to drop the tool_choice, which lets the model call any tool in `tools`."
|
||||
),
|
||||
model=model,
|
||||
llm_provider=custom_llm_provider,
|
||||
|
|
@ -92,8 +93,10 @@ def _has_name(selector: Mapping[str, object]) -> bool:
|
|||
|
||||
def _chat_allowed_tool(tool: Mapping[str, object]) -> Mapping[str, object] | None:
|
||||
tool_type: Final = tool.get("type")
|
||||
if not isinstance(tool_type, str) or tool_type not in ("function", "custom") or tool_type in tool:
|
||||
if not isinstance(tool_type, str) or tool_type not in ("function", "custom"):
|
||||
return tool
|
||||
if tool_type in tool:
|
||||
return None if "name" in tool else tool
|
||||
if not _has_name(tool):
|
||||
return None
|
||||
return {**{key: value for key, value in tool.items() if key != "name"}, tool_type: {"name": tool["name"]}}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,10 @@
|
|||
from collections.abc import Mapping
|
||||
from types import MappingProxyType
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
Final,
|
||||
cast, # noqa: TID251 # non_default_params reaches the mapper as an untyped dict
|
||||
)
|
||||
from typing import TYPE_CHECKING, Any, Final
|
||||
|
||||
from httpx._models import Headers, Response
|
||||
|
||||
import litellm
|
||||
from litellm.litellm_core_utils.allowed_tools_tool_choice import is_allowed_tools_tool_choice
|
||||
from litellm.litellm_core_utils.prompt_templates.common_utils import (
|
||||
drop_tool_reference_parts_from_tool_messages,
|
||||
flatten_combinators_and_drop_non_python_regex_patterns,
|
||||
|
|
@ -230,12 +224,10 @@ class AzureOpenAIConfig(BaseConfig):
|
|||
or (api_version_year == "2023" and api_version_month < "12")
|
||||
or (api_version_year == "2023" and api_version_month == "12" and api_version_day < "01")
|
||||
):
|
||||
if is_allowed_tools_tool_choice(
|
||||
cast(object, value) # cast-ok: widening an untyped dict value to object is always sound
|
||||
) or not (litellm.drop_params is True or (drop_params is not None and drop_params is True)):
|
||||
if not (litellm.drop_params is True or drop_params is True):
|
||||
raise UnsupportedParamsError(
|
||||
status_code=400,
|
||||
message=f"""Azure does not support 'tool_choice', for api_version={api_version}. Bump your API version to '2023-12-01-preview' or later. This parameter requires 'api_version="2023-12-01-preview"' or later. drop_params never drops an allowed_tools tool_choice, since that would let the model call tools the request excluded. Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions""",
|
||||
message=f"""Azure does not support 'tool_choice', for api_version={api_version}. Bump your API version to '2023-12-01-preview' or later. This parameter requires 'api_version="2023-12-01-preview"' or later. Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions""",
|
||||
)
|
||||
elif value == "required" and (
|
||||
api_version_year == "2024" and api_version_month <= "05"
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ from litellm.constants import (
|
|||
from litellm.exceptions import LiteLLMUnknownProvider
|
||||
from litellm.integrations.custom_logger import CustomLogger
|
||||
from litellm.litellm_core_utils.allowed_tools_tool_choice import (
|
||||
raise_if_allowed_tools_tool_choice_unsupported,
|
||||
drops_unsupported_allowed_tools_tool_choice,
|
||||
)
|
||||
from litellm.litellm_core_utils.asyncify import asyncify, run_async_function
|
||||
from litellm.litellm_core_utils.audio_utils.utils import (
|
||||
|
|
@ -200,7 +200,7 @@ from litellm.utils import (
|
|||
|
||||
from ._logging import verbose_logger
|
||||
from .caching.caching import disable_cache, enable_cache, update_cache
|
||||
from .litellm_core_utils.core_helpers import normalize_drop_params, safe_deep_copy
|
||||
from .litellm_core_utils.core_helpers import drops_unsupported_params, normalize_drop_params, safe_deep_copy
|
||||
from .litellm_core_utils.fallback_utils import (
|
||||
async_completion_with_fallbacks,
|
||||
completion_with_fallbacks,
|
||||
|
|
@ -5353,7 +5353,14 @@ def completion(
|
|||
messages = validate_and_fix_openai_messages(messages=messages)
|
||||
tools = validate_and_fix_openai_tools(tools=tools)
|
||||
# validate tool_choice
|
||||
tool_choice = validate_chat_completion_tool_choice(tool_choice=tool_choice, model=model)
|
||||
drops_unsupported: Final = drops_unsupported_params(
|
||||
cast("Mapping[str, object]", kwargs).get("drop_params") # cast-ok: kwargs is the untyped request dict
|
||||
)
|
||||
tool_choice = validate_chat_completion_tool_choice(
|
||||
tool_choice=tool_choice,
|
||||
model=model,
|
||||
drop_params=drops_unsupported,
|
||||
)
|
||||
# validate optional params
|
||||
stop = validate_openai_optional_params(stop=stop)
|
||||
thinking = validate_and_fix_thinking_param(thinking=thinking)
|
||||
|
|
@ -5610,12 +5617,13 @@ def completion(
|
|||
if not _is_claude_tool_target(custom_llm_provider=custom_llm_provider, model=model):
|
||||
tools = _drop_anthropic_only_tool_keys(tools=tools)
|
||||
|
||||
raise_if_allowed_tools_tool_choice_unsupported(
|
||||
drops_tool_choice: Final = drops_unsupported_allowed_tools_tool_choice(
|
||||
tool_choice=cast( # cast-ok: widening an untyped dict to object is always sound, the helper validates it with pydantic
|
||||
object, tool_choice
|
||||
),
|
||||
custom_llm_provider=custom_llm_provider,
|
||||
model=model,
|
||||
drop_params=drops_unsupported,
|
||||
)
|
||||
|
||||
if provider_specific_header is not None:
|
||||
|
|
@ -5740,7 +5748,7 @@ def completion(
|
|||
"response_format": response_format,
|
||||
"seed": seed,
|
||||
"tools": tools,
|
||||
"tool_choice": tool_choice,
|
||||
"tool_choice": None if drops_tool_choice else tool_choice,
|
||||
"max_retries": max_retries,
|
||||
"logprobs": logprobs,
|
||||
"top_logprobs": top_logprobs,
|
||||
|
|
|
|||
|
|
@ -8833,6 +8833,7 @@ def validate_chat_completion_user_messages(messages: list[AllMessageValues]):
|
|||
def validate_chat_completion_tool_choice(
|
||||
tool_choice: dict | str | None,
|
||||
model: str = "",
|
||||
drop_params: bool = False,
|
||||
) -> dict | str | None:
|
||||
"""
|
||||
Confirm the tool choice is passed in the OpenAI format.
|
||||
|
|
@ -8851,6 +8852,8 @@ def validate_chat_completion_tool_choice(
|
|||
object, tool_choice
|
||||
)
|
||||
)
|
||||
if allowed_tools is None and drop_params:
|
||||
return None
|
||||
if allowed_tools is None:
|
||||
raise BadRequestError(
|
||||
message=(
|
||||
|
|
|
|||
|
|
@ -115,6 +115,11 @@ def test_responses_layout_round_trips_back_to_what_the_caller_sent():
|
|||
{"type": "allowed_tools", "mode": "none", "tools": [{"type": "function", "name": "get_time"}]},
|
||||
{"type": "allowed_tools", "mode": "auto", "tools": [{"type": "function"}]},
|
||||
{"type": "allowed_tools", "mode": "auto", "tools": [{"type": "custom", "name": ""}]},
|
||||
{
|
||||
"type": "allowed_tools",
|
||||
"mode": "required",
|
||||
"tools": [{"type": "function", "name": "get_time", "function": {"name": "get_weather"}}],
|
||||
},
|
||||
{
|
||||
"type": "allowed_tools",
|
||||
"mode": "auto",
|
||||
|
|
|
|||
|
|
@ -92,10 +92,21 @@ def test_map_openai_params_drop_params_still_drops_a_named_tool_choice_on_old_ap
|
|||
assert "tool_choice" not in mapped
|
||||
|
||||
|
||||
def test_map_openai_params_drop_params_never_drops_allowed_tools_on_old_api_version():
|
||||
with pytest.raises(litellm.UnsupportedParamsError, match="allowed_tools") as exc:
|
||||
_map_tool_choice_on_pre_tool_choice_api_version(
|
||||
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "function"}]}}
|
||||
def test_map_openai_params_drop_params_drops_allowed_tools_on_old_api_version():
|
||||
mapped: Final = _map_tool_choice_on_pre_tool_choice_api_version(
|
||||
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "function"}]}}
|
||||
)
|
||||
assert "tool_choice" not in mapped
|
||||
|
||||
|
||||
def test_map_openai_params_allowed_tools_on_old_api_version_is_a_400_without_drop_params():
|
||||
with pytest.raises(litellm.UnsupportedParamsError, match="2023-12-01-preview") as exc:
|
||||
AzureOpenAIConfig().map_openai_params(
|
||||
{"tool_choice": {"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "function"}]}}},
|
||||
{},
|
||||
"gpt-4o",
|
||||
drop_params=False,
|
||||
api_version="2023-07-01-preview",
|
||||
)
|
||||
assert exc.value.status_code == 400
|
||||
|
||||
|
|
|
|||
|
|
@ -462,10 +462,10 @@ def test_completion_forwards_allowed_tools_tool_choice_to_openai_compatible_chat
|
|||
assert [tool["function"]["name"] for tool in sent["tools"]] == ["get_time", "get_weather"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("drop_params", [False, True])
|
||||
@pytest.mark.parametrize("tool_choice", [_ALLOWED_TOOLS_TOOL_CHOICE, _RESPONSES_LAYOUT_ALLOWED_TOOLS_TOOL_CHOICE])
|
||||
@pytest.mark.parametrize("model", ["anthropic/claude-sonnet-4-5", "gemini/gemini-2.5-flash", "bedrock/converse/x"])
|
||||
def test_completion_allowed_tools_tool_choice_is_a_400_where_the_provider_cannot_enforce_it(
|
||||
respx_mock: respx.MockRouter, model: str, drop_params: bool
|
||||
respx_mock: respx.MockRouter, model: str, tool_choice: dict[str, object]
|
||||
):
|
||||
with pytest.raises(
|
||||
litellm.BadRequestError, match=r"allowed_tools.*only supported for azure, litellm_proxy, openai"
|
||||
|
|
@ -474,8 +474,7 @@ def test_completion_allowed_tools_tool_choice_is_a_400_where_the_provider_cannot
|
|||
model=model,
|
||||
messages=[{"role": "user", "content": "What time is it?"}],
|
||||
tools=_TWO_TOOLS,
|
||||
tool_choice=_ALLOWED_TOOLS_TOOL_CHOICE,
|
||||
drop_params=drop_params,
|
||||
tool_choice=tool_choice,
|
||||
api_key="fake_api_key",
|
||||
aws_access_key_id="fake",
|
||||
aws_secret_access_key="fake",
|
||||
|
|
@ -485,6 +484,38 @@ def test_completion_allowed_tools_tool_choice_is_a_400_where_the_provider_cannot
|
|||
assert not respx_mock.calls
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("model", "tool_choice"),
|
||||
[
|
||||
("hosted_vllm/my-model", _ALLOWED_TOOLS_TOOL_CHOICE),
|
||||
("hosted_vllm/my-model", _RESPONSES_LAYOUT_ALLOWED_TOOLS_TOOL_CHOICE),
|
||||
("openai/gpt-5.5", {"type": "allowed_tools", "mode": "required", "tools": []}),
|
||||
("openai/gpt-5.5", {"type": "allowed_tools", "mode": "required", "tools": [{"type": "function"}]}),
|
||||
],
|
||||
)
|
||||
def test_completion_drop_params_drops_an_allowed_tools_tool_choice_it_cannot_send(
|
||||
respx_mock: respx.MockRouter, openai_api_response: dict[str, object], model: str, tool_choice: dict[str, object]
|
||||
):
|
||||
mock_route: Final = respx_mock.post(url__regex=r"http://localhost:12346/.*chat/completions.*").mock(
|
||||
return_value=httpx.Response(status_code=200, json=openai_api_response)
|
||||
)
|
||||
|
||||
litellm.completion(
|
||||
model=model,
|
||||
messages=[{"role": "user", "content": "What time is it?"}],
|
||||
tools=_TWO_TOOLS,
|
||||
tool_choice=tool_choice,
|
||||
drop_params=True,
|
||||
api_base="http://localhost:12346/v1",
|
||||
api_key="fake_api_key",
|
||||
)
|
||||
|
||||
assert mock_route.called
|
||||
sent: Final = json.loads(respx_mock.calls[0].request.content)
|
||||
assert "tool_choice" not in sent
|
||||
assert [tool["function"]["name"] for tool in sent["tools"]] == ["get_time", "get_weather"]
|
||||
|
||||
|
||||
_CLAUDE_TOOL_SEARCH_CALL: Final = {
|
||||
"id": "srvtoolu_01search",
|
||||
"type": "function",
|
||||
|
|
|
|||
|
|
@ -7487,6 +7487,18 @@ def test_validate_tool_choice_allowed_tools_malformed_is_a_400(tool_choice: dict
|
|||
assert '"mode": "auto" | "required"' in str(exc_info.value)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_vcr_outcome_gate", "setup_and_teardown")
|
||||
@pytest.mark.parametrize(
|
||||
"tool_choice",
|
||||
[
|
||||
{"type": "allowed_tools", "mode": "required", "tools": []},
|
||||
{"type": "allowed_tools", "allowed_tools": {"mode": "none", "tools": [{"type": "function"}]}},
|
||||
],
|
||||
)
|
||||
def test_validate_tool_choice_allowed_tools_malformed_is_dropped_under_drop_params(tool_choice: dict[str, object]):
|
||||
assert validate_chat_completion_tool_choice(tool_choice, model=MODEL, drop_params=True) is None
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_vcr_outcome_gate", "setup_and_teardown")
|
||||
@pytest.mark.parametrize("tool_choice", [123, []])
|
||||
def test_validate_tool_choice_invalid_type_is_a_400(tool_choice):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue