@@ -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,10 @@ 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
577+ or os .environ .get ("GOOGLE_API_KEY" )
578+ ),
574579 }
575580
576581 async def _init_session (
@@ -708,9 +713,18 @@ def set_up(self):
708713
709714 os .environ ["GOOGLE_GENAI_USE_VERTEXAI" ] = "1"
710715 project = self ._tmpl_attrs .get ("project" )
711- os .environ ["GOOGLE_CLOUD_PROJECT" ] = project
716+ if project :
717+ os .environ ["GOOGLE_CLOUD_PROJECT" ] = project
712718 location = self ._tmpl_attrs .get ("location" )
713- os .environ ["GOOGLE_CLOUD_LOCATION" ] = location
719+ if location :
720+ os .environ ["GOOGLE_CLOUD_LOCATION" ] = location
721+ express_mode_api_key = self ._tmpl_attrs .get ("express_mode_api_key" )
722+ if express_mode_api_key and not project :
723+ os .environ ["GOOGLE_API_KEY" ] = express_mode_api_key
724+ # Clear location and project env vars if express mode api key is provided.
725+ os .environ .pop ("GOOGLE_CLOUD_LOCATION" , None )
726+ os .environ .pop ("GOOGLE_CLOUD_PROJECT" , None )
727+ location = None
714728
715729 # Disable content capture in custom ADK spans unless user enabled
716730 # tracing explicitly with the old flag
@@ -757,6 +771,8 @@ def set_up(self):
757771 VertexAiSessionService ,
758772 )
759773
774+ # If the express mode api key is set, it will be read from the
775+ # environment variable when initializing the session service.
760776 self ._tmpl_attrs ["session_service" ] = VertexAiSessionService (
761777 project = project ,
762778 location = location ,
@@ -767,6 +783,8 @@ def set_up(self):
767783 VertexAiSessionService ,
768784 )
769785
786+ # If the express mode api key is set, it will be read from the
787+ # environment variable when initializing the session service.
770788 self ._tmpl_attrs ["session_service" ] = VertexAiSessionService (
771789 project = project ,
772790 location = location ,
@@ -787,6 +805,8 @@ def set_up(self):
787805 VertexAiMemoryBankService ,
788806 )
789807
808+ # If the express mode api key is set, it will be read from the
809+ # environment variable when initializing the memory service.
790810 self ._tmpl_attrs ["memory_service" ] = VertexAiMemoryBankService (
791811 project = project ,
792812 location = location ,
0 commit comments