@@ -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
@@ -783,6 +796,8 @@ def set_up(self):
783796 VertexAiSessionService ,
784797 )
785798
799+ # If the express mode api key is set, it will be read from the
800+ # environment variable when initializing the session service.
786801 self ._tmpl_attrs ["session_service" ] = VertexAiSessionService (
787802 project = project ,
788803 location = location ,
@@ -793,6 +808,8 @@ def set_up(self):
793808 VertexAiSessionService ,
794809 )
795810
811+ # If the express mode api key is set, it will be read from the
812+ # environment variable when initializing the session service.
796813 self ._tmpl_attrs ["session_service" ] = VertexAiSessionService (
797814 project = project ,
798815 location = location ,
@@ -813,6 +830,8 @@ def set_up(self):
813830 VertexAiMemoryBankService ,
814831 )
815832
833+ # If the express mode api key is set, it will be read from the
834+ # environment variable when initializing the memory service.
816835 self ._tmpl_attrs ["memory_service" ] = VertexAiMemoryBankService (
817836 project = project ,
818837 location = location ,
0 commit comments