@@ -702,7 +702,21 @@ def __init__(
702702 )
703703 self ._http_options .api_version = 'v1beta1'
704704 else : # Implicit initialization or missing arguments.
705- if not self .api_key :
705+ if env_api_key and api_key :
706+ # Explicit api_key takes precedence over implicit api_key.
707+ logger .info (
708+ 'The client initializer api_key argument takes '
709+ 'precedence over the API key from the environment variable.'
710+ )
711+ if credentials :
712+ if env_api_key :
713+ logger .info (
714+ 'The user `credentials` argument will take precedence over the'
715+ ' api key from the environment variables.'
716+ )
717+ self .api_key = None
718+
719+ if not self .api_key and not credentials :
706720 raise ValueError (
707721 'No API key was provided. Please pass a valid API key. Learn how to'
708722 ' create an API key at'
@@ -1175,20 +1189,21 @@ def _request_once(
11751189 stream : bool = False ,
11761190 ) -> HttpResponse :
11771191 data : Optional [Union [str , bytes ]] = None
1178- # If using proj/location, fetch ADC
1179- if self .vertexai and (self .project or self .location ):
1192+
1193+ uses_vertex_creds = self .vertexai and (self .project or self .location )
1194+ uses_mldev_creds = not self .vertexai and self ._credentials
1195+ if (uses_vertex_creds or uses_mldev_creds ):
11801196 http_request .headers ['Authorization' ] = f'Bearer { self ._access_token ()} '
11811197 if self ._credentials and self ._credentials .quota_project_id :
11821198 http_request .headers ['x-goog-user-project' ] = (
11831199 self ._credentials .quota_project_id
11841200 )
1185- data = json .dumps (http_request .data ) if http_request .data else None
1186- else :
1187- if http_request .data :
1188- if not isinstance (http_request .data , bytes ):
1189- data = json .dumps (http_request .data ) if http_request .data else None
1190- else :
1191- data = http_request .data
1201+
1202+ if http_request .data :
1203+ if not isinstance (http_request .data , bytes ):
1204+ data = json .dumps (http_request .data )
1205+ else :
1206+ data = http_request .data
11921207
11931208 if stream :
11941209 httpx_request = self ._httpx_client .build_request (
@@ -1241,22 +1256,22 @@ async def _async_request_once(
12411256 ) -> HttpResponse :
12421257 data : Optional [Union [str , bytes ]] = None
12431258
1244- # If using proj/location, fetch ADC
1245- if self .vertexai and (self .project or self .location ):
1259+ uses_vertex_creds = self .vertexai and (self .project or self .location )
1260+ uses_mldev_creds = not self .vertexai and self ._credentials
1261+ if (uses_vertex_creds or uses_mldev_creds ):
12461262 http_request .headers ['Authorization' ] = (
12471263 f'Bearer { await self ._async_access_token ()} '
12481264 )
12491265 if self ._credentials and self ._credentials .quota_project_id :
12501266 http_request .headers ['x-goog-user-project' ] = (
12511267 self ._credentials .quota_project_id
12521268 )
1253- data = json .dumps (http_request .data ) if http_request .data else None
1254- else :
1255- if http_request .data :
1256- if not isinstance (http_request .data , bytes ):
1257- data = json .dumps (http_request .data ) if http_request .data else None
1258- else :
1259- data = http_request .data
1269+
1270+ if http_request .data :
1271+ if not isinstance (http_request .data , bytes ):
1272+ data = json .dumps (http_request .data )
1273+ else :
1274+ data = http_request .data
12601275
12611276 if stream :
12621277 if self ._use_aiohttp ():
0 commit comments