From 8c608b311a23d723462d07304b414e2de6eb0269 Mon Sep 17 00:00:00 2001 From: DennyHo0917 Date: Sun, 6 Sep 2026 19:45:49 +0800 Subject: [PATCH] fix(api_route): add default API base --- litellm/llms/api_route/chat/transformation.py | 4 +-- .../llms/api_route/test_api_route.py | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/litellm/llms/api_route/chat/transformation.py b/litellm/llms/api_route/chat/transformation.py index 0fa715ad7e3..4b9424dacbc 100644 --- a/litellm/llms/api_route/chat/transformation.py +++ b/litellm/llms/api_route/chat/transformation.py @@ -12,8 +12,6 @@ class APIRouteChatConfig(OpenAIGPTConfig): def _get_openai_compatible_provider_info( self, api_base: str | None, api_key: str | None ) -> tuple[str | None, str | None]: - resolved_api_base: Final = api_base or get_secret_str("API_ROUTE_BASE_URL") - if not resolved_api_base: - raise ValueError("API Route requires API_ROUTE_BASE_URL or api_base parameter.") + resolved_api_base: Final = api_base or get_secret_str("API_ROUTE_BASE_URL") or "https://global.api-route.com/v1" resolved_api_key: Final = api_key or get_secret_str("API_ROUTE_API_KEY") return resolved_api_base, resolved_api_key diff --git a/tests/test_litellm/llms/api_route/test_api_route.py b/tests/test_litellm/llms/api_route/test_api_route.py index b3362047ce0..e476fbfd54b 100644 --- a/tests/test_litellm/llms/api_route/test_api_route.py +++ b/tests/test_litellm/llms/api_route/test_api_route.py @@ -1,7 +1,5 @@ import json -import pytest - import litellm from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider from litellm.llms.api_route.chat.transformation import APIRouteChatConfig @@ -39,17 +37,27 @@ def test_api_route_credentials_from_environment(monkeypatch): assert api_base == "https://env.example.com/v1" -def test_api_route_requires_base_url(monkeypatch): +def test_api_route_uses_default_base_url(monkeypatch): monkeypatch.delenv("API_ROUTE_BASE_URL", raising=False) - with pytest.raises( - ValueError, - match=r"API Route requires API_ROUTE_BASE_URL or api_base parameter\.", - ): - APIRouteChatConfig()._get_openai_compatible_provider_info( - api_base=None, - api_key="test-key", - ) + api_base, api_key = APIRouteChatConfig()._get_openai_compatible_provider_info( + api_base=None, + api_key="test-key", + ) + + assert api_base == "https://global.api-route.com/v1" + assert api_key == "test-key" + + +def test_api_route_explicit_api_base_overrides_default(monkeypatch): + monkeypatch.delenv("API_ROUTE_BASE_URL", raising=False) + + api_base, _ = APIRouteChatConfig()._get_openai_compatible_provider_info( + api_base="https://custom.example.com/v1", + api_key="test-key", + ) + + assert api_base == "https://custom.example.com/v1" def test_api_route_chat_completion_request(monkeypatch, respx_mock):