Skip to content

Commit edaedb1

Browse files
yeesiancopybara-github
authored andcommitted
fix: GenAI SDK client - Provide the operation error message if it is available
PiperOrigin-RevId: 799667737
1 parent 93561a7 commit edaedb1

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

tests/unit/vertexai/genai/replays/test_list_agent_engine_memories.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def test_list_memories(client):
3535
)
3636
assert len(memory_list) == 1
3737
assert isinstance(memory_list[0], types.Memory)
38+
# Clean up resources.
39+
agent_engine.delete(force=True)
3840

3941

4042
pytestmark = pytest_helper.setup(

tests/unit/vertexai/genai/replays/test_retrieve_agent_engine_memories.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def test_retrieve_memories_with_similarity_search_params(client):
6868
)
6969
== 2
7070
)
71+
# Clean up resources.
72+
agent_engine.delete(force=True)
7173

7274

7375
def test_retrieve_memories_with_simple_retrieval_params(client):
@@ -87,6 +89,8 @@ def test_retrieve_memories_with_simple_retrieval_params(client):
8789
assert isinstance(memories, pagers.Pager)
8890
assert isinstance(memories.page[0], types.RetrieveMemoriesResponseRetrievedMemory)
8991
assert memories.page_size == 1
92+
# Clean up resources.
93+
agent_engine.delete(force=True)
9094

9195

9296
pytestmark = pytest_helper.setup(

tests/unit/vertexai/genai/test_agent_engines.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,10 @@ def register_operations(self) -> Dict[str, List[str]]:
617617
"pydantic": ["pydantic"],
618618
}
619619
_TEST_OPERATION_NAME = "test_operation_name"
620+
_TEST_AGENT_ENGINE_ERROR = {
621+
"message": "The following quotas are exceeded",
622+
"code": 8,
623+
}
620624

621625

622626
def _create_empty_fake_package(package_name: str) -> str:
@@ -1830,6 +1834,49 @@ def setup_method(self):
18301834
)
18311835
self.test_agent = CapitalizeEngine()
18321836

1837+
@mock.patch.object(_agent_engines_utils, "_prepare")
1838+
@mock.patch.object(_agent_engines_utils, "_await_operation")
1839+
def test_create_agent_engine_error(self, mock_await_operation, mock_prepare):
1840+
mock_await_operation.return_value = _genai_types.AgentEngineOperation(
1841+
error=_TEST_AGENT_ENGINE_ERROR,
1842+
)
1843+
with mock.patch.object(
1844+
self.client.agent_engines._api_client, "request"
1845+
) as request_mock:
1846+
request_mock.return_value = genai_types.HttpResponse(body="")
1847+
with pytest.raises(RuntimeError) as excinfo:
1848+
self.client.agent_engines.create(
1849+
agent=self.test_agent,
1850+
config=_genai_types.AgentEngineConfig(
1851+
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
1852+
description=_TEST_AGENT_ENGINE_DESCRIPTION,
1853+
requirements=_TEST_AGENT_ENGINE_REQUIREMENTS,
1854+
extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH],
1855+
staging_bucket=_TEST_STAGING_BUCKET,
1856+
gcs_dir_name=_TEST_GCS_DIR_NAME,
1857+
env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT,
1858+
),
1859+
)
1860+
assert "Failed to create agent engine" in str(excinfo.value)
1861+
1862+
@mock.patch.object(_agent_engines_utils, "_await_operation")
1863+
def test_update_agent_engine_description(self, mock_await_operation):
1864+
mock_await_operation.return_value = _genai_types.AgentEngineOperation(
1865+
error=_TEST_AGENT_ENGINE_ERROR,
1866+
)
1867+
with mock.patch.object(
1868+
self.client.agent_engines._api_client, "request"
1869+
) as request_mock:
1870+
request_mock.return_value = genai_types.HttpResponse(body="")
1871+
with pytest.raises(RuntimeError) as excinfo:
1872+
self.client.agent_engines.update(
1873+
name=_TEST_AGENT_ENGINE_RESOURCE_NAME,
1874+
config=_genai_types.AgentEngineConfig(
1875+
description=_TEST_AGENT_ENGINE_DESCRIPTION,
1876+
),
1877+
)
1878+
assert "Failed to update agent engine" in str(excinfo.value)
1879+
18331880
@pytest.mark.parametrize(
18341881
"test_case_name, test_operation_schemas, want_log_output",
18351882
[

vertexai/_genai/agent_engines.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,8 @@ def create(
11251125
logger.info(
11261126
f"agent_engine=client.agent_engines.get('{agent_engine.api_resource.name}')"
11271127
)
1128+
elif operation.error:
1129+
raise RuntimeError(f"Failed to create Agent Engine: {operation.error}")
11281130
else:
11291131
logger.warning("The operation returned an empty response.")
11301132
if agent is not None:
@@ -1516,6 +1518,8 @@ def update(
15161518
logger.info(
15171519
f"agent_engine=client.agent_engines.get('{agent_engine.api_resource.name}')"
15181520
)
1521+
elif operation.error:
1522+
raise RuntimeError(f"Failed to update Agent Engine: {operation.error}")
15191523
if agent_engine.api_resource.spec:
15201524
self._register_api_methods(agent_engine=agent_engine)
15211525
return agent_engine

vertexai/_genai/memories.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,9 +1222,12 @@ def create(
12221222
)
12231223
# We need to make a call to get the memory because the operation
12241224
# response might not contain the relevant fields.
1225-
if not operation.response:
1226-
raise ValueError("Error retrieving memory.")
1227-
operation.response = self.get(name=operation.response.name)
1225+
if operation.response:
1226+
operation.response = self.get(name=operation.response.name)
1227+
elif operation.error:
1228+
raise RuntimeError(f"Failed to create memory: {operation.error}")
1229+
else:
1230+
raise RuntimeError("Error creating memory.")
12281231
return operation
12291232

12301233
def generate(
@@ -1285,10 +1288,14 @@ def generate(
12851288
elif isinstance(config, dict):
12861289
config = types.GenerateAgentEngineMemoriesConfig.model_validate(config)
12871290
if config.wait_for_completion and not operation.done:
1288-
return _agent_engines_utils._await_operation(
1291+
operation = _agent_engines_utils._await_operation(
12891292
operation_name=operation.name,
12901293
get_operation_fn=self._get_generate_memories_operation,
12911294
)
1295+
if not operation.response:
1296+
if operation.error:
1297+
raise RuntimeError(f"Failed to generate memory: {operation.error}")
1298+
raise RuntimeError(f"Error generating memory: {operation}")
12921299
return operation
12931300

12941301
def list(

vertexai/_genai/sessions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,15 @@ def create(
792792
operation_name=operation.name,
793793
get_operation_fn=self._get_session_operation,
794794
)
795-
if not operation.response:
796-
raise ValueError(
795+
if operation.response:
796+
operation.response = self.get(name=operation.response.name)
797+
elif operation.error:
798+
raise RuntimeError(f"Failed to create session: {operation.error}")
799+
else:
800+
raise RuntimeError(
797801
"Error retrieving session from the operation response. "
798802
f"Operation name: {operation.name}"
799803
)
800-
operation.response = self.get(name=operation.response.name)
801804
return operation
802805

803806
def list(

0 commit comments

Comments
 (0)