Skip to content

Commit a087253

Browse files
committed
fix(agentplatform): resolve A2aAgent location from environment variables
1 parent 20471f3 commit a087253

5 files changed

Lines changed: 231 additions & 6 deletions

File tree

agentplatform/agent_engines/templates/a2a.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,16 @@ def set_up(self):
320320
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "1"
321321
project = self._tmpl_attrs.get("project")
322322
os.environ["GOOGLE_CLOUD_PROJECT"] = project
323-
location = self._tmpl_attrs.get("location")
324-
os.environ["GOOGLE_CLOUD_LOCATION"] = location
323+
location = (
324+
os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION")
325+
or os.getenv("GOOGLE_CLOUD_LOCATION")
326+
or self._tmpl_attrs.get("location")
327+
)
328+
if location:
329+
if "GOOGLE_CLOUD_AGENT_ENGINE_LOCATION" not in os.environ:
330+
os.environ["GOOGLE_CLOUD_AGENT_ENGINE_LOCATION"] = location
331+
if "GOOGLE_CLOUD_LOCATION" not in os.environ:
332+
os.environ["GOOGLE_CLOUD_LOCATION"] = location
325333
agent_engine_id = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", "test-agent-engine")
326334
version = "v1beta1"
327335

noxfile.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,37 @@ def unit_agentplatform_adk(session):
328328
)
329329

330330

331+
@nox.session(python=UNIT_TEST_TEMPLATES_PYTHON_VERSIONS)
332+
def unit_agentplatform_a2a(session):
333+
# Install all test dependencies, then install this package in-place.
334+
335+
constraints_path = str(CURRENT_DIRECTORY / "testing" / "constraints-adk.txt")
336+
standard_deps = UNIT_TEST_STANDARD_DEPENDENCIES + UNIT_TEST_DEPENDENCIES
337+
session.install(*standard_deps, "-c", constraints_path)
338+
339+
# Install adk extras (A2A uses the ADK runtime base)
340+
session.install("-e", ".[adk_testing]", "-c", constraints_path)
341+
342+
# Install A2A-specific testing dependencies
343+
session.install("pandas", "a2a-sdk", "sse-starlette")
344+
345+
# Run py.test against the A2A unit tests.
346+
session.run(
347+
"py.test",
348+
"--quiet",
349+
"--junitxml=unit_agentplatform_a2a_sponge_log.xml",
350+
"--cov=google",
351+
"--cov-append",
352+
"--cov-config=.coveragerc",
353+
"--cov-report=",
354+
"--cov-fail-under=0",
355+
os.path.join(
356+
"tests", "unit", "agentplatform", "frameworks", "test_frameworks_a2a.py"
357+
),
358+
*session.posargs,
359+
)
360+
361+
331362
@nox.session(python=UNIT_TEST_TEMPLATES_PYTHON_VERSIONS)
332363
def unit_agentplatform_langchain(session):
333364
# Install all test dependencies, then install this package in-place.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import importlib
16+
import os
17+
from unittest import mock
18+
19+
import pytest
20+
import vertexai
21+
from google.cloud.aiplatform import initializer
22+
23+
# Skip A2A tests if a2a-sdk is not installed
24+
pytest.importorskip(
25+
"a2a.types", reason="a2a-sdk not installed, skipping A2A Agent tests"
26+
)
27+
28+
from vertexai.preview.reasoning_engines.templates.a2a import A2aAgent as PreviewA2aAgent
29+
from vertexai.preview.reasoning_engines.templates.a2a import create_agent_card as preview_create_card
30+
31+
from vertexai.agent_engines.templates.a2a import A2aAgent as VertexA2aAgent
32+
from vertexai.agent_engines.templates.a2a import create_agent_card as vertex_create_card
33+
34+
from agentplatform.agent_engines.templates.a2a import A2aAgent as PlatformA2aAgent
35+
from agentplatform.agent_engines.templates.a2a import create_agent_card as platform_create_card
36+
37+
38+
_TEST_LOCATION = "us-central1"
39+
_TEST_PROJECT = "test-project"
40+
41+
42+
@pytest.fixture
43+
def preview_agent() -> "PreviewA2aAgent":
44+
from a2a.types import AgentSkill
45+
skill = AgentSkill(
46+
id="hello_world",
47+
name="Returns hello world",
48+
description="just returns hello world",
49+
)
50+
try:
51+
card = preview_create_card(agent_name="Test", description="Test", skills=[skill])
52+
except ImportError as e:
53+
pytest.skip(f"Legacy preview A2A template is not compatible with the installed a2a-sdk version: {e}")
54+
return PreviewA2aAgent(agent_card=card)
55+
56+
57+
@pytest.fixture
58+
def vertex_agent() -> "VertexA2aAgent":
59+
from a2a.types import AgentSkill
60+
skill = AgentSkill(
61+
id="hello_world",
62+
name="Returns hello world",
63+
description="just returns hello world",
64+
)
65+
card = vertex_create_card(agent_name="Test", description="Test", skills=[skill])
66+
return VertexA2aAgent(agent_card=card)
67+
68+
69+
@pytest.fixture
70+
def platform_agent() -> "PlatformA2aAgent":
71+
from a2a.types import AgentSkill
72+
skill = AgentSkill(
73+
id="hello_world",
74+
name="Returns hello world",
75+
description="just returns hello world",
76+
)
77+
card = platform_create_card(agent_name="Test", description="Test", skills=[skill])
78+
return PlatformA2aAgent(agent_card=card)
79+
80+
81+
class TestA2aLocationResolution:
82+
83+
def setup_method(self):
84+
importlib.reload(initializer)
85+
importlib.reload(vertexai)
86+
vertexai.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
87+
88+
def teardown_method(self):
89+
initializer.global_pool.shutdown(wait=True)
90+
91+
@pytest.mark.parametrize(
92+
"agent_fixture",
93+
["preview_agent", "vertex_agent", "platform_agent"],
94+
)
95+
def test_default_location_from_global_config(self, agent_fixture, request):
96+
agent = request.getfixturevalue(agent_fixture)
97+
with mock.patch.dict(os.environ, {}, clear=True):
98+
agent.set_up()
99+
assert os.environ.get("GOOGLE_CLOUD_LOCATION") == _TEST_LOCATION
100+
101+
# Check URL in agent card
102+
if hasattr(agent.agent_card, "url"):
103+
url = agent.agent_card.url
104+
else:
105+
url = agent.agent_card.supported_interfaces[0].url
106+
assert _TEST_LOCATION in url
107+
108+
@pytest.mark.parametrize(
109+
"agent_fixture",
110+
["preview_agent", "vertex_agent", "platform_agent"],
111+
)
112+
def test_location_from_agent_engine_env_var(self, agent_fixture, request):
113+
agent = request.getfixturevalue(agent_fixture)
114+
with mock.patch.dict(
115+
os.environ,
116+
{"GOOGLE_CLOUD_AGENT_ENGINE_LOCATION": "us-east1"},
117+
clear=True,
118+
):
119+
agent.set_up()
120+
assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-east1"
121+
122+
if hasattr(agent.agent_card, "url"):
123+
url = agent.agent_card.url
124+
else:
125+
url = agent.agent_card.supported_interfaces[0].url
126+
assert "us-east1" in url
127+
128+
@pytest.mark.parametrize(
129+
"agent_fixture",
130+
["preview_agent", "vertex_agent", "platform_agent"],
131+
)
132+
def test_location_from_cloud_location_env_var(self, agent_fixture, request):
133+
agent = request.getfixturevalue(agent_fixture)
134+
with mock.patch.dict(
135+
os.environ,
136+
{"GOOGLE_CLOUD_LOCATION": "us-west1"},
137+
clear=True,
138+
):
139+
agent.set_up()
140+
assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-west1"
141+
142+
if hasattr(agent.agent_card, "url"):
143+
url = agent.agent_card.url
144+
else:
145+
url = agent.agent_card.supported_interfaces[0].url
146+
assert "us-west1" in url
147+
148+
@pytest.mark.parametrize(
149+
"agent_fixture",
150+
["preview_agent", "vertex_agent", "platform_agent"],
151+
)
152+
def test_location_env_var_precedence(self, agent_fixture, request):
153+
agent = request.getfixturevalue(agent_fixture)
154+
with mock.patch.dict(
155+
os.environ,
156+
{
157+
"GOOGLE_CLOUD_AGENT_ENGINE_LOCATION": "us-east1",
158+
"GOOGLE_CLOUD_LOCATION": "us-west1",
159+
},
160+
clear=True,
161+
):
162+
agent.set_up()
163+
# Should not overwrite existing GOOGLE_CLOUD_LOCATION
164+
assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-west1"
165+
166+
if hasattr(agent.agent_card, "url"):
167+
url = agent.agent_card.url
168+
else:
169+
url = agent.agent_card.supported_interfaces[0].url
170+
assert "us-east1" in url

vertexai/agent_engines/templates/a2a.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,16 @@ def set_up(self):
320320
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "1"
321321
project = self._tmpl_attrs.get("project")
322322
os.environ["GOOGLE_CLOUD_PROJECT"] = project
323-
location = self._tmpl_attrs.get("location")
324-
os.environ["GOOGLE_CLOUD_LOCATION"] = location
323+
location = (
324+
os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION")
325+
or os.getenv("GOOGLE_CLOUD_LOCATION")
326+
or self._tmpl_attrs.get("location")
327+
)
328+
if location:
329+
if "GOOGLE_CLOUD_AGENT_ENGINE_LOCATION" not in os.environ:
330+
os.environ["GOOGLE_CLOUD_AGENT_ENGINE_LOCATION"] = location
331+
if "GOOGLE_CLOUD_LOCATION" not in os.environ:
332+
os.environ["GOOGLE_CLOUD_LOCATION"] = location
325333
agent_engine_id = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", "test-agent-engine")
326334
version = "v1beta1"
327335

vertexai/preview/reasoning_engines/templates/a2a.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,16 @@ def set_up(self):
241241
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "1"
242242
project = self._tmpl_attrs.get("project")
243243
os.environ["GOOGLE_CLOUD_PROJECT"] = project
244-
location = self._tmpl_attrs.get("location")
245-
os.environ["GOOGLE_CLOUD_LOCATION"] = location
244+
location = (
245+
os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION")
246+
or os.getenv("GOOGLE_CLOUD_LOCATION")
247+
or self._tmpl_attrs.get("location")
248+
)
249+
if location:
250+
if "GOOGLE_CLOUD_AGENT_ENGINE_LOCATION" not in os.environ:
251+
os.environ["GOOGLE_CLOUD_AGENT_ENGINE_LOCATION"] = location
252+
if "GOOGLE_CLOUD_LOCATION" not in os.environ:
253+
os.environ["GOOGLE_CLOUD_LOCATION"] = location
246254
agent_engine_id = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", "test-agent-engine")
247255
version = "v1beta1"
248256

0 commit comments

Comments
 (0)