fix: honor empty mock completion responses (#48162) (#48266)

This commit is contained in:
ishaan-j-berri 2026-10-27 17:56:45 -07:00 • committed by GitHub
parent f277f1e260
commit 7a23e497bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 1 deletions

View file

@ -6062,7 +6062,7 @@ 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 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,

View file

@ -0,0 +1,46 @@
from unittest.mock import patch
import pytest
import litellm
@pytest.mark.parametrize("mock_response", ["", "Hello"])
@pytest.mark.parametrize("stream", [False, True])
def test_completion_mock_response(mock_response: str, stream: bool) -> None:
with patch("httpx.Client.send", side_effect=AssertionError("Unexpected HTTP request")):
response = litellm.completion(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "hi"}],
mock_response=mock_response,
stream=stream,
)
if stream:
content = "".join(chunk.choices[0].delta.content or "" for chunk in response)
else:
content = response.choices[0].message.content
assert content == mock_response
@pytest.mark.asyncio
@pytest.mark.parametrize("mock_response", ["", "Hello"])
@pytest.mark.parametrize("stream", [False, True])
async def test_acompletion_mock_response(mock_response: str, stream: bool) -> None:
with (
patch(
"httpx.AsyncClient.send",
side_effect=AssertionError("Unexpected HTTP request"),
),
patch("httpx.Client.send", side_effect=AssertionError("Unexpected HTTP request")),
):
response = await litellm.acompletion(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "hi"}],
mock_response=mock_response,
stream=stream,
)
if stream:
content = "".join([chunk.choices[0].delta.content or "" async for chunk in response])
else:
content = response.choices[0].message.content
assert content == mock_response