Skip to content

Commit 3ce65ff

Browse files
google-genai-botcopybara-github
authored andcommitted
perf: lazily import the interactions API to speed up import google.genai
The top-level package eagerly imported the interactions submodule, and client.py eagerly imported the _gaos backend, so every `import google.genai` paid for hundreds of interactions-API modules even when the caller only uses generate_content. Defer both: make `interactions` a lazy module attribute via __getattr__, and move the _gaos imports in client.py under TYPE_CHECKING plus into the Client/AsyncClient properties that construct them. `from __future__ import annotations` keeps the property return annotations valid without the eager import. The _gaos modules now load only when .interactions/.agents/ .webhooks (or the nextgen client) are first accessed. PiperOrigin-RevId: 945816846
1 parent 0ab9525 commit 3ce65ff

2 files changed

Lines changed: 44 additions & 19 deletions

File tree

google/genai/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@
1515

1616
"""Google Gen AI SDK"""
1717

18-
from . import interactions
18+
import importlib
19+
from typing import Any
20+
1921
from . import types
2022
from . import version
2123
from .client import Client
2224

2325

2426
__version__ = version.__version__
2527

26-
__all__ = ['Client']
28+
__all__ = ['Client', 'interactions', 'types']
29+
30+
31+
def __getattr__(name: str) -> Any:
32+
if name == 'interactions':
33+
module = importlib.import_module('.interactions', __name__)
34+
globals()[name] = module
35+
return module
36+
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')

google/genai/client.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
# limitations under the License.
1414
#
1515

16+
from __future__ import annotations
17+
1618
import asyncio
1719
import os
18-
from typing import Any, Optional, Union
20+
from typing import Any, Optional, TYPE_CHECKING, Union
21+
import warnings
1922

2023
import google.auth
2124
import pydantic
2225

26+
from . import _common
2327
from ._api_client import BaseApiClient
2428
from ._base_url import get_base_url
2529
from ._replay_api_client import ReplayApiClient
@@ -35,22 +39,17 @@
3539
from .tunings import AsyncTunings, Tunings
3640
from .types import HttpOptions, HttpOptionsDict, HttpRetryOptions
3741

38-
import warnings
39-
40-
from . import _common
41-
42-
from ._gaos.google_genai import (
43-
AsyncGeminiNextGenAgents,
44-
AsyncGeminiNextGenInteractions,
45-
AsyncGeminiNextGenWebhooks,
46-
GeminiNextGenAgents,
47-
GeminiNextGenInteractions,
48-
GeminiNextGenWebhooks,
49-
build_google_genai_async_client,
50-
build_google_genai_client,
51-
)
52-
from ._gaos.sdk import AsyncGenAI as AsyncGeminiNextGenAPI
53-
from ._gaos.sdk import GenAI as GeminiNextGenAPI
42+
if TYPE_CHECKING:
43+
from ._gaos.google_genai import (
44+
AsyncGeminiNextGenAgents,
45+
AsyncGeminiNextGenInteractions,
46+
AsyncGeminiNextGenWebhooks,
47+
GeminiNextGenAgents,
48+
GeminiNextGenInteractions,
49+
GeminiNextGenWebhooks,
50+
)
51+
from ._gaos.sdk import AsyncGenAI as AsyncGeminiNextGenAPI
52+
from ._gaos.sdk import GenAI as GeminiNextGenAPI
5453

5554
_agent_experimental_warned = False
5655

@@ -77,6 +76,8 @@ def __init__(self, api_client: BaseApiClient):
7776

7877
@property
7978
def _nextgen_client(self) -> AsyncGeminiNextGenAPI:
79+
from ._gaos.google_genai import build_google_genai_async_client
80+
8081
if self._nextgen_client_instance is None:
8182
self._nextgen_client_instance = build_google_genai_async_client(
8283
self._api_client
@@ -85,18 +86,24 @@ def _nextgen_client(self) -> AsyncGeminiNextGenAPI:
8586

8687
@property
8788
def interactions(self) -> AsyncGeminiNextGenInteractions:
89+
from ._gaos.google_genai import AsyncGeminiNextGenInteractions
90+
8891
if self._interactions is None:
8992
self._interactions = AsyncGeminiNextGenInteractions(self._api_client)
9093
return self._interactions
9194

9295
@property
9396
def webhooks(self) -> AsyncGeminiNextGenWebhooks:
97+
from ._gaos.google_genai import AsyncGeminiNextGenWebhooks
98+
9499
if self._webhooks is None:
95100
self._webhooks = AsyncGeminiNextGenWebhooks(self._api_client)
96101
return self._webhooks
97102

98103
@property
99104
def agents(self) -> AsyncGeminiNextGenAgents:
105+
from ._gaos.google_genai import AsyncGeminiNextGenAgents
106+
100107
global _agent_experimental_warned
101108
if not _agent_experimental_warned:
102109
_agent_experimental_warned = True
@@ -400,6 +407,8 @@ def _get_api_client(
400407

401408
@property
402409
def _nextgen_client(self) -> GeminiNextGenAPI:
410+
from ._gaos.google_genai import build_google_genai_client
411+
403412
if self._nextgen_client_instance is None:
404413
self._nextgen_client_instance = build_google_genai_client(
405414
self._api_client
@@ -408,18 +417,24 @@ def _nextgen_client(self) -> GeminiNextGenAPI:
408417

409418
@property
410419
def interactions(self) -> GeminiNextGenInteractions:
420+
from ._gaos.google_genai import GeminiNextGenInteractions
421+
411422
if self._interactions is None:
412423
self._interactions = GeminiNextGenInteractions(self._api_client)
413424
return self._interactions
414425

415426
@property
416427
def webhooks(self) -> GeminiNextGenWebhooks:
428+
from ._gaos.google_genai import GeminiNextGenWebhooks
429+
417430
if self._webhooks is None:
418431
self._webhooks = GeminiNextGenWebhooks(self._api_client)
419432
return self._webhooks
420433

421434
@property
422435
def agents(self) -> GeminiNextGenAgents:
436+
from ._gaos.google_genai import GeminiNextGenAgents
437+
423438
global _agent_experimental_warned
424439
if not _agent_experimental_warned:
425440
_agent_experimental_warned = True

0 commit comments

Comments
 (0)