@@ -547,6 +547,7 @@ def __init__(
547547 http_options : Optional [HttpOptionsOrDict ] = None ,
548548 ):
549549 self .vertexai = vertexai
550+ self .custom_base_url = None
550551 if self .vertexai is None :
551552 if os .environ .get ('GOOGLE_GENAI_USE_VERTEXAI' , '0' ).lower () in [
552553 'true' ,
@@ -628,29 +629,37 @@ def __init__(
628629 )
629630 self .api_key = None
630631
632+ self .custom_base_url = (
633+ validated_http_options .base_url
634+ if validated_http_options .base_url
635+ else None
636+ )
637+
631638 # Skip fetching project from ADC if base url is provided in http options.
632639 if (
633640 not self .project
634641 and not self .api_key
635- and not validated_http_options . base_url
642+ and not self . custom_base_url
636643 ):
637644 credentials , self .project = load_auth (project = None )
638645 if not self ._credentials :
639646 self ._credentials = credentials
640647
641648 has_sufficient_auth = (self .project and self .location ) or self .api_key
642649
643- if not has_sufficient_auth and not validated_http_options . base_url :
650+ if not has_sufficient_auth and not self . custom_base_url :
644651 # Skip sufficient auth check if base url is provided in http options.
645652 raise ValueError (
646653 'Project and location or API key must be set when using the Vertex '
647654 'AI API.'
648655 )
649656 if self .api_key or self .location == 'global' :
650657 self ._http_options .base_url = f'https://aiplatform.googleapis.com/'
651- elif validated_http_options . base_url and not has_sufficient_auth :
658+ elif self . custom_base_url and not has_sufficient_auth :
652659 # Avoid setting default base url and api version if base_url provided.
653- self ._http_options .base_url = validated_http_options .base_url
660+ # API gateway proxy can use the auth in custom headers, not url.
661+ # Enable custom url if auth is not sufficient.
662+ self ._http_options .base_url = self .custom_base_url
654663 else :
655664 self ._http_options .base_url = (
656665 f'https://{ self .location } -aiplatform.googleapis.com/'
@@ -897,6 +906,11 @@ def _use_aiohttp(self) -> bool:
897906 )
898907
899908 def _websocket_base_url (self ) -> str :
909+ has_sufficient_auth = (self .project and self .location ) or self .api_key
910+ if self .custom_base_url and not has_sufficient_auth :
911+ # API gateway proxy can use the auth in custom headers, not url.
912+ # Enable custom url if auth is not sufficient.
913+ return self .custom_base_url
900914 url_parts = urlparse (self ._http_options .base_url )
901915 return url_parts ._replace (scheme = 'wss' ).geturl () # type: ignore[arg-type, return-value]
902916
0 commit comments