Skip to content

Commit b1d0b7c

Browse files
yeesiancopybara-github
authored andcommitted
fix: GenAI SDK client - switch to nested modules for agent engines
PiperOrigin-RevId: 797839264
1 parent a330ecb commit b1d0b7c

File tree

7 files changed

+7750
-7197
lines changed

7 files changed

+7750
-7197
lines changed

tests/unit/vertexai/genai/test_agent_engines.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -926,16 +926,17 @@ def test_await_operation(self):
926926
}
927927
),
928928
)
929-
agent_engine = self.client.agent_engines._await_operation(
929+
operation = _agent_engines_utils._await_operation(
930930
operation_name=_TEST_AGENT_ENGINE_OPERATION_NAME,
931+
get_operation_fn=self.client.agent_engines._get_agent_operation,
931932
)
932933
request_mock.assert_called_with(
933934
"get",
934935
_TEST_AGENT_ENGINE_OPERATION_NAME,
935936
{"_url": {"operationName": _TEST_AGENT_ENGINE_OPERATION_NAME}},
936937
None,
937938
)
938-
assert isinstance(agent_engine, _genai_types.AgentEngineOperation)
939+
assert isinstance(operation, _genai_types.AgentEngineOperation)
939940

940941
def test_register_api_methods(self):
941942
agent = self.client.agent_engines._register_api_methods(
@@ -1145,7 +1146,7 @@ def test_list_agent_engine(self):
11451146

11461147
@pytest.mark.usefixtures("caplog")
11471148
@mock.patch.object(_agent_engines_utils, "_prepare")
1148-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1149+
@mock.patch.object(_agent_engines_utils, "_await_operation")
11491150
def test_create_agent_engine(self, mock_await_operation, mock_prepare, caplog):
11501151
mock_await_operation.return_value = _genai_types.AgentEngineOperation(
11511152
response=_genai_types.ReasoningEngine(
@@ -1170,10 +1171,6 @@ def test_create_agent_engine(self, mock_await_operation, mock_prepare, caplog):
11701171
env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT,
11711172
),
11721173
)
1173-
mock_await_operation.assert_called_once_with(
1174-
operation_name=None,
1175-
poll_interval_seconds=10,
1176-
)
11771174
request_mock.assert_called_with(
11781175
"post",
11791176
"reasoningEngines",
@@ -1199,7 +1196,7 @@ def test_create_agent_engine(self, mock_await_operation, mock_prepare, caplog):
11991196
)
12001197

12011198
@mock.patch.object(agent_engines.AgentEngines, "_create_config")
1202-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1199+
@mock.patch.object(_agent_engines_utils, "_await_operation")
12031200
def test_create_agent_engine_lightweight(
12041201
self,
12051202
mock_await_operation,
@@ -1225,10 +1222,6 @@ def test_create_agent_engine_lightweight(
12251222
description=_TEST_AGENT_ENGINE_DESCRIPTION,
12261223
)
12271224
)
1228-
mock_await_operation.assert_called_once_with(
1229-
operation_name=None,
1230-
poll_interval_seconds=1,
1231-
)
12321225
request_mock.assert_called_with(
12331226
"post",
12341227
"reasoningEngines",
@@ -1240,7 +1233,7 @@ def test_create_agent_engine_lightweight(
12401233
)
12411234

12421235
@mock.patch.object(agent_engines.AgentEngines, "_create_config")
1243-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1236+
@mock.patch.object(_agent_engines_utils, "_await_operation")
12441237
def test_create_agent_engine_with_env_vars_dict(
12451238
self,
12461239
mock_await_operation,
@@ -1317,7 +1310,7 @@ def test_create_agent_engine_with_env_vars_dict(
13171310

13181311
@pytest.mark.usefixtures("caplog")
13191312
@mock.patch.object(_agent_engines_utils, "_prepare")
1320-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1313+
@mock.patch.object(_agent_engines_utils, "_await_operation")
13211314
def test_update_agent_engine_requirements(
13221315
self, mock_await_operation, mock_prepare, caplog
13231316
):
@@ -1374,7 +1367,7 @@ def test_update_agent_engine_requirements(
13741367
)
13751368

13761369
@mock.patch.object(_agent_engines_utils, "_prepare")
1377-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1370+
@mock.patch.object(_agent_engines_utils, "_await_operation")
13781371
def test_update_agent_engine_extra_packages(
13791372
self, mock_await_operation, mock_prepare
13801373
):
@@ -1428,7 +1421,7 @@ def test_update_agent_engine_extra_packages(
14281421
)
14291422

14301423
@mock.patch.object(_agent_engines_utils, "_prepare")
1431-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1424+
@mock.patch.object(_agent_engines_utils, "_await_operation")
14321425
def test_update_agent_engine_env_vars(
14331426
self, mock_await_operation, mock_prepare, caplog
14341427
):
@@ -1484,7 +1477,7 @@ def test_update_agent_engine_env_vars(
14841477
None,
14851478
)
14861479

1487-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1480+
@mock.patch.object(_agent_engines_utils, "_await_operation")
14881481
def test_update_agent_engine_display_name(self, mock_await_operation):
14891482
mock_await_operation.return_value = _genai_types.AgentEngineOperation(
14901483
response=_genai_types.ReasoningEngine(
@@ -1513,7 +1506,7 @@ def test_update_agent_engine_display_name(self, mock_await_operation):
15131506
None,
15141507
)
15151508

1516-
@mock.patch.object(agent_engines.AgentEngines, "_await_operation")
1509+
@mock.patch.object(_agent_engines_utils, "_await_operation")
15171510
def test_update_agent_engine_description(self, mock_await_operation):
15181511
mock_await_operation.return_value = _genai_types.AgentEngineOperation(
15191512
response=_genai_types.ReasoningEngine(

vertexai/_genai/_agent_engines_utils.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import os
2424
import sys
2525
import tarfile
26+
import time
2627
import types
2728
import typing
2829
from typing import (
@@ -350,6 +351,46 @@ class _RequirementsValidationResult(TypedDict):
350351
actions: _RequirementsValidationActions
351352

352353

354+
AgentEngineOperationUnion = Union[
355+
genai_types.AgentEngineOperation,
356+
genai_types.AgentEngineMemoryOperation,
357+
genai_types.AgentEngineGenerateMemoriesOperation,
358+
]
359+
360+
361+
class GetOperationFunction(Protocol):
362+
def __call__(self, *, operation_name: str, **kwargs) -> AgentEngineOperationUnion:
363+
pass
364+
365+
366+
def _await_operation(
367+
*,
368+
operation_name: str,
369+
get_operation_fn: GetOperationFunction,
370+
poll_interval_seconds: int = 10,
371+
) -> Any:
372+
"""Waits for the operation for creating an agent engine to complete.
373+
374+
Args:
375+
operation_name (str):
376+
Required. The name of the operation for creating the Agent Engine.
377+
poll_interval_seconds (int):
378+
The number of seconds to wait between each poll.
379+
get_operation_fn (Callable[[str], Any]):
380+
Optional. The function to use for getting the operation. If not
381+
provided, `self._get_agent_operation` will be used.
382+
383+
Returns:
384+
The operation that has completed (i.e. `operation.done==True`).
385+
"""
386+
operation = get_operation_fn(operation_name=operation_name)
387+
while not operation.done:
388+
time.sleep(poll_interval_seconds)
389+
operation = get_operation_fn(operation_name=operation.name)
390+
391+
return operation
392+
393+
353394
def _compare_requirements(
354395
*,
355396
requirements: Mapping[str, str],
@@ -1286,18 +1327,6 @@ def _method(self, **kwargs) -> Any: # type: ignore[no-untyped-def]
12861327
return _method
12871328

12881329

1289-
AgentEngineOperationUnion = Union[
1290-
genai_types.AgentEngineOperation,
1291-
genai_types.AgentEngineMemoryOperation,
1292-
genai_types.AgentEngineGenerateMemoriesOperation,
1293-
]
1294-
1295-
1296-
class GetOperationFunction(Protocol):
1297-
def __call__(self, *, operation_name: str, **kwargs) -> AgentEngineOperationUnion:
1298-
...
1299-
1300-
13011330
def _wrap_query_operation(*, method_name: str) -> Callable[..., Any]:
13021331
"""Wraps an Agent Engine method, creating a callable for `query` API.
13031332

0 commit comments

Comments
 (0)