From 7356d096e9ed6254b53a66e6a000968cf3454e2b Mon Sep 17 00:00:00 2001 From: ANISAYAK MITRA <266799942+anisayakmitra-in@users.noreply.github.com> Date: Sat, 24 Oct 2026 13:17:02 +0530 Subject: [PATCH 1/2] fix(mock_completion): use mock response when mock_response is empty string (#48162) --- litellm/main.py | 6 ++- litellm/utils.py | 2 +- tests/unit/test_mock_response_empty_string.py | 49 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_mock_response_empty_string.py diff --git a/litellm/main.py b/litellm/main.py index 327a7a1c4b5..646fa0d4bc5 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -6027,7 +6027,11 @@ def completion( custom_llm_provider=custom_llm_provider, model_id=optional_params.get("model_id"), ) - if mock_response or mock_tool_calls or mock_timeout: + if should_run_mock_completion( + mock_response=mock_response, + mock_tool_calls=mock_tool_calls, + mock_timeout=mock_timeout, + ): kwargs.pop("mock_timeout", None) # remove for any fallbacks triggered return mock_completion( model, diff --git a/litellm/utils.py b/litellm/utils.py index 809a94cd93e..a3fc161f6d6 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -11102,7 +11102,7 @@ def should_run_mock_completion( mock_tool_calls: object | None, mock_timeout: object | None, ) -> bool: - if mock_response or mock_tool_calls or mock_timeout: + if mock_response is not None or mock_tool_calls or mock_timeout: return True return False diff --git a/tests/unit/test_mock_response_empty_string.py b/tests/unit/test_mock_response_empty_string.py new file mode 100644 index 00000000000..605bf5972e2 --- /dev/null +++ b/tests/unit/test_mock_response_empty_string.py @@ -0,0 +1,49 @@ +import pytest +import litellm +from litellm.utils import should_run_mock_completion + + +def test_should_run_mock_completion_with_empty_string(): + assert ( + should_run_mock_completion( + mock_response="", mock_tool_calls=None, mock_timeout=None + ) + is True + ) + assert ( + should_run_mock_completion( + mock_response=None, mock_tool_calls=None, mock_timeout=None + ) + is False + ) + assert ( + should_run_mock_completion( + mock_response="test", mock_tool_calls=None, mock_timeout=None + ) + is True + ) + + +def test_completion_mock_response_empty_string(): + response = litellm.completion( + model="openai/gpt-4o-mini", + messages=[{"role": "user", "content": "hi"}], + mock_response="", + api_base="http://127.0.0.1:9", + api_key="sk-test", + num_retries=0, + ) + assert response.choices[0].message.content == "" + + +@pytest.mark.asyncio +async def test_acompletion_mock_response_empty_string(): + response = await litellm.acompletion( + model="openai/gpt-4o-mini", + messages=[{"role": "user", "content": "hi"}], + mock_response="", + api_base="http://127.0.0.1:9", + api_key="sk-test", + num_retries=0, + ) + assert response.choices[0].message.content == "" From 1ecf39b9b2e64cba17faa7aaf1347e4d964f9187 Mon Sep 17 00:00:00 2001 From: ANISAYAK MITRA <266799942+anisayakmitra-in@users.noreply.github.com> Date: Sat, 24 Oct 2026 13:36:09 +0530 Subject: [PATCH 2/2] fix(types): avoid introducing unknown argument type in mock dispatch --- litellm/main.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/litellm/main.py b/litellm/main.py index 646fa0d4bc5..df7e6b85fab 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -6027,11 +6027,7 @@ def completion( custom_llm_provider=custom_llm_provider, model_id=optional_params.get("model_id"), ) - if should_run_mock_completion( - mock_response=mock_response, - mock_tool_calls=mock_tool_calls, - mock_timeout=mock_timeout, - ): + if mock_response is not None or mock_tool_calls or mock_timeout: kwargs.pop("mock_timeout", None) # remove for any fallbacks triggered return mock_completion( model,