Skip to content

Commit 0527777

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 0527777

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ def __init__(
558558
"artifact_service_builder": artifact_service_builder,
559559
"memory_service_builder": memory_service_builder,
560560
"instrumentor_builder": instrumentor_builder,
561+
"express_mode_api_key": initializer.global_config.api_key,
561562
}
562563

563564
async def _init_session(
@@ -701,9 +702,18 @@ def set_up(self):
701702

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

708718
# Disable content capture in custom ADK spans unless user enabled
709719
# tracing explicitly with the old flag
@@ -750,6 +760,8 @@ def set_up(self):
750760
VertexAiSessionService,
751761
)
752762

763+
# If the express mode api key is set, it will be read from the
764+
# environment variable when initializing the session service.
753765
self._tmpl_attrs["session_service"] = VertexAiSessionService(
754766
project=project,
755767
location=location,
@@ -760,6 +772,8 @@ def set_up(self):
760772
VertexAiSessionService,
761773
)
762774

775+
# If the express mode api key is set, it will be read from the
776+
# environment variable when initializing the session service.
763777
self._tmpl_attrs["session_service"] = VertexAiSessionService(
764778
project=project,
765779
location=location,
@@ -780,6 +794,8 @@ def set_up(self):
780794
VertexAiMemoryBankService,
781795
)
782796

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

0 commit comments

Comments
 (0)