From f655f47d84e00a6bfa4dda38c6294021c9b65de6 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Wed, 28 Jan 2026 17:30:41 -0800 Subject: [PATCH] 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