@@ -534,6 +534,7 @@ def __init__(
534534 If not provided, a default instrumentor builder will be used.
535535 This parameter is ignored if `enable_tracing` is False.
536536 """
537+ import os
537538 from google .cloud .aiplatform import initializer
538539
539540 adk_version = get_adk_version ()
@@ -571,6 +572,9 @@ def __init__(
571572 "artifact_service_builder" : artifact_service_builder ,
572573 "memory_service_builder" : memory_service_builder ,
573574 "instrumentor_builder" : instrumentor_builder ,
575+ "express_mode_api_key" : (
576+ initializer .global_config .api_key or os .environ .get ("GOOGLE_API_KEY" )
577+ ),
574578 }
575579
576580 async def _init_session (
@@ -708,9 +712,18 @@ def set_up(self):
708712
709713 os .environ ["GOOGLE_GENAI_USE_VERTEXAI" ] = "1"
710714 project = self ._tmpl_attrs .get ("project" )
711- os .environ ["GOOGLE_CLOUD_PROJECT" ] = project
715+ if project :
716+ os .environ ["GOOGLE_CLOUD_PROJECT" ] = project
712717 location = self ._tmpl_attrs .get ("location" )
713- os .environ ["GOOGLE_CLOUD_LOCATION" ] = location
718+ if location :
719+ os .environ ["GOOGLE_CLOUD_LOCATION" ] = location
720+ express_mode_api_key = self ._tmpl_attrs .get ("express_mode_api_key" )
721+ if express_mode_api_key and not project :
722+ os .environ ["GOOGLE_API_KEY" ] = express_mode_api_key
723+ # Clear location and project env vars if express mode api key is provided.
724+ os .environ .pop ("GOOGLE_CLOUD_LOCATION" , None )
725+ os .environ .pop ("GOOGLE_CLOUD_PROJECT" , None )
726+ location = None
714727
715728 # Disable content capture in custom ADK spans unless user enabled
716729 # tracing explicitly with the old flag
@@ -757,6 +770,8 @@ def set_up(self):
757770 VertexAiSessionService ,
758771 )
759772
773+ # If the express mode api key is set, it will be read from the
774+ # environment variable when initializing the session service.
760775 self ._tmpl_attrs ["session_service" ] = VertexAiSessionService (
761776 project = project ,
762777 location = location ,
@@ -767,6 +782,8 @@ def set_up(self):
767782 VertexAiSessionService ,
768783 )
769784
785+ # If the express mode api key is set, it will be read from the
786+ # environment variable when initializing the session service.
770787 self ._tmpl_attrs ["session_service" ] = VertexAiSessionService (
771788 project = project ,
772789 location = location ,
@@ -787,6 +804,8 @@ def set_up(self):
787804 VertexAiMemoryBankService ,
788805 )
789806
807+ # If the express mode api key is set, it will be read from the
808+ # environment variable when initializing the memory service.
790809 self ._tmpl_attrs ["memory_service" ] = VertexAiMemoryBankService (
791810 project = project ,
792811 location = location ,
0 commit comments