From f655f47d84e00a6bfa4dda38c6294021c9b65de6 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Wed, 28 Jan 2026 17:30:41 -0800 Subject: [PATCH 1/3] perf: skip Pydantic model construction in get_api_base when api_base is in dict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add fast path to check optional_params.get("api_base") directly before constructing a full LiteLLM_Params Pydantic model. When api_base is present (the common case via router), return it immediately — avoiding ~29µs of Pydantic validation overhead per request. Profiled: get_api_base 30.5µs/call → 1.2µs/call (-96%) --- .../litellm_core_utils/llm_response_utils/get_api_base.py | 7 +++++++ 1 file changed, 7 insertions(+) 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..c633f421059 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 _api_base is not None: + return _api_base + try: if isinstance(optional_params, LiteLLM_Params): _optional_params = optional_params From 769d1777fca97ece5dc1a3141a4dcff1dfa8e8dd Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Sat, 7 Feb 2026 09:51:51 -0800 Subject: [PATCH 2/3] fix: add isinstance(str) check to get_api_base fast path Ensures non-string api_base values (e.g. int, list) fall through to Pydantic validation instead of being returned directly. --- litellm/litellm_core_utils/llm_response_utils/get_api_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c633f421059..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 @@ -32,7 +32,7 @@ def get_api_base( # without constructing a full LiteLLM_Params Pydantic model. if isinstance(optional_params, dict): _api_base = optional_params.get("api_base") - if _api_base is not None: + if isinstance(_api_base, str): return _api_base try: From d337713976b33d3c9394e4f368195ec966fcf879 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Sat, 7 Feb 2026 11:01:40 -0800 Subject: [PATCH 3/3] test: add tests for isinstance guard on api_base fast path --- .../litellm_core_utils/test_get_api_base.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/test_litellm/litellm_core_utils/test_get_api_base.py 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"