From 400304c2b785ac4515ab771ed9f480f95ac108c3 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Mon, 9 Feb 2026 14:42:17 -0800 Subject: [PATCH] perf: pre-compute OpenAI client __init__ params at module load MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace per-request inspect.signature() calls with module-level constants. The OpenAI/AzureOpenAI __init__ signatures never change at runtime, so introspecting them on every request was wasted CPU. Line profile: 107µs → 0.7µs per call (154x speedup) --- litellm/llms/openai/common_utils.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/litellm/llms/openai/common_utils.py b/litellm/llms/openai/common_utils.py index ce470f04aca..868d02ee1e7 100644 --- a/litellm/llms/openai/common_utils.py +++ b/litellm/llms/openai/common_utils.py @@ -14,6 +14,8 @@ from openai import AsyncAzureOpenAI, AsyncOpenAI, AzureOpenAI, OpenAI if TYPE_CHECKING: from aiohttp import ClientSession +import inspect + import litellm from litellm.llms.base_llm.chat.transformation import BaseLLMException from litellm.llms.custom_httpx.http_handler import ( @@ -22,6 +24,14 @@ from litellm.llms.custom_httpx.http_handler import ( get_ssl_configuration, ) +def _get_client_init_params(cls: type) -> List[str]: + """Extract __init__ parameter names (excluding 'self') from a class.""" + return [p for p in inspect.signature(cls.__init__).parameters if p != "self"] + + +_OPENAI_INIT_PARAMS: List[str] = _get_client_init_params(OpenAI) +_AZURE_OPENAI_INIT_PARAMS: List[str] = _get_client_init_params(AzureOpenAI) + class OpenAIError(BaseLLMException): def __init__( @@ -183,18 +193,10 @@ class BaseOpenAILLM: client_type: Literal["openai", "azure"] ) -> List[str]: """Returns a list of fields that are used to initialize the OpenAI client""" - import inspect - - from openai import AzureOpenAI, OpenAI - if client_type == "openai": - signature = inspect.signature(OpenAI.__init__) + return _OPENAI_INIT_PARAMS else: - signature = inspect.signature(AzureOpenAI.__init__) - - # Extract parameter names, excluding 'self' - param_names = [param for param in signature.parameters if param != "self"] - return param_names + return _AZURE_OPENAI_INIT_PARAMS @staticmethod def _get_async_http_client( @@ -230,3 +232,5 @@ class BaseOpenAILLM: verify=ssl_config, follow_redirects=True, ) + +