diff --git a/litellm/litellm_core_utils/allowed_tools_tool_choice.py b/litellm/litellm_core_utils/allowed_tools_tool_choice.py index ae256580b93..1d254aeb559 100644 --- a/litellm/litellm_core_utils/allowed_tools_tool_choice.py +++ b/litellm/litellm_core_utils/allowed_tools_tool_choice.py @@ -249,10 +249,9 @@ def _with_builtin_as( def allowed_tools_tool_choice_with_builtin_as_function( tool_choice: object, is_builtin: Callable[[Mapping[str, object]], bool], function_name: str ) -> object: - try: - parsed: Final = _ALLOWED_TOOLS_TOOL_CHOICE.validate_python(tool_choice) - except ValidationError: + if parse_allowed_tools_tool_choice(tool_choice) is None: return tool_choice + parsed: Final = _ALLOWED_TOOLS_TOOL_CHOICE.validate_python(tool_choice) match parsed: case ChatAllowedToolsToolChoice(): nested: Final = {"type": "function", "function": {"name": function_name}} diff --git a/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py b/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py index 5d7fbbb2228..5a87f0e6434 100644 --- a/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py +++ b/tests/unit/litellm_core_utils/test_allowed_tools_tool_choice.py @@ -375,6 +375,16 @@ def test_a_function_or_mcp_entry_is_never_taken_for_a_builtin(): assert parse_allowed_tools_tool_choice(rewritten) == parse_allowed_tools_tool_choice(tool_choice) -@pytest.mark.parametrize("tool_choice", ["auto", None, {"type": "allowed_tools", "mode": "auto", "tools": []}]) +@pytest.mark.parametrize( + "tool_choice", + [ + "auto", + None, + {"type": "allowed_tools", "mode": "auto", "tools": []}, + {"type": "allowed_tools", "mode": "auto", "tools": [{"type": "web_search", "name": []}]}, + {"type": "allowed_tools", "mode": "auto", "tools": [{"type": "web_search"}, {"type": "function", "name": 3}]}, + {"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "web_search", "name": {}}]}}, + ], +) def test_any_other_or_malformed_choice_is_left_as_sent(tool_choice: object): assert allowed_tools_tool_choice_with_builtin_as_function(tool_choice, _is_web_search, "litellm_web_search") is tool_choice