Skip to content

Commit 05834cb

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add support for Vertex Express Mode API key in AdkApp
PiperOrigin-RevId: 827628603
1 parent 7d18e72 commit 05834cb

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

tests/unit/vertex_adk/test_agent_engine_templates_adk.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, name: str, model: str):
5454

5555
_TEST_LOCATION = "us-central1"
5656
_TEST_PROJECT = "test-project"
57+
_TEST_API_KEY = "test-api-key"
5758
_TEST_MODEL = "gemini-2.0-flash"
5859
_TEST_USER_ID = "test_user_id"
5960
_TEST_AGENT_NAME = "test_agent"
@@ -868,6 +869,41 @@ def test_dump_event_for_json():
868869
assert base64.b64decode(part["thought_signature"]) == raw_signature
869870

870871

872+
def test_adk_app_initialization_with_api_key():
873+
importlib.reload(initializer)
874+
importlib.reload(vertexai)
875+
try:
876+
vertexai.init(api_key=_TEST_API_KEY)
877+
app = agent_engines.AdkApp(agent=_TEST_AGENT)
878+
assert app._tmpl_attrs.get("project") is None
879+
assert app._tmpl_attrs.get("location") is None
880+
assert app._tmpl_attrs.get("express_mode_api_key") == _TEST_API_KEY
881+
assert app._tmpl_attrs.get("runner") is None
882+
app.set_up()
883+
assert app._tmpl_attrs.get("runner") is not None
884+
assert os.environ.get("GOOGLE_API_KEY") == _TEST_API_KEY
885+
assert "GOOGLE_CLOUD_LOCATION" not in os.environ
886+
assert "GOOGLE_CLOUD_PROJECT" not in os.environ
887+
finally:
888+
initializer.global_pool.shutdown(wait=True)
889+
890+
891+
def test_adk_app_initialization_with_env_api_key():
892+
try:
893+
os.environ["GOOGLE_API_KEY"] == _TEST_API_KEY
894+
app = agent_engines.AdkApp(agent=_TEST_AGENT)
895+
assert app._tmpl_attrs.get("project") is None
896+
assert app._tmpl_attrs.get("location") is None
897+
assert app._tmpl_attrs.get("express_mode_api_key") == _TEST_API_KEY
898+
assert app._tmpl_attrs.get("runner") is None
899+
app.set_up()
900+
assert app._tmpl_attrs.get("runner") is not None
901+
assert "GOOGLE_CLOUD_LOCATION" not in os.environ
902+
assert "GOOGLE_CLOUD_PROJECT" not in os.environ
903+
finally:
904+
initializer.global_pool.shutdown(wait=True)
905+
906+
871907
@pytest.mark.usefixtures("mock_adk_version")
872908
class TestAdkAppErrors:
873909
@pytest.mark.asyncio

vertexai/agent_engines/templates/adk.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)