From 285da69807864a9f68691ca5bb8c9c8bf50b11d0 Mon Sep 17 00:00:00 2001 From: John Bang Date: Wed, 23 Sep 2026 15:36:54 -0700 Subject: [PATCH] test(deepseek): consolidate transformation coverage --- litellm/llms/deepseek/chat/transformation.py | 1 - .../test_deepseek_thinking_tool_choice.py | 136 ------------------ .../chat/test_deepseek_chat_transformation.py | 129 ++++++++++++++++- 3 files changed, 128 insertions(+), 138 deletions(-) delete mode 100644 tests/test_litellm/llms/deepseek/chat/test_deepseek_thinking_tool_choice.py diff --git a/litellm/llms/deepseek/chat/transformation.py b/litellm/llms/deepseek/chat/transformation.py index daf3938cecc..70901858ed9 100644 --- a/litellm/llms/deepseek/chat/transformation.py +++ b/litellm/llms/deepseek/chat/transformation.py @@ -228,7 +228,6 @@ class DeepSeekChatConfig(OpenAIGPTConfig): def _normalize_thinking_tool_choice( optional_params: dict[str, object], model: str, drop_params: bool ) -> dict[str, object]: - """Relax forced choices only when the caller opted into dropping unsupported parameters.""" tool_choice: Final = optional_params.get("tool_choice") if tool_choice is None or tool_choice in ("none", "auto"): return optional_params diff --git a/tests/test_litellm/llms/deepseek/chat/test_deepseek_thinking_tool_choice.py b/tests/test_litellm/llms/deepseek/chat/test_deepseek_thinking_tool_choice.py deleted file mode 100644 index 4b5863582f3..00000000000 --- a/tests/test_litellm/llms/deepseek/chat/test_deepseek_thinking_tool_choice.py +++ /dev/null @@ -1,136 +0,0 @@ -import pytest - -import litellm -from litellm.llms.deepseek.chat.transformation import DeepSeekChatConfig - - -def _function_tool(name: str) -> dict: - return { - "type": "function", - "function": {"name": name, "parameters": {"type": "object"}}, - } - - -@pytest.mark.parametrize("is_async", [False, True]) -@pytest.mark.parametrize( - ( - "model", - "thinking", - "tool_choice", - "request_drop_params", - "global_drop_params", - "expected", - ), - [ - ( - "deepseek-v4-pro", - {"type": "enabled"}, - "required", - True, - False, - "auto", - ), - ( - "deepseek-v4-pro", - {"type": "enabled"}, - "required", - False, - True, - "auto", - ), - ( - "deepseek-v4-pro", - {"type": "enabled"}, - {"type": "function", "function": {"name": "shell"}}, - True, - False, - "auto", - ), - ("deepseek-v4-pro", {"type": "enabled"}, "none", False, False, "none"), - ("deepseek-v4-pro", {"type": "enabled"}, "auto", False, False, "auto"), - ("deepseek-v4-pro", {"type": "enabled"}, None, False, False, None), - ( - "deepseek-v4-pro", - {"type": "disabled"}, - "required", - False, - False, - "required", - ), - ( - "deepseek-chat", - {"type": "enabled"}, - "required", - False, - False, - "required", - ), - ], -) -async def test_transform_request_normalizes_tool_choice_for_thinking( - is_async: bool, - model: str, - thinking: dict[str, str], - tool_choice: object | None, - request_drop_params: bool, - global_drop_params: bool, - expected: object | None, - monkeypatch: pytest.MonkeyPatch, -): - monkeypatch.setattr(litellm, "drop_params", global_drop_params) - config = DeepSeekChatConfig() - tool_choice_param = {"tool_choice": tool_choice} if tool_choice is not None else {} - request = { - "model": model, - "messages": [{"role": "user", "content": "hi"}], - "optional_params": { - "thinking": thinking, - "tools": [_function_tool("shell"), _function_tool("read_file")], - **tool_choice_param, - }, - "litellm_params": {"drop_params": request_drop_params}, - "headers": {}, - } - - body = ( - await config.async_transform_request(**request) - if is_async - else config.transform_request(**request) - ) - - assert body.get("tool_choice") == expected - - -@pytest.mark.parametrize("is_async", [False, True]) -@pytest.mark.parametrize( - "tool_choice", - [ - "required", - {"type": "function", "function": {"name": "shell"}}, - ], -) -async def test_transform_request_rejects_forced_tool_choice_without_drop_params( - is_async: bool, - tool_choice: object, - monkeypatch: pytest.MonkeyPatch, -): - monkeypatch.setattr(litellm, "drop_params", False) - config = DeepSeekChatConfig() - request = { - "model": "deepseek-v4-pro", - "messages": [{"role": "user", "content": "hi"}], - "optional_params": { - "thinking": {"type": "enabled"}, - "tools": [_function_tool("shell"), _function_tool("read_file")], - "tool_choice": tool_choice, - }, - "litellm_params": {}, - "headers": {}, - } - - if is_async: - with pytest.raises(litellm.UnsupportedParamsError, match="drop_params=True"): - await config.async_transform_request(**request) - else: - with pytest.raises(litellm.UnsupportedParamsError, match="drop_params=True"): - config.transform_request(**request) diff --git a/tests/unit/llms/deepseek/chat/test_deepseek_chat_transformation.py b/tests/unit/llms/deepseek/chat/test_deepseek_chat_transformation.py index 3f93264d0d0..85e1039c712 100644 --- a/tests/unit/llms/deepseek/chat/test_deepseek_chat_transformation.py +++ b/tests/unit/llms/deepseek/chat/test_deepseek_chat_transformation.py @@ -1,8 +1,10 @@ +import pytest + import litellm from litellm.llms.deepseek.chat.transformation import DeepSeekChatConfig -def _function_tool(name: str) -> dict: +def _function_tool(name: str) -> dict[str, object]: return { "type": "function", "function": {"name": name, "parameters": {"type": "object"}}, @@ -109,6 +111,131 @@ def test_thinking_mode_active_bool_thinking_returns_false_without_crashing(): assert config._thinking_mode_active(model="deepseek-reasoner", optional_params={"thinking": True}) is False +@pytest.mark.parametrize("is_async", [False, True]) +@pytest.mark.parametrize( + ( + "model", + "thinking", + "tool_choice", + "request_drop_params", + "global_drop_params", + "expected", + ), + [ + ( + "deepseek-v4-pro", + {"type": "enabled"}, + "required", + True, + False, + "auto", + ), + ( + "deepseek-v4-pro", + {"type": "enabled"}, + "required", + False, + True, + "auto", + ), + ( + "deepseek-v4-pro", + {"type": "enabled"}, + {"type": "function", "function": {"name": "shell"}}, + True, + False, + "auto", + ), + ("deepseek-v4-pro", {"type": "enabled"}, "none", False, False, "none"), + ("deepseek-v4-pro", {"type": "enabled"}, "auto", False, False, "auto"), + ("deepseek-v4-pro", {"type": "enabled"}, None, False, False, None), + ( + "deepseek-v4-pro", + {"type": "disabled"}, + "required", + False, + False, + "required", + ), + ( + "deepseek-chat", + {"type": "enabled"}, + "required", + False, + False, + "required", + ), + ], +) +async def test_transform_request_normalizes_tool_choice_for_thinking( + is_async: bool, + model: str, + thinking: dict[str, str], + tool_choice: object | None, + request_drop_params: bool, + global_drop_params: bool, + expected: object | None, + monkeypatch: pytest.MonkeyPatch, +): + monkeypatch.setattr(litellm, "drop_params", global_drop_params) + config = DeepSeekChatConfig() + tool_choice_param = {"tool_choice": tool_choice} if tool_choice is not None else {} + request = { + "model": model, + "messages": [{"role": "user", "content": "hi"}], + "optional_params": { + "thinking": thinking, + "tools": [_function_tool("shell"), _function_tool("read_file")], + **tool_choice_param, + }, + "litellm_params": {"drop_params": request_drop_params}, + "headers": {}, + } + + body = ( + await config.async_transform_request(**request) + if is_async + else config.transform_request(**request) + ) + + assert body.get("tool_choice") == expected + + +@pytest.mark.parametrize("is_async", [False, True]) +@pytest.mark.parametrize( + "tool_choice", + [ + "required", + {"type": "function", "function": {"name": "shell"}}, + ], +) +async def test_transform_request_rejects_forced_tool_choice_without_drop_params( + is_async: bool, + tool_choice: object, + monkeypatch: pytest.MonkeyPatch, +): + monkeypatch.setattr(litellm, "drop_params", False) + config = DeepSeekChatConfig() + request = { + "model": "deepseek-v4-pro", + "messages": [{"role": "user", "content": "hi"}], + "optional_params": { + "thinking": {"type": "enabled"}, + "tools": [_function_tool("shell"), _function_tool("read_file")], + "tool_choice": tool_choice, + }, + "litellm_params": {}, + "headers": {}, + } + + if is_async: + with pytest.raises(litellm.UnsupportedParamsError, match="drop_params=True"): + await config.async_transform_request(**request) + else: + with pytest.raises(litellm.UnsupportedParamsError, match="drop_params=True"): + config.transform_request(**request) + + class TestDeepSeekVisionMultimodalContent: """Image content lists are forwarded only for user messages on vision models."""