diff --git a/litellm/litellm_core_utils/llm_response_utils/get_api_base.py b/litellm/litellm_core_utils/llm_response_utils/get_api_base.py index c23bbb936b9..253e975178a 100644 --- a/litellm/litellm_core_utils/llm_response_utils/get_api_base.py +++ b/litellm/litellm_core_utils/llm_response_utils/get_api_base.py @@ -28,6 +28,13 @@ def get_api_base( ``` """ + # Fast path: if api_base is already in the dict, return it directly + # without constructing a full LiteLLM_Params Pydantic model. + if isinstance(optional_params, dict): + _api_base = optional_params.get("api_base") + if isinstance(_api_base, str): + return _api_base + try: if isinstance(optional_params, LiteLLM_Params): _optional_params = optional_params diff --git a/tests/test_litellm/litellm_core_utils/test_get_api_base.py b/tests/test_litellm/litellm_core_utils/test_get_api_base.py new file mode 100644 index 00000000000..19cc5b8e28b --- /dev/null +++ b/tests/test_litellm/litellm_core_utils/test_get_api_base.py @@ -0,0 +1,16 @@ +from litellm.litellm_core_utils.llm_response_utils.get_api_base import get_api_base + + +def test_get_api_base_rejects_non_string_api_base(): + """When api_base is in dict but not a string, fast path should be skipped.""" + result = get_api_base(model="gpt-3.5-turbo", optional_params={"api_base": 123}) + assert result is None or isinstance(result, str) + + +def test_get_api_base_fast_path_returns_string(): + """When api_base is a valid string in dict, fast path should return it directly.""" + result = get_api_base( + model="gpt-3.5-turbo", + optional_params={"api_base": "https://my-proxy.example.com"}, + ) + assert result == "https://my-proxy.example.com"