diff --git a/litellm/utils.py b/litellm/utils.py index e2820f9c682..43650eb9e72 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -9020,6 +9020,15 @@ def validate_chat_completion_tool_choice( if tool_choice_type in ("auto", "none", "required") and "function" not in tool_choice: return tool_choice_type + # OpenAI also allows constraining which tools the model may call: + # {"type": "allowed_tools", "allowed_tools": {"mode": ..., "tools": [...]}}. + # Pass it through untouched so downstream providers see the full payload. + if tool_choice_type == "allowed_tools" and isinstance( + tool_choice.get("allowed_tools"), # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # raw dict payload + dict, + ): + return tool_choice # pyright: ignore[reportUnknownVariableType] # narrowed raw dict payload + # Standard OpenAI format: {"type": "function", "function": {...}} if tool_choice.get("type") is None or tool_choice.get("function") is None: raise BadRequestError( diff --git a/tests/unit/test_validate_tool_choice.py b/tests/unit/test_validate_tool_choice.py new file mode 100644 index 00000000000..a28aee87c48 --- /dev/null +++ b/tests/unit/test_validate_tool_choice.py @@ -0,0 +1,93 @@ +import re +from typing import Final + +import pytest + +import litellm +from litellm.utils import validate_chat_completion_tool_choice + +MODEL: Final = "anthropic/claude-haiku-4-5" + + +def test_validate_tool_choice_none(): + """Test that None is returned as-is.""" + result = validate_chat_completion_tool_choice(None, model=MODEL) + assert result is None + + +def test_validate_tool_choice_string(): + """Test that string values are returned as-is.""" + assert validate_chat_completion_tool_choice("auto", model=MODEL) == "auto" + assert validate_chat_completion_tool_choice("none", model=MODEL) == "none" + assert validate_chat_completion_tool_choice("required", model=MODEL) == "required" + + +def test_validate_tool_choice_standard_dict(): + """Test standard OpenAI format with function.""" + tool_choice = {"type": "function", "function": {"name": "my_function"}} + result = validate_chat_completion_tool_choice(tool_choice, model=MODEL) + assert result == tool_choice + + +def test_validate_tool_choice_cursor_format(): + """Cursor IDE format {"type": "auto"} is unwrapped to the bare string.""" + assert validate_chat_completion_tool_choice({"type": "auto"}, model=MODEL) == "auto" + assert validate_chat_completion_tool_choice({"type": "none"}, model=MODEL) == "none" + assert validate_chat_completion_tool_choice({"type": "required"}, model=MODEL) == "required" + + +def test_validate_tool_choice_allowed_tools_passthrough(): + """Chat completions accepts the allowed_tools shape and keeps its payload intact.""" + tool_choice = { + "type": "allowed_tools", + "allowed_tools": { + "mode": "required", + "tools": [{"type": "function", "function": {"name": "get_time"}}], + }, + } + assert validate_chat_completion_tool_choice(tool_choice, model=MODEL) is tool_choice + + +def test_validate_tool_choice_allowed_tools_without_payload_is_a_400(): + """allowed_tools without its nested object stays a caller error.""" + tool_choice = {"type": "allowed_tools"} + with pytest.raises(litellm.BadRequestError, match="Invalid tool choice"): + validate_chat_completion_tool_choice(tool_choice, model=MODEL) + + +@pytest.mark.parametrize( + "tool_choice", + [ + {}, + {"type": "invalid"}, + {"type": "function"}, + {"name": "lookup_fruit"}, + {"type": "file_search"}, + ], +) +def test_validate_tool_choice_invalid_dict_is_a_400(tool_choice): + """A dict shape chat completions cannot carry is the caller's mistake: a 400 that names the field, never a 500.""" + with pytest.raises( + litellm.BadRequestError, match=f"Invalid tool choice, tool_choice={re.escape(str(tool_choice))}\\. Please ensure" + ) as exc_info: + validate_chat_completion_tool_choice(tool_choice, model=MODEL) + assert exc_info.value.status_code == 400 + assert exc_info.value.model == MODEL + + +@pytest.mark.parametrize("tool_choice", [123, []]) +def test_validate_tool_choice_invalid_type_is_a_400(tool_choice): + """A non-str, non-dict tool_choice is rejected as a 400 that names the type it got.""" + with pytest.raises( + litellm.BadRequestError, match=f"Got={re.escape(str(type(tool_choice)))}\\. Expecting str, or dict\\." + ) as exc_info: + validate_chat_completion_tool_choice(tool_choice, model=MODEL) + assert exc_info.value.status_code == 400 + + +def test_validate_tool_choice_without_model_is_still_a_400(): + """Callers that predate the model argument keep getting a 400, with an empty model on the error.""" + with pytest.raises(litellm.BadRequestError, match="Invalid tool choice") as exc_info: + validate_chat_completion_tool_choice({"type": "bogus"}) + assert exc_info.value.status_code == 400 + assert exc_info.value.model == ""