Skip to content

Commit af5abc0

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add support for Vertex Express Mode API key in AdkApp
PiperOrigin-RevId: 825638989
1 parent e600277 commit af5abc0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

tests/unit/vertex_adk/test_agent_engine_templates_adk.py

Lines changed: 20 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"
@@ -761,6 +762,25 @@ def test_dump_event_for_json():
761762
assert base64.b64decode(part["thought_signature"]) == raw_signature
762763

763764

765+
def test_adk_app_initialization_with_api_key():
766+
importlib.reload(initializer)
767+
importlib.reload(vertexai)
768+
try:
769+
vertexai.init(api_key=_TEST_API_KEY)
770+
app = agent_engines.AdkApp(agent=_TEST_AGENT)
771+
assert app._tmpl_attrs.get("project") is None
772+
assert app._tmpl_attrs.get("location") is None
773+
assert app._tmpl_attrs.get("express_mode_api_key") == _TEST_API_KEY
774+
assert app._tmpl_attrs.get("runner") is None
775+
app.set_up()
776+
assert app._tmpl_attrs.get("runner") is not None
777+
assert os.environ.get("GOOGLE_API_KEY") == _TEST_API_KEY
778+
assert "GOOGLE_CLOUD_LOCATION" not in os.environ
779+
assert "GOOGLE_CLOUD_PROJECT" not in os.environ
780+
finally:
781+
initializer.global_pool.shutdown(wait=True)
782+
783+
764784
@pytest.mark.usefixtures("mock_adk_version")
765785
class TestAdkAppErrors:
766786
@pytest.mark.asyncio

vertexai/agent_engines/templates/adk.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ def __init__(
521521
If not provided, a default instrumentor builder will be used.
522522
This parameter is ignored if `enable_tracing` is False.
523523
"""
524+
import os
524525
from google.cloud.aiplatform import initializer
525526

526527
adk_version = get_adk_version()
@@ -558,6 +559,7 @@ def __init__(
558559
"artifact_service_builder": artifact_service_builder,
559560
"memory_service_builder": memory_service_builder,
560561
"instrumentor_builder": instrumentor_builder,
562+
"express_mode_api_key": initializer.global_config.api_key,
561563
}
562564

563565
async def _init_session(
@@ -701,9 +703,18 @@ def set_up(self):
701703

702704
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "1"
703705
project = self._tmpl_attrs.get("project")
704-
os.environ["GOOGLE_CLOUD_PROJECT"] = project
706+
if project:
707+
os.environ["GOOGLE_CLOUD_PROJECT"] = project
705708
location = self._tmpl_attrs.get("location")
706-
os.environ["GOOGLE_CLOUD_LOCATION"] = location
709+
if location:
710+
os.environ["GOOGLE_CLOUD_LOCATION"] = location
711+
express_mode_api_key = self._tmpl_attrs.get("express_mode_api_key")
712+
if express_mode_api_key and not project:
713+
os.environ["GOOGLE_API_KEY"] = express_mode_api_key
714+
# Clear location and project env vars if express mode api key is provided.
715+
os.environ.pop("GOOGLE_CLOUD_LOCATION", None)
716+
os.environ.pop("GOOGLE_CLOUD_PROJECT", None)
717+
location = None
707718

708719
# Disable content capture in custom ADK spans unless user enabled
709720
# tracing explicitly with the old flag
@@ -750,6 +761,8 @@ def set_up(self):
750761
VertexAiSessionService,
751762
)
752763

764+
# If the express mode api key is set, it will be read from the
765+
# environment variable when initializing the session service.
753766
self._tmpl_attrs["session_service"] = VertexAiSessionService(
754767
project=project,
755768
location=location,
@@ -760,6 +773,8 @@ def set_up(self):
760773
VertexAiSessionService,
761774
)
762775

776+
# If the express mode api key is set, it will be read from the
777+
# environment variable when initializing the session service.
763778
self._tmpl_attrs["session_service"] = VertexAiSessionService(
764779
project=project,
765780
location=location,
@@ -780,6 +795,8 @@ def set_up(self):
780795
VertexAiMemoryBankService,
781796
)
782797

798+
# If the express mode api key is set, it will be read from the
799+
# environment variable when initializing the memory service.
783800
self._tmpl_attrs["memory_service"] = VertexAiMemoryBankService(
784801
project=project,
785802
location=location,

0 commit comments

Comments
 (0)