Skip to content

Commit 8d5c777

Browse files
sararobcopybara-github
authored andcommitted
chore: Gen AI SDK client - resolve some agent engine mypy errors
PiperOrigin-RevId: 833396344
1 parent 2f59484 commit 8d5c777

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

vertexai/_genai/_agent_engines_utils.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,23 @@
6464
_BUILTIN_MODULE_NAMES: Sequence[str] = [] # type: ignore[no-redef]
6565

6666
try:
67-
_PACKAGE_DISTRIBUTIONS: Mapping[
68-
str, Sequence[str]
69-
] = importlib_metadata.packages_distributions() # type: ignore[attr-defined]
67+
_PACKAGE_DISTRIBUTIONS: Mapping[str, Sequence[str]] = (
68+
importlib_metadata.packages_distributions()
69+
)
7070
except AttributeError:
7171
_PACKAGE_DISTRIBUTIONS: Mapping[str, Sequence[str]] = {} # type: ignore[no-redef]
7272

7373
try:
7474
# sys.stdlib_module_names is available from Python 3.10 onwards.
75-
_STDLIB_MODULE_NAMES: frozenset[str] = sys.stdlib_module_names # type: ignore[attr-defined]
75+
_STDLIB_MODULE_NAMES: frozenset[str] = sys.stdlib_module_names
7676
except AttributeError:
7777
_STDLIB_MODULE_NAMES: frozenset[str] = frozenset() # type: ignore[no-redef]
7878

7979

8080
try:
81-
from google.cloud import storage # type: ignore[attr-defined]
81+
from google.cloud import storage
8282

83-
_StorageBucket: type[Any] = storage.Bucket
83+
_StorageBucket: type[Any] = storage.Bucket # type: ignore[attr-defined]
8484
except (ImportError, AttributeError):
8585
_StorageBucket: type[Any] = Any # type: ignore[no-redef]
8686

@@ -90,7 +90,7 @@
9090

9191
_SpecifierSet: type[Any] = packaging.specifiers.SpecifierSet
9292
except (ImportError, AttributeError):
93-
_SpecifierSet: type[Any] = Any
93+
_SpecifierSet: type[Any] = Any # type: ignore[no-redef]
9494

9595

9696
try:
@@ -125,7 +125,7 @@
125125
_ASYNC_API_MODE = "async"
126126
_ASYNC_STREAM_API_MODE = "async_stream"
127127
_BIDI_STREAM_API_MODE = "bidi_stream"
128-
_BASE_MODULES = set(_BUILTIN_MODULE_NAMES + tuple(_STDLIB_MODULE_NAMES))
128+
_BASE_MODULES = set(_BUILTIN_MODULE_NAMES).union(_STDLIB_MODULE_NAMES)
129129
_BLOB_FILENAME = "agent_engine.pkl"
130130
_DEFAULT_AGENT_FRAMEWORK = "custom"
131131
_SUPPORTED_AGENT_FRAMEWORKS = frozenset(
@@ -237,7 +237,9 @@ class BidiStreamQueryable(Protocol):
237237
"""Protocol for Agent Engines that can stream requests and responses."""
238238

239239
@abc.abstractmethod
240-
async def bidi_stream_query(self, input_queue: asyncio.Queue) -> AsyncIterator[Any]:
240+
async def bidi_stream_query(
241+
self, input_queue: asyncio.Queue[Any]
242+
) -> AsyncIterator[Any]:
241243
"""Stream requests and responses to serve the user queries."""
242244

243245

@@ -255,7 +257,7 @@ class OperationRegistrable(Protocol):
255257
"""Protocol for agents that have registered operations."""
256258

257259
@abc.abstractmethod
258-
def register_operations(self, **kwargs) -> Dict[str, Sequence[str]]:
260+
def register_operations(self, **kwargs) -> Dict[str, Sequence[str]]: # type: ignore[no-untyped-def]
259261
"""Register the user provided operations (modes and methods)."""
260262

261263

@@ -264,7 +266,7 @@ def register_operations(self, **kwargs) -> Dict[str, Sequence[str]]:
264266

265267
ADKAgent: type[Any] = BaseAgent
266268
except (ImportError, AttributeError):
267-
ADKAgent: type[Any] = Any
269+
ADKAgent: type[Any] = Any # type: ignore[no-redef]
268270

269271
_AgentEngineInterface = Union[
270272
ADKAgent,
@@ -399,13 +401,15 @@ class _RequirementsValidationResult(TypedDict):
399401

400402

401403
class GetOperationFunction(Protocol):
402-
def __call__(self, *, operation_name: str, **kwargs) -> AgentEngineOperationUnion:
404+
def __call__(
405+
self, *, operation_name: str, **kwargs: Any
406+
) -> AgentEngineOperationUnion:
403407
pass
404408

405409

406410
class GetAsyncOperationFunction(Protocol):
407411
async def __call__(
408-
self, *, operation_name: str, **kwargs
412+
self, *, operation_name: str, **kwargs: Any
409413
) -> Awaitable[AgentEngineOperationUnion]:
410414
pass
411415

@@ -727,7 +731,7 @@ def _get_agent_framework(
727731
*,
728732
agent_framework: Optional[str],
729733
agent: _AgentEngineInterface,
730-
) -> str:
734+
) -> Union[str, Any]:
731735
"""Gets the agent framework to use.
732736
733737
The agent framework is determined in the following order of priority:
@@ -820,13 +824,13 @@ def _import_cloudpickle_or_raise() -> types.ModuleType:
820824
def _import_cloud_storage_or_raise() -> types.ModuleType:
821825
"""Tries to import the Cloud Storage module."""
822826
try:
823-
from google.cloud import storage # type: ignore[attr-defined]
827+
from google.cloud import storage
824828
except ImportError as e:
825829
raise ImportError(
826830
"Cloud Storage is not installed. Please call "
827831
"'pip install google-cloud-aiplatform[agent_engines]'."
828832
) from e
829-
return storage # type: ignore[no-any-return]
833+
return storage
830834

831835

832836
def _import_packaging_requirements_or_raise() -> types.ModuleType:
@@ -838,7 +842,7 @@ def _import_packaging_requirements_or_raise() -> types.ModuleType:
838842
"packaging.requirements is not installed. Please call "
839843
"'pip install google-cloud-aiplatform[agent_engines]'."
840844
) from e
841-
return requirements # type: ignore[no-any-return]
845+
return requirements
842846

843847

844848
def _import_packaging_version_or_raise() -> types.ModuleType:
@@ -850,7 +854,7 @@ def _import_packaging_version_or_raise() -> types.ModuleType:
850854
"packaging.version is not installed. Please call "
851855
"'pip install google-cloud-aiplatform[agent_engines]'."
852856
) from e
853-
return version # type: ignore[no-any-return]
857+
return version
854858

855859

856860
def _import_pydantic_or_raise() -> types.ModuleType:
@@ -866,7 +870,7 @@ def _import_pydantic_or_raise() -> types.ModuleType:
866870
"pydantic is not installed. Please call "
867871
"'pip install google-cloud-aiplatform[agent_engines]'."
868872
) from e
869-
return pydantic # type: ignore[no-any-return]
873+
return pydantic
870874

871875

872876
def _parse_constraints(
@@ -1022,7 +1026,7 @@ def _register_api_methods_or_raise(
10221026
}
10231027
if isinstance(wrap_operation_fn, dict) and api_mode in wrap_operation_fn:
10241028
# Override the default function with user-specified function if it exists.
1025-
_wrap_operation = wrap_operation_fn[api_mode] # type: ignore[assignment]
1029+
_wrap_operation = wrap_operation_fn[api_mode]
10261030
elif api_mode in _wrap_operation_map:
10271031
_wrap_operation = _wrap_operation_map[api_mode] # type: ignore[assignment]
10281032
else:
@@ -1531,7 +1535,7 @@ def _method(self, **kwargs) -> Any: # type: ignore[no-untyped-def]
15311535

15321536
class GetOperationFunction(Protocol):
15331537
def __call__( # noqa: E704
1534-
self, *, operation_name: str, **kwargs
1538+
self, *, operation_name: str, **kwargs: Any
15351539
) -> AgentEngineOperationUnion: ...
15361540

15371541

@@ -1588,8 +1592,8 @@ def _wrap_async_query_operation(
15881592
"""
15891593

15901594
async def _method(
1591-
self: genai_types.AgentEngine, **kwargs
1592-
) -> Coroutine[Any, Any, Any]: # type: ignore[no-untyped-def]
1595+
self: genai_types.AgentEngine, **kwargs: Any
1596+
) -> Union[Coroutine[Any, Any, Any], Any]:
15931597
if not self.api_async_client:
15941598
raise ValueError("api_async_client is not initialized.")
15951599
if not self.api_resource:
@@ -1681,7 +1685,7 @@ async def _method(self: genai_types.AgentEngine, **kwargs) -> AsyncIterator[Any]
16811685
return _method
16821686

16831687

1684-
def _wrap_a2a_operation(method_name: str, agent_card: str) -> Callable[..., list]:
1688+
def _wrap_a2a_operation(method_name: str, agent_card: str) -> Callable[..., list[Any]]:
16851689
"""Wraps an Agent Engine method, creating a callable for A2A API.
16861690
16871691
Args:
@@ -1727,7 +1731,7 @@ def _wrap_a2a_operation(method_name: str, agent_card: str) -> Callable[..., list
17271731
the A2A API.
17281732
"""
17291733

1730-
async def _method(self, **kwargs) -> Any:
1734+
async def _method(self, **kwargs) -> Any: # type: ignore[no-untyped-def]
17311735
"""Wraps an Agent Engine method, creating a callable for A2A API."""
17321736
if not self.api_client:
17331737
raise ValueError("api_client is not initialized.")

vertexai/_genai/agent_engines.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def _update(
703703
_sessions = None
704704

705705
@property
706-
def memories(self):
706+
def memories(self) -> Any:
707707
if self._memories is None:
708708
try:
709709
# We need to lazy load the memories module to handle the
@@ -722,7 +722,7 @@ def memories(self):
722722
"The Vertex SDK GenAI agent_engines.sandboxes module is experimental, "
723723
"and may change in future versions."
724724
)
725-
def sandboxes(self):
725+
def sandboxes(self) -> Any:
726726
if self._sandboxes is None:
727727
try:
728728
# We need to lazy load the sandboxes module to handle the
@@ -737,7 +737,7 @@ def sandboxes(self):
737737
return self._sandboxes.Sandboxes(self._api_client)
738738

739739
@property
740-
def sessions(self):
740+
def sessions(self) -> Any:
741741
if self._sessions is None:
742742
try:
743743
# We need to lazy load the sessions module to handle the
@@ -1039,7 +1039,7 @@ def _create_config(
10391039
sys_version = python_version
10401040
else:
10411041
sys_version = f"{sys.version_info.major}.{sys.version_info.minor}"
1042-
agent_engine_spec = None
1042+
agent_engine_spec: Any = None
10431043
if agent is not None:
10441044
if source_packages is not None:
10451045
raise ValueError(

0 commit comments

Comments
 (0)