Skip to content

Commit 11c3ff0

Browse files
sararobcopybara-github
authored andcommitted
chore: GenAI SDK client - fix types import logic to correctly lazy load _evals_metric_loaders
PiperOrigin-RevId: 824518772
1 parent 91c8df5 commit 11c3ff0

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

tests/unit/architecture/test_vertexai_import.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ def test_vertexai_import():
100100
assert "pandas" not in modules_after_genai_client_import
101101
assert "pydantic" in modules_after_genai_client_import
102102

103+
# The types module should not import _evals_metric_loaders until
104+
# PrebuiltMetric or RubricMetric are accessed.
105+
from vertexai._genai import types # noqa: F401
106+
107+
assert (
108+
"google.cloud.aiplatform.vertexai._genai._evals_metric_loaders"
109+
not in sys.modules
110+
)
111+
103112
# Tests the evals module is lazy loaded.
104113
from vertexai._genai import evals as _ # noqa: F401,F811
105114

vertexai/_genai/types/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# Code generated by the Google Gen AI SDK generator DO NOT EDIT.
1717
# flake8: noqa: F401
1818

19+
import importlib
20+
import typing
21+
1922
from . import agent_engines
2023
from . import evals
2124
from .common import _AppendAgentEngineSessionEventRequestParameters
@@ -756,7 +759,6 @@
756759
from .common import RubricGroup
757760
from .common import RubricGroupDict
758761
from .common import RubricGroupOrDict
759-
from .common import RubricMetric
760762
from .common import RubricOrDict
761763
from .common import RubricVerdict
762764
from .common import RubricVerdictDict
@@ -1813,7 +1815,6 @@
18131815
"PromptDataDict",
18141816
"PromptDataOrDict",
18151817
"LLMMetric",
1816-
"RubricMetric",
18171818
"MetricPromptBuilder",
18181819
"_CreateEvaluationItemParameters",
18191820
"_CreateEvaluationRunParameters",
@@ -1878,4 +1879,15 @@
18781879
"_UpdateDatasetParameters",
18791880
"evals",
18801881
"agent_engines",
1882+
"PrebuiltMetric",
1883+
"RubricMetric",
18811884
]
1885+
1886+
1887+
def __getattr__(name: str) -> typing.Any:
1888+
if name == "PrebuiltMetric" or name == "RubricMetric":
1889+
module = importlib.import_module(".._evals_metric_loaders", __package__)
1890+
prebuilt_metric_obj = getattr(module, name)
1891+
globals()[name] = prebuilt_metric_obj
1892+
return prebuilt_metric_obj
1893+
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")

vertexai/_genai/types/common.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# Code generated by the Google Gen AI SDK generator DO NOT EDIT.
1717

1818
import datetime
19-
import importlib
2019
import json
2120
import logging
2221
import os
@@ -47,17 +46,6 @@
4746
from typing_extensions import TypedDict
4847
from . import evals as evals_types
4948

50-
__all__ = ["PrebuiltMetric", "RubricMetric"] # noqa: F822
51-
52-
53-
def __getattr__(name: str) -> typing.Any:
54-
if name == "PrebuiltMetric" or name == "RubricMetric":
55-
module = importlib.import_module(".._evals_metric_loaders", __package__)
56-
prebuilt_metric_obj = getattr(module, name)
57-
globals()[name] = prebuilt_metric_obj
58-
return prebuilt_metric_obj
59-
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
60-
6149

6250
def _camel_to_snake(camel_case_string: str) -> str:
6351
snake_case_string = re.sub(r"(?<!^)([A-Z])", r"_\1", camel_case_string)

0 commit comments

Comments
 (0)