Skip to content

Commit 1ba7a41

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: Model Garden list_deployable_models, list_models and list_publisher_model_deploy_options now read the catalog from us-central1 for clients in any location
PiperOrigin-RevId: 1007662216
1 parent 5b3dba9 commit 1ba7a41

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

‎agentplatform/_genai/model_garden.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,33 @@ def get_batch_prediction_job(
11571157
_DEFAULT_DEPLOY_TIMEOUT_SECONDS = 2 * 60 * 60
11581158
_DEFAULT_DEPLOY_POLL_INTERVAL_SECONDS = 30
11591159

1160+
# Model Garden catalog reads (ListPublisherModels, GetPublisherModel) are
1161+
# served from us-central1, as in vertexai.model_garden. Other regional
1162+
# endpoints return a partial catalog (e.g. no deploy configs for models that
1163+
# deploy fine there, since Deploy reads the global model configs).
1164+
_CATALOG_BASE_URL = "https://us-central1-aiplatform.googleapis.com/"
1165+
1166+
@staticmethod
1167+
def _catalog_http_options(
1168+
api_client: Any,
1169+
) -> Optional[genai_types.HttpOptions]:
1170+
"""Returns ``http_options`` that send catalog reads to us-central1.
1171+
1172+
Only the catalog reads are routed this way; deploy, export and batch
1173+
requests still go to the client's location.
1174+
1175+
Args:
1176+
api_client: The client's API client.
1177+
1178+
Returns:
1179+
``HttpOptions`` with the us-central1 base URL, or None when the client
1180+
was created with a custom ``base_url`` (e.g. a proxy or private
1181+
endpoint), which is then left unchanged.
1182+
"""
1183+
if api_client.custom_base_url:
1184+
return None
1185+
return genai_types.HttpOptions(base_url=ModelGarden._CATALOG_BASE_URL)
1186+
11601187
@staticmethod
11611188
def _build_filter_str(
11621189
model_filter: Optional[str],
@@ -1716,6 +1743,7 @@ def _list_all_publisher_models(
17161743
filter=api_config.filter,
17171744
list_all_versions=api_config.list_all_versions,
17181745
page_token=page_token,
1746+
http_options=api_config.http_options,
17191747
)
17201748
response = self._list_publisher_models(
17211749
parent="publishers/*",
@@ -1752,6 +1780,7 @@ def _list(
17521780
api_config = types.ListPublisherModelsConfig(
17531781
filter=filter_str,
17541782
list_all_versions=True,
1783+
http_options=ModelGarden._catalog_http_options(self._api_client),
17551784
)
17561785

17571786
models = self._list_all_publisher_models(api_config)
@@ -1865,6 +1894,7 @@ def list_publisher_model_deploy_options(
18651894
get_publisher_model_config = types.GetPublisherModelConfig(
18661895
is_hugging_face_model=self._is_hugging_face_model(model),
18671896
include_equivalent_model_garden_model_deployment_configs=True,
1897+
http_options=ModelGarden._catalog_http_options(self._api_client),
18681898
)
18691899
publisher_model = self._get_publisher_model(
18701900
name=self._reconcile_model_name(model),
@@ -3195,6 +3225,7 @@ async def _list_all_publisher_models(
31953225
filter=api_config.filter,
31963226
list_all_versions=api_config.list_all_versions,
31973227
page_token=page_token,
3228+
http_options=api_config.http_options,
31983229
)
31993230
response = await self._list_publisher_models(
32003231
parent="publishers/*",
@@ -3231,6 +3262,7 @@ async def _list(
32313262
api_config = types.ListPublisherModelsConfig(
32323263
filter=filter_str,
32333264
list_all_versions=True,
3265+
http_options=ModelGarden._catalog_http_options(self._api_client),
32343266
)
32353267

32363268
models = await self._list_all_publisher_models(api_config)
@@ -3342,6 +3374,7 @@ async def list_publisher_model_deploy_options(
33423374
api_config = types.GetPublisherModelConfig(
33433375
is_hugging_face_model=ModelGarden._is_hugging_face_model(model),
33443376
include_equivalent_model_garden_model_deployment_configs=True,
3377+
http_options=ModelGarden._catalog_http_options(self._api_client),
33453378
)
33463379
publisher_model = await self._get_publisher_model(
33473380
name=ModelGarden._reconcile_model_name(model), config=api_config

‎tests/unit/agentplatform/genai/test_genai_model_garden.py‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@
2828
_OPEN_MODEL = "google/gemma3@gemma-3-12b-it"
2929

3030

31+
_CATALOG_HTTP_OPTIONS = model_garden.genai_types.HttpOptions(
32+
base_url="https://us-central1-aiplatform.googleapis.com/"
33+
)
34+
35+
3136
@pytest.fixture
3237
def mock_client():
3338
mock_api_client = mock.Mock(spec=client.Client)
3439
mock_api_client.project = _TEST_PROJECT
3540
mock_api_client.location = _TEST_LOCATION
3641
mock_api_client.vertexai = True
42+
mock_api_client.custom_base_url = None
3743

3844
return model_garden.ModelGarden(mock_api_client)
3945

@@ -44,6 +50,7 @@ def mock_async_client():
4450
mock_api_client.project = _TEST_PROJECT
4551
mock_api_client.location = _TEST_LOCATION
4652
mock_api_client.vertexai = True
53+
mock_api_client.custom_base_url = None
4754

4855
return model_garden.AsyncModelGarden(mock_api_client)
4956

@@ -488,6 +495,7 @@ def test_list_publisher_model_deploy_options_basic(mock_client):
488495
config=types.GetPublisherModelConfig(
489496
is_hugging_face_model=False,
490497
include_equivalent_model_garden_model_deployment_configs=True,
498+
http_options=_CATALOG_HTTP_OPTIONS,
491499
),
492500
)
493501
assert len(options) == 1
@@ -826,10 +834,125 @@ def test_list_publisher_model_deploy_options_hugging_face_model(mock_client):
826834
config=types.GetPublisherModelConfig(
827835
is_hugging_face_model=True,
828836
include_equivalent_model_garden_model_deployment_configs=True,
837+
http_options=_CATALOG_HTTP_OPTIONS,
829838
),
830839
)
831840

832841

842+
# --- Catalog reads are served from us-central1 ---------------------------
843+
844+
845+
def test_list_models_catalog_routed_to_us_central1(mock_client):
846+
"""A client outside us-central1 still lists the catalog from us-central1."""
847+
mock_client._api_client.location = "europe-west4"
848+
with mock.patch.object(
849+
mock_client,
850+
"_list_publisher_models",
851+
return_value=types.ListPublisherModelsResponse(publisher_models=[]),
852+
) as mock_list:
853+
mock_client.list_deployable_models()
854+
mock_client.list_models()
855+
856+
for call in mock_list.call_args_list:
857+
assert call.kwargs["config"].http_options == _CATALOG_HTTP_OPTIONS
858+
859+
860+
def test_list_models_catalog_routing_kept_across_pages(mock_client):
861+
"""Every page request carries the us-central1 base URL, not just the first."""
862+
pages = [
863+
types.ListPublisherModelsResponse(
864+
publisher_models=[
865+
_make_deployable_model("publishers/google/models/a")
866+
],
867+
next_page_token="page-2",
868+
),
869+
types.ListPublisherModelsResponse(
870+
publisher_models=[
871+
_make_deployable_model("publishers/google/models/b")
872+
],
873+
),
874+
]
875+
with mock.patch.object(
876+
mock_client, "_list_publisher_models", side_effect=pages
877+
) as mock_list:
878+
models = mock_client.list_models()
879+
880+
assert models == ["google/a@001", "google/b@001"]
881+
second_config = mock_list.call_args_list[1].kwargs["config"]
882+
assert second_config.page_token == "page-2"
883+
assert second_config.http_options == _CATALOG_HTTP_OPTIONS
884+
885+
886+
def test_list_publisher_model_deploy_options_routed_to_us_central1(
887+
mock_client,
888+
):
889+
"""GetPublisherModel is sent to us-central1 for a client in another region."""
890+
mock_client._api_client.location = "asia-southeast1"
891+
dummy_model = _make_model_with_deploy_options([_make_deploy_option()])
892+
with mock.patch.object(
893+
mock_client, "_get_publisher_model", return_value=dummy_model
894+
) as mock_get:
895+
mock_client.list_publisher_model_deploy_options(model=_OPEN_MODEL)
896+
897+
assert mock_get.call_args.kwargs["config"].http_options == (
898+
_CATALOG_HTTP_OPTIONS
899+
)
900+
901+
902+
def test_catalog_reads_keep_custom_base_url(mock_client):
903+
"""A client created with a custom base_url is not redirected."""
904+
mock_client._api_client.custom_base_url = "https://my-proxy.example.com/"
905+
dummy_model = _make_model_with_deploy_options([_make_deploy_option()])
906+
with (
907+
mock.patch.object(
908+
mock_client,
909+
"_list_publisher_models",
910+
return_value=types.ListPublisherModelsResponse(publisher_models=[]),
911+
) as mock_list,
912+
mock.patch.object(
913+
mock_client, "_get_publisher_model", return_value=dummy_model
914+
) as mock_get,
915+
):
916+
mock_client.list_models()
917+
mock_client.list_publisher_model_deploy_options(model=_OPEN_MODEL)
918+
919+
assert mock_list.call_args.kwargs["config"].http_options is None
920+
assert mock_get.call_args.kwargs["config"].http_options is None
921+
922+
923+
def test_catalog_reads_routed_to_us_central1_async(mock_async_client):
924+
"""The async surface routes catalog reads the same way."""
925+
mock_async_client._api_client.location = "europe-west4"
926+
dummy_model = _make_model_with_deploy_options([_make_deploy_option()])
927+
with (
928+
mock.patch.object(
929+
mock_async_client,
930+
"_list_publisher_models",
931+
new=mock.AsyncMock(
932+
return_value=types.ListPublisherModelsResponse(
933+
publisher_models=[]
934+
)
935+
),
936+
) as mock_list,
937+
mock.patch.object(
938+
mock_async_client,
939+
"_get_publisher_model",
940+
new=mock.AsyncMock(return_value=dummy_model),
941+
) as mock_get,
942+
):
943+
asyncio.run(mock_async_client.list_deployable_models())
944+
asyncio.run(
945+
mock_async_client.list_publisher_model_deploy_options(model=_OPEN_MODEL)
946+
)
947+
948+
assert mock_list.call_args.kwargs["config"].http_options == (
949+
_CATALOG_HTTP_OPTIONS
950+
)
951+
assert mock_get.call_args.kwargs["config"].http_options == (
952+
_CATALOG_HTTP_OPTIONS
953+
)
954+
955+
833956
def test_list_publisher_model_deploy_options_async(mock_async_client):
834957
"""Tests the async client returns deploy options."""
835958
dummy_model = _make_model_with_deploy_options([_make_deploy_option()])

0 commit comments

Comments
 (0)