diff --git a/google/genai/_api_client.py b/google/genai/_api_client.py index 751211e51..5cb099735 100644 --- a/google/genai/_api_client.py +++ b/google/genai/_api_client.py @@ -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, @@ -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') diff --git a/google/genai/tests/client/test_client_initialization.py b/google/genai/tests/client/test_client_initialization.py index 7b0136044..b4c3a9811 100644 --- a/google/genai/tests/client/test_client_initialization.py +++ b/google/genai/tests/client/test_client_initialization.py @@ -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"