Skip to content

Commit 8e3e00c

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: support us region routing
PiperOrigin-RevId: 890026941
1 parent 208a173 commit 8e3e00c

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

google/genai/_api_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,10 @@ def __init__(
702702
self.api_key or self.location == 'global'
703703
) and not self.custom_base_url:
704704
self._http_options.base_url = f'https://aiplatform.googleapis.com/'
705+
elif self.location == 'us' and not self.custom_base_url:
706+
self._http_options.base_url = (
707+
f'https://aiplatform.{self.location}.rep.googleapis.com/'
708+
)
705709
elif (
706710
self.custom_base_url
707711
and not self.custom_base_url.endswith('.googleapis.com')

google/genai/tests/client/test_client_initialization.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,43 @@ def test_vertexai_no_default_location_when_location_explicitly_set(monkeypatch):
516516
assert client.models._api_client.project == project_id
517517

518518

519+
def test_vertexai_location_us_routing(monkeypatch):
520+
# Verify that location='us' correctly routes to the us.rep endpoint
521+
project_id = "fake_project_id"
522+
location = "us"
523+
524+
with monkeypatch.context() as m:
525+
m.delenv("GOOGLE_CLOUD_LOCATION", raising=False)
526+
client = Client(vertexai=True, project=project_id, location=location)
527+
assert client.models._api_client.location == location
528+
assert client.models._api_client.project == project_id
529+
assert (
530+
client.models._api_client.get_read_only_http_options()["base_url"]
531+
== "https://aiplatform.us.rep.googleapis.com/"
532+
)
533+
534+
535+
def test_vertexai_location_us_routing_base_url_override(monkeypatch):
536+
# Verify that base_url override takes precedence over location='us' routing
537+
project_id = "fake_project_id"
538+
location = "us"
539+
540+
with monkeypatch.context() as m:
541+
m.delenv("GOOGLE_CLOUD_LOCATION", raising=False)
542+
client = Client(
543+
vertexai=True,
544+
project=project_id,
545+
location=location,
546+
http_options={"base_url": "https://my-custom-url.com/"},
547+
)
548+
assert client.models._api_client.location == location
549+
assert client.models._api_client.project == project_id
550+
assert (
551+
client.models._api_client.get_read_only_http_options()["base_url"]
552+
== "https://my-custom-url.com/"
553+
)
554+
555+
519556
def test_vertexai_no_default_location_when_env_location_set(monkeypatch):
520557
# Verify that location is NOT defaulted to global when set via environment
521558
project_id = "fake_project_id"

0 commit comments

Comments
 (0)