Skip to content

Commit 2fd8ca0

Browse files
cleop-googlecopybara-github
authored andcommitted
feat: GenAI SDK client(multimodal) - Allow passing dataset ID in addition to full resource name in dataset methods.
PiperOrigin-RevId: 905067692
1 parent 6332d33 commit 2fd8ca0

3 files changed

Lines changed: 111 additions & 29 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def test_get_dataset_from_public_method(client):
4141
assert dataset.display_name == "test-display-name"
4242

4343

44+
def test_get_dataset_by_id(client):
45+
dataset = client.datasets.get_multimodal_dataset(
46+
name="8810841321427173376",
47+
)
48+
assert isinstance(dataset, types.MultimodalDataset)
49+
assert dataset.name == DATASET
50+
assert dataset.display_name == "test-display-name"
51+
52+
4453
pytestmark = pytest_helper.setup(
4554
file=__file__,
4655
globals_for_file=globals(),
@@ -67,3 +76,13 @@ async def test_get_dataset_from_public_method_async(client):
6776
assert isinstance(dataset, types.MultimodalDataset)
6877
assert dataset.name == DATASET
6978
assert dataset.display_name == "test-display-name"
79+
80+
81+
@pytest.mark.asyncio
82+
async def test_get_dataset_by_id_async(client):
83+
dataset = await client.aio.datasets.get_multimodal_dataset(
84+
name="8810841321427173376",
85+
)
86+
assert isinstance(dataset, types.MultimodalDataset)
87+
assert dataset.name == DATASET
88+
assert dataset.display_name == "test-display-name"

vertexai/_genai/_datasets_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,10 @@ async def save_dataframe_to_bigquery_async(
262262
)
263263
await asyncio.to_thread(copy_job.result)
264264
await asyncio.to_thread(bq_client.delete_table, temp_table_id)
265+
266+
267+
def resolve_dataset_name(resource_name_or_id: str, project: str, location: str) -> str:
268+
"""Resolves a dataset name or ID to a full resource name."""
269+
if "/" not in resource_name_or_id:
270+
return f"projects/{project}/locations/{location}/datasets/{resource_name_or_id}"
271+
return resource_name_or_id

vertexai/_genai/datasets.py

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,8 @@ def get_multimodal_dataset(
11461146
11471147
Args:
11481148
name:
1149-
Required. name of a multimodal dataset. The name should be in
1150-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1149+
Required. A fully-qualified resource name or ID of the dataset.
1150+
Example: "projects/.../locations/.../datasets/123" or "123".
11511151
config:
11521152
Optional. A configuration for getting the multimodal dataset. If not
11531153
provided, the default configuration will be used.
@@ -1161,6 +1161,10 @@ def get_multimodal_dataset(
11611161
elif not config:
11621162
config = types.VertexBaseConfig()
11631163

1164+
name = _datasets_utils.resolve_dataset_name(
1165+
name, self._api_client.project, self._api_client.location
1166+
)
1167+
11641168
return self._get_multimodal_dataset(config=config, name=name)
11651169

11661170
def delete_multimodal_dataset(
@@ -1173,8 +1177,8 @@ def delete_multimodal_dataset(
11731177
11741178
Args:
11751179
name:
1176-
Required. name of a multimodal dataset. The name should be in
1177-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1180+
Required. A fully-qualified resource name or ID of the dataset.
1181+
Example: "projects/.../locations/.../datasets/123" or "123".
11781182
config:
11791183
Optional. A configuration for deleting the multimodal dataset. If not
11801184
provided, the default configuration will be used.
@@ -1188,6 +1192,10 @@ def delete_multimodal_dataset(
11881192
elif not config:
11891193
config = types.VertexBaseConfig()
11901194

1195+
name = _datasets_utils.resolve_dataset_name(
1196+
name, self._api_client.project, self._api_client.location
1197+
)
1198+
11911199
return self._delete_multimodal_dataset(config=config, name=name)
11921200

11931201
def assemble(
@@ -1205,8 +1213,8 @@ def assemble(
12051213
12061214
Args:
12071215
name:
1208-
Required. The name of the dataset to assemble. The name should be in
1209-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1216+
Required. A fully-qualified resource name or ID of the dataset.
1217+
Example: "projects/.../locations/.../datasets/123" or "123".
12101218
gemini_request_read_config:
12111219
Optional. The read config to use to assemble the dataset. If
12121220
not provided, the read config attached to the dataset will be
@@ -1223,6 +1231,10 @@ def assemble(
12231231
elif not config:
12241232
config = types.AssembleDatasetConfig()
12251233

1234+
name = _datasets_utils.resolve_dataset_name(
1235+
name, self._api_client.project, self._api_client.location
1236+
)
1237+
12261238
operation = self._assemble_multimodal_dataset(
12271239
name=name,
12281240
gemini_request_read_config=gemini_request_read_config,
@@ -1248,8 +1260,8 @@ def assess_tuning_resources(
12481260
12491261
Args:
12501262
dataset_name:
1251-
Required. The name of the dataset to assess the tuning resources
1252-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1263+
Required. A fully-qualified resource name or ID of the dataset.
1264+
Example: "projects/.../locations/.../datasets/123" or "123".
12531265
model_name:
12541266
Required. The name of the model to assess the tuning resources
12551267
for.
@@ -1271,6 +1283,10 @@ def assess_tuning_resources(
12711283
elif not config:
12721284
config = types.AssessDatasetConfig()
12731285

1286+
dataset_name = _datasets_utils.resolve_dataset_name(
1287+
dataset_name, self._api_client.project, self._api_client.location
1288+
)
1289+
12741290
operation = self._assess_multimodal_dataset(
12751291
name=dataset_name,
12761292
tuning_resource_usage_assessment_config=types.TuningResourceUsageAssessmentConfig(
@@ -1304,8 +1320,8 @@ def assess_tuning_validity(
13041320
13051321
Args:
13061322
dataset_name:
1307-
Required. The name of the dataset to assess the tuning validity
1308-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1323+
Required. A fully-qualified resource name or ID of the dataset.
1324+
Example: "projects/.../locations/.../datasets/123" or "123".
13091325
model_name:
13101326
Required. The name of the model to assess the tuning validity
13111327
for.
@@ -1332,6 +1348,10 @@ def assess_tuning_validity(
13321348
elif not config:
13331349
config = types.AssessDatasetConfig()
13341350

1351+
dataset_name = _datasets_utils.resolve_dataset_name(
1352+
dataset_name, self._api_client.project, self._api_client.location
1353+
)
1354+
13351355
operation = self._assess_multimodal_dataset(
13361356
name=dataset_name,
13371357
tuning_validation_assessment_config=types.TuningValidationAssessmentConfig(
@@ -1364,8 +1384,8 @@ def assess_batch_prediction_resources(
13641384
13651385
Args:
13661386
dataset_name:
1367-
Required. The name of the dataset to assess the batch prediction
1368-
resources. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1387+
Required. A fully-qualified resource name or ID of the dataset.
1388+
Example: "projects/.../locations/.../datasets/123" or "123".
13691389
model_name:
13701390
Required. The name of the model to assess the batch prediction
13711391
resources.
@@ -1392,6 +1412,10 @@ def assess_batch_prediction_resources(
13921412
elif not config:
13931413
config = types.AssessDatasetConfig()
13941414

1415+
dataset_name = _datasets_utils.resolve_dataset_name(
1416+
dataset_name, self._api_client.project, self._api_client.location
1417+
)
1418+
13951419
operation = self._assess_multimodal_dataset(
13961420
name=dataset_name,
13971421
batch_prediction_resource_usage_assessment_config=types.BatchPredictionResourceUsageAssessmentConfig(
@@ -1425,8 +1449,8 @@ def assess_batch_prediction_validity(
14251449
14261450
Args:
14271451
dataset_name:
1428-
Required. The name of the dataset to assess the batch prediction
1429-
validity for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1452+
Required. A fully-qualified resource name or ID of the dataset.
1453+
Example: "projects/.../locations/.../datasets/123" or "123".
14301454
model_name:
14311455
Required. The name of the model to assess the batch prediction
14321456
validity for.
@@ -1451,6 +1475,10 @@ def assess_batch_prediction_validity(
14511475
elif not config:
14521476
config = types.AssessDatasetConfig()
14531477

1478+
dataset_name = _datasets_utils.resolve_dataset_name(
1479+
dataset_name, self._api_client.project, self._api_client.location
1480+
)
1481+
14541482
operation = self._assess_multimodal_dataset(
14551483
name=dataset_name,
14561484
batch_prediction_validation_assessment_config=types.BatchPredictionValidationAssessmentConfig(
@@ -2384,21 +2412,25 @@ async def get_multimodal_dataset(
23842412
23852413
Args:
23862414
name:
2387-
Required. name of a multimodal dataset. The name should be in
2388-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2415+
Required. A fully-qualified resource name or ID of the dataset.
2416+
Example: "projects/.../locations/.../datasets/123" or "123".
23892417
config:
23902418
Optional. A configuration for getting the multimodal dataset. If not
23912419
provided, the default configuration will be used.
23922420
23932421
Returns:
2394-
A types.MultimodalDataset object representing the updated multimodal
2422+
A types.MultimodalDataset object representing the retrieved multimodal
23952423
dataset.
23962424
"""
23972425
if isinstance(config, dict):
23982426
config = types.VertexBaseConfig(**config)
23992427
elif not config:
24002428
config = types.VertexBaseConfig()
24012429

2430+
name = _datasets_utils.resolve_dataset_name(
2431+
name, self._api_client.project, self._api_client.location
2432+
)
2433+
24022434
return await self._get_multimodal_dataset(config=config, name=name)
24032435

24042436
async def delete_multimodal_dataset(
@@ -2411,8 +2443,8 @@ async def delete_multimodal_dataset(
24112443
24122444
Args:
24132445
name:
2414-
Required. name of a multimodal dataset. The name should be in
2415-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2446+
Required. A fully-qualified resource name or ID of the dataset.
2447+
Example: "projects/.../locations/.../datasets/123" or "123".
24162448
config:
24172449
Optional. A configuration for deleting the multimodal dataset. If not
24182450
provided, the default configuration will be used.
@@ -2426,6 +2458,10 @@ async def delete_multimodal_dataset(
24262458
elif not config:
24272459
config = types.VertexBaseConfig()
24282460

2461+
name = _datasets_utils.resolve_dataset_name(
2462+
name, self._api_client.project, self._api_client.location
2463+
)
2464+
24292465
return await self._delete_multimodal_dataset(config=config, name=name)
24302466

24312467
async def assemble(
@@ -2443,8 +2479,8 @@ async def assemble(
24432479
24442480
Args:
24452481
name:
2446-
Required. The name of the dataset to assemble. The name should be in
2447-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2482+
Required. A fully-qualified resource name or ID of the dataset.
2483+
Example: "projects/.../locations/.../datasets/123" or "123".
24482484
gemini_request_read_config:
24492485
Optional. The read config to use to assemble the dataset. If
24502486
not provided, the read config attached to the dataset will be
@@ -2461,6 +2497,10 @@ async def assemble(
24612497
elif not config:
24622498
config = types.AssembleDatasetConfig()
24632499

2500+
name = _datasets_utils.resolve_dataset_name(
2501+
name, self._api_client.project, self._api_client.location
2502+
)
2503+
24642504
operation = await self._assemble_multimodal_dataset(
24652505
name=name,
24662506
gemini_request_read_config=gemini_request_read_config,
@@ -2486,8 +2526,8 @@ async def assess_tuning_resources(
24862526
24872527
Args:
24882528
dataset_name:
2489-
Required. The name of the dataset to assess the tuning resources
2490-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2529+
Required. A fully-qualified resource name or ID of the dataset.
2530+
Example: "projects/.../locations/.../datasets/123" or "123".
24912531
model_name:
24922532
Required. The name of the model to assess the tuning resources
24932533
for.
@@ -2509,6 +2549,10 @@ async def assess_tuning_resources(
25092549
elif not config:
25102550
config = types.AssessDatasetConfig()
25112551

2552+
dataset_name = _datasets_utils.resolve_dataset_name(
2553+
dataset_name, self._api_client.project, self._api_client.location
2554+
)
2555+
25122556
operation = await self._assess_multimodal_dataset(
25132557
name=dataset_name,
25142558
tuning_resource_usage_assessment_config=types.TuningResourceUsageAssessmentConfig(
@@ -2542,8 +2586,8 @@ async def assess_tuning_validity(
25422586
25432587
Args:
25442588
dataset_name:
2545-
Required. The name of the dataset to assess the tuning validity
2546-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2589+
Required. A fully-qualified resource name or ID of the dataset.
2590+
Example: "projects/.../locations/.../datasets/123" or "123".
25472591
model_name:
25482592
Required. The name of the model to assess the tuning validity
25492593
for.
@@ -2570,6 +2614,10 @@ async def assess_tuning_validity(
25702614
elif not config:
25712615
config = types.AssessDatasetConfig()
25722616

2617+
dataset_name = _datasets_utils.resolve_dataset_name(
2618+
dataset_name, self._api_client.project, self._api_client.location
2619+
)
2620+
25732621
operation = await self._assess_multimodal_dataset(
25742622
name=dataset_name,
25752623
tuning_validation_assessment_config=types.TuningValidationAssessmentConfig(
@@ -2602,8 +2650,8 @@ async def assess_batch_prediction_resources(
26022650
26032651
Args:
26042652
dataset_name:
2605-
Required. The name of the dataset to assess the batch prediction
2606-
resources. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2653+
Required. A fully-qualified resource name or ID of the dataset.
2654+
Example: "projects/.../locations/.../datasets/123" or "123".
26072655
model_name:
26082656
Required. The name of the model to assess the batch prediction
26092657
resources.
@@ -2630,6 +2678,10 @@ async def assess_batch_prediction_resources(
26302678
elif not config:
26312679
config = types.AssessDatasetConfig()
26322680

2681+
dataset_name = _datasets_utils.resolve_dataset_name(
2682+
dataset_name, self._api_client.project, self._api_client.location
2683+
)
2684+
26332685
operation = await self._assess_multimodal_dataset(
26342686
name=dataset_name,
26352687
batch_prediction_resource_usage_assessment_config=types.BatchPredictionResourceUsageAssessmentConfig(
@@ -2663,8 +2715,8 @@ async def assess_batch_prediction_validity(
26632715
26642716
Args:
26652717
dataset_name:
2666-
Required. The name of the dataset to assess the batch prediction
2667-
validity for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2718+
Required. A fully-qualified resource name or ID of the dataset.
2719+
Example: "projects/.../locations/.../datasets/123" or "123".
26682720
model_name:
26692721
Required. The name of the model to assess the batch prediction
26702722
validity for.
@@ -2689,6 +2741,10 @@ async def assess_batch_prediction_validity(
26892741
elif not config:
26902742
config = types.AssessDatasetConfig()
26912743

2744+
dataset_name = _datasets_utils.resolve_dataset_name(
2745+
dataset_name, self._api_client.project, self._api_client.location
2746+
)
2747+
26922748
operation = await self._assess_multimodal_dataset(
26932749
name=dataset_name,
26942750
batch_prediction_validation_assessment_config=types.BatchPredictionValidationAssessmentConfig(

0 commit comments

Comments
 (0)