@@ -451,6 +451,7 @@ def __init__(
451
451
http_options : Optional [HttpOptionsOrDict ] = None ,
452
452
):
453
453
self .vertexai = vertexai
454
+ self .custom_base_url = None
454
455
if self .vertexai is None :
455
456
if os .environ .get ('GOOGLE_GENAI_USE_VERTEXAI' , '0' ).lower () in [
456
457
'true' ,
@@ -536,29 +537,33 @@ def __init__(
536
537
)
537
538
self .api_key = None
538
539
540
+ self .custom_base_url = (
541
+ validated_http_options .base_url
542
+ if validated_http_options .base_url
543
+ else None
544
+ )
545
+
539
546
# Skip fetching project from ADC if base url is provided in http options.
540
- if (
541
- not self .project
542
- and not self .api_key
543
- and not validated_http_options .base_url
544
- ):
547
+ if not self .project and not self .api_key and not self .custom_base_url :
545
548
credentials , self .project = _load_auth (project = None )
546
549
if not self ._credentials :
547
550
self ._credentials = credentials
548
551
549
552
has_sufficient_auth = (self .project and self .location ) or self .api_key
550
553
551
- if ( not has_sufficient_auth and not validated_http_options . base_url ) :
554
+ if not has_sufficient_auth and not self . custom_base_url :
552
555
# Skip sufficient auth check if base url is provided in http options.
553
556
raise ValueError (
554
557
'Project and location or API key must be set when using the Vertex '
555
558
'AI API.'
556
559
)
557
560
if self .api_key or self .location == 'global' :
558
561
self ._http_options .base_url = f'https://aiplatform.googleapis.com/'
559
- elif validated_http_options . base_url and not has_sufficient_auth :
562
+ elif self . custom_base_url and not has_sufficient_auth :
560
563
# Avoid setting default base url and api version if base_url provided.
561
- self ._http_options .base_url = validated_http_options .base_url
564
+ # API gateway proxy can use the auth in custom headers, not url.
565
+ # Enable custom url if auth is not sufficient.
566
+ self ._http_options .base_url = self .custom_base_url
562
567
else :
563
568
self ._http_options .base_url = (
564
569
f'https://{ self .location } -aiplatform.googleapis.com/'
@@ -793,6 +798,11 @@ def _use_aiohttp(self) -> bool:
793
798
)
794
799
795
800
def _websocket_base_url (self ) -> str :
801
+ has_sufficient_auth = (self .project and self .location ) or self .api_key
802
+ if self .custom_base_url and not has_sufficient_auth :
803
+ # API gateway proxy can use the auth in custom headers, not url.
804
+ # Enable custom url if auth is not sufficient.
805
+ return self .custom_base_url
796
806
url_parts = urlparse (self ._http_options .base_url )
797
807
return url_parts ._replace (scheme = 'wss' ).geturl () # type: ignore[arg-type, return-value]
798
808
0 commit comments