From 0a8955e73a819af43bf8fbeabd3067d62356956d Mon Sep 17 00:00:00 2001 From: Dhruvil Mistry Date: Wed, 7 Oct 2026 19:52:34 +0530 Subject: [PATCH] fix(proxy): make 'litellm --test' fall back to http://{host}:{port} instead of raising The CLI declares --test with flag_value=True, so Click always passes the boolean True into _run_test_chat_completion. The helper's else-branch treated any non-str value as an error and raised 'Invalid test value' before any request was sent, making the advertised 'litellm --test' command unusable. Keep the default api_base of http://{host}:{port} when the flag is a bool and honor an explicit str override when given. The existing unit test asserted the ValueError; flip it to assert the OpenAI client is constructed against the default base URL for test=True and against the explicit URL for a str value. Fixes #44912 --- litellm/proxy/proxy_cli.py | 2 -- tests/unit/proxy/test_proxy_cli.py | 18 +++++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.py index 53a447bc6ec..456564639cc 100644 --- a/litellm/proxy/proxy_cli.py +++ b/litellm/proxy/proxy_cli.py @@ -247,8 +247,6 @@ class ProxyInitializationHelpers: api_base = f"http://{host}:{port}" if isinstance(test, str): api_base = test - else: - raise ValueError("Invalid test value") client: Final = openai.OpenAI(api_key="My API Key", base_url=api_base) response: Final = client.chat.completions.create( diff --git a/tests/unit/proxy/test_proxy_cli.py b/tests/unit/proxy/test_proxy_cli.py index 8fc807cd8a0..491dc3441dd 100644 --- a/tests/unit/proxy/test_proxy_cli.py +++ b/tests/unit/proxy/test_proxy_cli.py @@ -84,23 +84,27 @@ class TestProxyInitializationHelpers: mock_client.chat.completions.create.side_effect = [ mock_response, mock_stream_response, + mock_response, + mock_stream_response, ] # Execute - with pytest.raises(ValueError, match="Invalid test value"): - ProxyInitializationHelpers._run_test_chat_completion( - "localhost", 8000, "gpt-3.5-turbo", True - ) + # The --test flag passes True; the helper must fall back to http://{host}:{port} + ProxyInitializationHelpers._run_test_chat_completion( + "localhost", 8000, "gpt-3.5-turbo", True + ) - # Test with valid string test value + # Test with explicit string test value ProxyInitializationHelpers._run_test_chat_completion( "localhost", 8000, "gpt-3.5-turbo", "http://test-url" ) # Assert - mock_openai.assert_called_once_with( - api_key="My API Key", base_url="http://test-url" + assert mock_openai.call_count == 2 + mock_openai.assert_any_call( + api_key="My API Key", base_url="http://localhost:8000" ) + mock_openai.assert_any_call(api_key="My API Key", base_url="http://test-url") mock_client.chat.completions.create.assert_called() def test_get_default_unvicorn_init_args(self):