1717import importlib
1818import sys
1919
20+ from types import ModuleType
21+
2022from google .cloud .aiplatform import version as aiplatform_version
2123
2224__version__ = aiplatform_version .__version__
2729_genai_types = None
2830
2931
32+ class _LazyTypesLoader (ModuleType ):
33+ """A module that lazily loads the actual types module when an attribute is accessed."""
34+
35+ def __init__ (self , * args , ** kwargs ):
36+ super ().__init__ (* args , ** kwargs )
37+ self ._module = None
38+
39+ def __getattr__ (self , name ):
40+ if self ._module is None :
41+ self ._module = importlib .import_module ("._genai.types" , __package__ )
42+ return getattr (self ._module , name )
43+
44+ sys .modules [__name__ + ".types" ] = _LazyTypesLoader (__name__ + ".types" )
45+
46+
47+
3048def __getattr__ (name ): # type: ignore[no-untyped-def]
3149 # Lazy importing the preview submodule
3250 # See https://peps.python.org/pep-0562/
@@ -45,12 +63,7 @@ def __getattr__(name): # type: ignore[no-untyped-def]
4563 return getattr (_genai_client , name )
4664
4765 if name == "types" :
48- global _genai_types
49- if _genai_types is None :
50- _genai_types = importlib .import_module ("._genai.types" , __name__ )
51- if "vertexai.types" not in sys .modules :
52- sys .modules ["vertexai.types" ] = _genai_types
53- return _genai_types
66+ return sys .modules [__name__ + ".types" ]
5467
5568 raise AttributeError (f"module '{ __name__ } ' has no attribute '{ name } '" )
5669
0 commit comments