Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions google/genai/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ def __del__(self) -> None:
class BaseApiClient:
"""Client for calling HTTP APIs sending and receiving JSON."""

_VERTEXAI_MULTI_REGIONAL_LOCATIONS = ('us',)

def __init__(
self,
vertexai: Optional[bool] = None,
Expand Down Expand Up @@ -686,6 +688,13 @@ def __init__(
self.api_key or self.location == 'global'
) and not self.custom_base_url:
self._http_options.base_url = f'https://aiplatform.googleapis.com/'
elif (
self.location in self._VERTEXAI_MULTI_REGIONAL_LOCATIONS
and not self.custom_base_url
):
self._http_options.base_url = (
f'https://aiplatform.{self.location}.rep.googleapis.com/'
)
elif (
self.custom_base_url
and not self.custom_base_url.endswith('.googleapis.com')
Expand Down
16 changes: 16 additions & 0 deletions google/genai/tests/client/test_client_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,22 @@ def test_vertexai_no_default_location_when_location_explicitly_set(monkeypatch):
assert client.models._api_client.project == project_id


def test_vertexai_location_us_routing(monkeypatch):
# Verify that location='us' correctly routes to the us.rep endpoint
project_id = "fake_project_id"
location = "us"

with monkeypatch.context() as m:
m.delenv("GOOGLE_CLOUD_LOCATION", raising=False)
client = Client(vertexai=True, project=project_id, location=location)
assert client.models._api_client.location == location
assert client.models._api_client.project == project_id
assert (
client.models._api_client.get_read_only_http_options()["base_url"]
== "https://aiplatform.us.rep.googleapis.com/"
)


def test_vertexai_no_default_location_when_env_location_set(monkeypatch):
# Verify that location is NOT defaulted to global when set via environment
project_id = "fake_project_id"
Expand Down
Loading