@@ -242,10 +242,10 @@ def to_agent_engine(
242242 agent_folder : str ,
243243 temp_folder : str ,
244244 adk_app : str ,
245- project : str ,
246- region : str ,
247245 staging_bucket : str ,
248246 trace_to_cloud : bool ,
247+ project : Optional [str ] = None ,
248+ region : Optional [str ] = None ,
249249 display_name : Optional [str ] = None ,
250250 description : Optional [str ] = None ,
251251 requirements_file : Optional [str ] = None ,
@@ -287,7 +287,9 @@ def to_agent_engine(
287287 If not specified, the `requirements.txt` file in the `agent_folder` will
288288 be used.
289289 env_file (str): The filepath to the `.env` file for environment variables.
290- If not specified, the `.env` file in the `agent_folder` will be used.
290+ If not specified, the `.env` file in the `agent_folder` will be used. The
291+ values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` will be
292+ overridden by `project` and `region` if they are specified.
291293 """
292294 # remove temp_folder if it exists
293295 if os .path .exists (temp_folder ):
@@ -306,13 +308,7 @@ def to_agent_engine(
306308 from vertexai import agent_engines
307309
308310 sys .path .append (temp_folder )
309-
310- vertexai .init (
311- project = _resolve_project (project ),
312- location = region ,
313- staging_bucket = staging_bucket ,
314- )
315- click .echo ('Vertex AI initialized.' )
311+ project = _resolve_project (project )
316312
317313 click .echo ('Resolving files and dependencies...' )
318314 if not requirements_file :
@@ -333,6 +329,37 @@ def to_agent_engine(
333329
334330 click .echo (f'Reading environment variables from { env_file } ' )
335331 env_vars = dotenv_values (env_file )
332+ if 'GOOGLE_CLOUD_PROJECT' in env_vars :
333+ env_project = env_vars .pop ('GOOGLE_CLOUD_PROJECT' )
334+ if env_project :
335+ if project :
336+ click .secho (
337+ 'Ignoring GOOGLE_CLOUD_PROJECT in .env as `--project` was'
338+ ' explicitly passed and takes precedence' ,
339+ fg = 'yellow' ,
340+ )
341+ else :
342+ project = env_project
343+ click .echo (f'{ project = } set by GOOGLE_CLOUD_PROJECT in { env_file } ' )
344+ if 'GOOGLE_CLOUD_LOCATION' in env_vars :
345+ env_region = env_vars .pop ('GOOGLE_CLOUD_LOCATION' )
346+ if env_region :
347+ if region :
348+ click .secho (
349+ 'Ignoring GOOGLE_CLOUD_LOCATION in .env as `--region` was'
350+ ' explicitly passed and takes precedence' ,
351+ fg = 'yellow' ,
352+ )
353+ else :
354+ region = env_region
355+ click .echo (f'{ region = } set by GOOGLE_CLOUD_LOCATION in { env_file } ' )
356+
357+ vertexai .init (
358+ project = project ,
359+ location = region ,
360+ staging_bucket = staging_bucket ,
361+ )
362+ click .echo ('Vertex AI initialized.' )
336363
337364 adk_app_file = f'{ adk_app } .py'
338365 with open (
0 commit comments