Skip to content

Commit bff232d

Browse files
sararobcopybara-github
authored andcommitted
chore: GenAI SDK client - fix mypy errors in client.py
PiperOrigin-RevId: 832340202
1 parent e9d9c31 commit bff232d

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

vertexai/_genai/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import importlib
1818

19-
from .client import Client # type: ignore[attr-defined]
19+
from .client import Client
2020

2121
_evals = None
2222

vertexai/_genai/client.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import asyncio
1717
import importlib
18-
from typing import Optional, Union, Any
19-
from types import TracebackType
18+
from typing import Optional, Union, TYPE_CHECKING
19+
from types import TracebackType, ModuleType
2020

2121
import google.auth
2222
from google.cloud.aiplatform import version as aip_version
@@ -26,6 +26,14 @@
2626
from google.genai import types
2727
from . import live
2828

29+
if TYPE_CHECKING:
30+
from vertexai._genai import agent_engines as agent_engines_module
31+
from vertexai._genai import datasets as datasets_module
32+
from vertexai._genai import evals as evals_module
33+
from vertexai._genai import prompt_optimizer as prompt_optimizer_module
34+
from vertexai._genai import prompts as prompts_module
35+
from vertexai._genai import live as live_module
36+
2937

3038
_GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules"
3139

@@ -50,52 +58,52 @@ def _add_tracking_headers(headers: dict[str, str]) -> None:
5058
class AsyncClient:
5159
"""Async Gen AI Client for the Vertex SDK."""
5260

53-
def __init__(self, api_client: genai_client.BaseApiClient):
61+
def __init__(self, api_client: genai_client.BaseApiClient): # type: ignore[name-defined]
5462
self._api_client = api_client
5563
self._live = live.AsyncLive(self._api_client)
56-
self._evals = None
57-
self._agent_engines = None
58-
self._prompt_optimizer = None
59-
self._prompts = None
60-
self._datasets = None
64+
self._evals: Optional[ModuleType] = None
65+
self._agent_engines: Optional[ModuleType] = None
66+
self._prompt_optimizer: Optional[ModuleType] = None
67+
self._prompts: Optional[ModuleType] = None
68+
self._datasets: Optional[ModuleType] = None
6169

6270
@property
6371
@_common.experimental_warning(
6472
"The Vertex SDK GenAI live module is experimental, and may change in future "
6573
"versions."
6674
)
67-
def live(self) -> live.AsyncLive:
75+
def live(self) -> "live_module.AsyncLive":
6876
return self._live
6977

7078
@property
71-
def evals(self) -> Any:
79+
def evals(self) -> "evals_module.AsyncEvals":
7280
if self._evals is None:
7381
try:
7482
# We need to lazy load the evals module to avoid ImportError when
7583
# pandas/tqdm are not installed.
76-
self._evals = importlib.import_module(".evals", __package__) # type: ignore[assignment]
84+
self._evals = importlib.import_module(".evals", __package__)
7785
except ImportError as e:
7886
raise ImportError(
7987
"The 'evals' module requires 'pandas' and 'tqdm'. "
8088
"Please install them using pip install "
8189
"google-cloud-aiplatform[evaluation]"
8290
) from e
83-
return self._evals.AsyncEvals(self._api_client) # type: ignore[attr-defined]
91+
return self._evals.AsyncEvals(self._api_client)
8492

8593
@property
8694
@_common.experimental_warning(
8795
"The Vertex SDK GenAI prompt optimizer module is experimental, "
8896
"and may change in future versions."
8997
)
90-
def prompt_optimizer(self):
98+
def prompt_optimizer(self) -> "prompt_optimizer_module.AsyncPromptOptimizer":
9199
if self._prompt_optimizer is None:
92100
self._prompt_optimizer = importlib.import_module(
93101
".prompt_optimizer", __package__
94102
)
95103
return self._prompt_optimizer.AsyncPromptOptimizer(self._api_client)
96104

97105
@property
98-
def agent_engines(self):
106+
def agent_engines(self) -> "agent_engines_module.AsyncAgentEngines":
99107
if self._agent_engines is None:
100108
try:
101109
# We need to lazy load the agent_engines module to handle the
@@ -113,7 +121,7 @@ def agent_engines(self):
113121
return self._agent_engines.AsyncAgentEngines(self._api_client)
114122

115123
@property
116-
def prompts(self):
124+
def prompts(self) -> "prompts_module.AsyncPrompts":
117125
if self._prompts is None:
118126
self._prompts = importlib.import_module(
119127
".prompts",
@@ -126,7 +134,7 @@ def prompts(self):
126134
"The Vertex SDK GenAI async datasets module is experimental, "
127135
"and may change in future versions."
128136
)
129-
def datasets(self):
137+
def datasets(self) -> "datasets_module.AsyncDatasets":
130138
if self._datasets is None:
131139
self._datasets = importlib.import_module(
132140
".datasets",
@@ -224,41 +232,41 @@ def __init__(
224232
http_options=http_options,
225233
)
226234
self._aio = AsyncClient(self._api_client)
227-
self._evals = None
228-
self._prompt_optimizer = None
229-
self._agent_engines = None
230-
self._prompts = None
231-
self._datasets = None
235+
self._evals: Optional[ModuleType] = None
236+
self._prompt_optimizer: Optional[ModuleType] = None
237+
self._agent_engines: Optional[ModuleType] = None
238+
self._prompts: Optional[ModuleType] = None
239+
self._datasets: Optional[ModuleType] = None
232240

233241
@property
234-
def evals(self) -> Any:
242+
def evals(self) -> "evals_module.Evals":
235243
if self._evals is None:
236244
try:
237245
# We need to lazy load the evals module to avoid ImportError when
238246
# pandas/tqdm are not installed.
239-
self._evals = importlib.import_module(".evals", __package__) # type: ignore[assignment]
247+
self._evals = importlib.import_module(".evals", __package__)
240248
except ImportError as e:
241249
raise ImportError(
242250
"The 'evals' module requires additional dependencies. "
243251
"Please install them using pip install "
244252
"google-cloud-aiplatform[evaluation]"
245253
) from e
246-
return self._evals.Evals(self._api_client) # type: ignore[attr-defined]
254+
return self._evals.Evals(self._api_client)
247255

248256
@property
249257
@_common.experimental_warning(
250258
"The Vertex SDK GenAI prompt optimizer module is experimental, and may change in future "
251259
"versions."
252260
)
253-
def prompt_optimizer(self):
261+
def prompt_optimizer(self) -> "prompt_optimizer_module.PromptOptimizer":
254262
if self._prompt_optimizer is None:
255263
self._prompt_optimizer = importlib.import_module(
256264
".prompt_optimizer", __package__
257265
)
258266
return self._prompt_optimizer.PromptOptimizer(self._api_client)
259267

260268
@property
261-
def aio(self):
269+
def aio(self) -> "AsyncClient":
262270
return self._aio
263271

264272
# This is only used for replay tests
@@ -269,27 +277,28 @@ def _get_api_client(
269277
project: Optional[str] = None,
270278
location: Optional[str] = None,
271279
debug_config: Optional[genai_client.DebugConfig] = None,
272-
http_options: Optional[genai_client.HttpOptions] = None,
273-
) -> Optional[genai_client.BaseApiClient]:
280+
http_options: Optional[types.HttpOptions] = None,
281+
) -> Optional[genai_client.BaseApiClient]: # type: ignore[name-defined]
274282
if debug_config and debug_config.client_mode in [
275283
"record",
276284
"replay",
277285
"auto",
278286
]:
279-
return genai_client.ReplayApiClient(
280-
mode=debug_config.client_mode, # type: ignore[arg-type]
281-
replay_id=debug_config.replay_id, # type: ignore[arg-type]
287+
return genai_client.ReplayApiClient( # type: ignore[attr-defined]
288+
mode=debug_config.client_mode,
289+
replay_id=debug_config.replay_id,
282290
replays_directory=debug_config.replays_directory,
283-
vertexai=True, # type: ignore[arg-type]
291+
vertexai=True,
284292
api_key=api_key,
285293
credentials=credentials,
286294
project=project,
287295
location=location,
288296
http_options=http_options,
289297
)
298+
return None
290299

291300
@property
292-
def agent_engines(self):
301+
def agent_engines(self) -> "agent_engines_module.AgentEngines":
293302
if self._agent_engines is None:
294303
try:
295304
# We need to lazy load the agent_engines module to handle the
@@ -307,7 +316,7 @@ def agent_engines(self):
307316
return self._agent_engines.AgentEngines(self._api_client)
308317

309318
@property
310-
def prompts(self):
319+
def prompts(self) -> prompts_module.Prompts:
311320
if self._prompts is None:
312321
# Lazy loading the prompts module
313322
self._prompts = importlib.import_module(
@@ -321,7 +330,7 @@ def prompts(self):
321330
"The Vertex SDK GenAI datasets module is experimental, "
322331
"and may change in future versions."
323332
)
324-
def datasets(self):
333+
def datasets(self) -> "datasets_module.Datasets":
325334
if self._datasets is None:
326335
self._datasets = importlib.import_module(
327336
".datasets",

vertexai/_genai/live.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,32 @@
1818
import importlib
1919
import logging
2020

21+
from typing import Optional, TYPE_CHECKING
22+
from types import ModuleType
2123

2224
from google.genai import _api_module
2325
from google.genai import _common
2426
from google.genai._api_client import BaseApiClient
2527

2628
logger = logging.getLogger("google_genai.live")
2729

30+
if TYPE_CHECKING:
31+
from vertexai._genai import live_agent_engines as live_agent_engines_module
32+
2833

2934
class AsyncLive(_api_module.BaseModule):
3035
"""[Preview] AsyncLive."""
3136

3237
def __init__(self, api_client: BaseApiClient):
3338
super().__init__(api_client)
34-
self._agent_engines = None
39+
self._agent_engines: Optional[ModuleType] = None
3540

3641
@property
3742
@_common.experimental_warning(
3843
"The Vertex SDK GenAI agent engines module is experimental, "
3944
"and may change in future versions."
4045
)
41-
def agent_engines(self):
46+
def agent_engines(self) -> "live_agent_engines_module.AsyncLiveAgentEngines":
4247
if self._agent_engines is None:
4348
try:
4449
# We need to lazy load the live_agent_engines module to handle

0 commit comments

Comments
 (0)