Skip to content

Commit 552dbfa

Browse files
sararobcopybara-github
authored andcommitted
fix: Remove lazy loading of vertexai.types module to enable from vertexai.types import TypeName
PiperOrigin-RevId: 834811652
1 parent 186e6d8 commit 552dbfa

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

vertexai/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ def __getattr__(name): # type: ignore[no-untyped-def]
4444
_genai_client = importlib.import_module("._genai.client", __name__)
4545
return getattr(_genai_client, name)
4646

47-
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
54-
5547
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
5648

5749

vertexai/_genai/types.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
"""A proxy module for lazy-loading the vertexai.types module."""
16+
17+
import importlib
18+
import sys
19+
20+
_LAZY_LOADED_TYPES_MODULE = "google.cloud.aiplatform.vertexai._genai.types"
21+
22+
_module = None
23+
24+
25+
# PEP 562: Lazy loading of submodules
26+
def __getattr__(name: str):
27+
"""Lazily load attributes from the `types` module."""
28+
global _module
29+
if _module is None:
30+
_module = importlib.import_module(_LAZY_LOADED_TYPES_MODULE)
31+
# After the first access, we replace this proxy module with the real module
32+
# in sys.modules. This makes subsequent imports faster.
33+
sys.modules[__name__] = _module
34+
35+
return getattr(_module, name)

0 commit comments

Comments
 (0)