Skip to content

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]

Fix Google GenAI types import to handle missing google.genai module

Henry Wangcb8194c22b42
diff --git a/litellm/types/google_genai/main.py b/litellm/types/google_genai/main.py
index b875495bab0..0a26f266a6e 100644
--- a/litellm/types/google_genai/main.py
+++ b/litellm/types/google_genai/main.py
@@ -1,28 +1,58 @@
# Import types from the Google GenAI SDK
-from typing import TYPE_CHECKING, Any, List, Optional, TypeAlias
+from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeAlias
-# During static type-checking we can rely on the real google-genai types.
-from google.genai import types as _genai_types # type: ignore
from pydantic import BaseModel
from typing_extensions import TypedDict
from litellm.types.llms.openai import BaseLiteLLMOpenAIResponseObject
-ContentListUnion = _genai_types.ContentListUnion
-ContentListUnionDict = _genai_types.ContentListUnionDict
-GenerateContentConfigOrDict = _genai_types.GenerateContentConfigOrDict
-GoogleGenAIGenerateContentResponse = _genai_types.GenerateContentResponse
+# During static type-checking we can rely on the real google-genai types.
+if TYPE_CHECKING:
+ from google.genai import types as _genai_types # type: ignore
+
+ ContentListUnion = _genai_types.ContentListUnion
+ ContentListUnionDict = _genai_types.ContentListUnionDict
+ GenerateContentConfigOrDict = _genai_types.GenerateContentConfigOrDict
+ GoogleGenAIGenerateContentResponse = _genai_types.GenerateContentResponse
+ GenerateContentContentListUnionDict = _genai_types.ContentListUnionDict
+ GenerateContentConfigDict = _genai_types.GenerateContentConfigDict
+ GenerateContentRequestParametersDict = _genai_types._GenerateContentParametersDict
+ ToolConfigDict = _genai_types.ToolConfigDict
+
+ class GenerateContentRequestDict(GenerateContentRequestParametersDict): # type: ignore[misc]
+ generationConfig: Optional[Any]
+ tools: Optional[ToolConfigDict] # type: ignore[assignment]
+
+ class GenerateContentResponse(GoogleGenAIGenerateContentResponse, BaseLiteLLMOpenAIResponseObject): # type: ignore[misc]
+ _hidden_params: dict = {}
+ pass
+else:
+ # Fallback types when google.genai is not available
+ ContentListUnion = Any
+ ContentListUnionDict = Dict[str, Any]
+ GenerateContentConfigOrDict = Dict[str, Any]
+ GoogleGenAIGenerateContentResponse = Dict[str, Any]
+ GenerateContentContentListUnionDict = Dict[str, Any]
+
+ # Create a proper fallback class that can be instantiated
+ class GenerateContentConfigDict(dict): # type: ignore[misc]
+ def __init__(self, **kwargs): # type: ignore
+ super().__init__(**kwargs)
-GenerateContentContentListUnionDict = _genai_types.ContentListUnionDict
-GenerateContentConfigDict = _genai_types.GenerateContentConfigDict
-GenerateContentRequestParametersDict = _genai_types._GenerateContentParametersDict
-ToolConfigDict = _genai_types.ToolConfigDict
+ class GenerateContentRequestParametersDict(dict): # type: ignore[misc]
+ def __init__(self, **kwargs): # type: ignore
+ super().__init__(**kwargs)
-class GenerateContentRequestDict(GenerateContentRequestParametersDict): # type: ignore[misc]
- generationConfig: Optional[Any]
- tools: Optional[ToolConfigDict] # type: ignore[assignment]
+ ToolConfigDict = Dict[str, Any]
+ class GenerateContentRequestDict(GenerateContentRequestParametersDict): # type: ignore[misc]
+ def __init__(self, **kwargs): # type: ignore
+ # Extract specific fields
+ self.generationConfig = kwargs.get('generationConfig')
+ self.tools = kwargs.get('tools')
+ super().__init__(**kwargs)
-class GenerateContentResponse(GoogleGenAIGenerateContentResponse, BaseLiteLLMOpenAIResponseObject): # type: ignore[misc]
- _hidden_params: dict = {}
- pass
\ No newline at end of file
+ class GenerateContentResponse(BaseLiteLLMOpenAIResponseObject): # type: ignore[misc]
+ def __init__(self, **kwargs): # type: ignore
+ super().__init__(**kwargs)
+ self._hidden_params = kwargs.get('_hidden_params', {})
\ No newline at end of file