The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Lazy load ImageEditRequestUtils in images/main.py to avoid heavy import (#18074)
Alexsander Hamir9b440caac2d3
diff --git a/litellm/images/main.py b/litellm/images/main.pyindex ca2d4e0b911..7ab496db0b7 100644--- a/litellm/images/main.py+++ b/litellm/images/main.py@@ -1,7 +1,11 @@import asyncioimport contextvars+import importlibfrom functools import partial-from typing import Any, Coroutine, Dict, List, Literal, Optional, Union, cast, overload+from typing import TYPE_CHECKING, Any, Coroutine, Dict, List, Literal, Optional, Union, cast, overload++if TYPE_CHECKING:+ from litellm.images.utils import ImageEditRequestUtilsimport httpx@@ -50,7 +54,20 @@ from litellm.utils import (get_optional_params_image_gen,)-from .utils import ImageEditRequestUtils+# Cache for ImageEditRequestUtils to avoid repeated __getattr__ calls+_ImageEditRequestUtils_cache: Optional["ImageEditRequestUtils"] = None+++def _get_ImageEditRequestUtils() -> "ImageEditRequestUtils":+ """Get ImageEditRequestUtils, loading it lazily if needed."""+ global _ImageEditRequestUtils_cache+ if _ImageEditRequestUtils_cache is None:+ # Access via module to trigger __getattr__ if not cached+ module = importlib.import_module(__name__)+ _ImageEditRequestUtils_cache = module.ImageEditRequestUtils+ assert _ImageEditRequestUtils_cache is not None # Type narrowing for type checker+ return _ImageEditRequestUtils_cache+##### Image Generation #######################@@ -769,12 +786,12 @@ def image_edit(local_vars.update(kwargs)# Get ImageEditOptionalRequestParams with only valid parametersimage_edit_optional_params: ImageEditOptionalRequestParams = (- ImageEditRequestUtils.get_requested_image_edit_optional_param(local_vars)+ _get_ImageEditRequestUtils().get_requested_image_edit_optional_param(local_vars))# Get optional parameters for the responses APIimage_edit_request_params: Dict = (- ImageEditRequestUtils.get_optional_params_image_edit(+ _get_ImageEditRequestUtils().get_optional_params_image_edit(model=model,image_edit_provider_config=image_edit_provider_config,image_edit_optional_params=image_edit_optional_params,@@ -898,3 +915,15 @@ async def aimage_edit(completion_kwargs=local_vars,extra_kwargs=kwargs,)+++def __getattr__(name: str) -> Any:+ """Lazy import handler for images.main module"""+ if name == "ImageEditRequestUtils":+ # Lazy load ImageEditRequestUtils to avoid heavy import from images.utils at module load time+ from .utils import ImageEditRequestUtils as _ImageEditRequestUtils+ # Cache it in the module's __dict__ for subsequent accesses+ module = importlib.import_module(__name__)+ module.__dict__["ImageEditRequestUtils"] = _ImageEditRequestUtils+ return _ImageEditRequestUtils+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")