Skip to content

Commit ea772b9

Browse files
adk-botharanrk
authored andcommitted
fix(cli): Ensure ADK compatibility in agent engine requirements
Use a Final constant (`_AGENT_ENGINE_REQUIREMENT`) containing the `adk` extra for the `google-cloud-aiplatform` package in the generated requirements.txt instead of hardcoding the dependency without it. This ensures all required ADK templates and classes are available in the deployed Vertex AI Agent Engine, preventing `ModuleNotFoundError: No module named 'vertexai.agent_engines.templates.adk'` at runtime. Fixes #5966 Co-authored-by: Haran Rajkumar <haranrk@google.com> PiperOrigin-RevId: 933989073
1 parent f36d257 commit ea772b9

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/google/adk/cli/cli_deploy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
_IS_WINDOWS = os.name == 'nt'
3636
_GCLOUD_CMD = 'gcloud.cmd' if _IS_WINDOWS else 'gcloud'
3737
_LOCAL_STORAGE_FLAG_MIN_VERSION: Final[str] = '1.21.0'
38+
_AGENT_ENGINE_REQUIREMENT: Final[str] = (
39+
'google-cloud-aiplatform[adk,agent_engines]'
40+
)
3841

3942

4043
def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
@@ -60,7 +63,7 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
6063
with open(requirements_txt_path, 'a', encoding='utf-8') as f:
6164
if requirements and not requirements.endswith('\n'):
6265
f.write('\n')
63-
f.write('google-cloud-aiplatform[agent_engines]\n')
66+
f.write(f'{_AGENT_ENGINE_REQUIREMENT}\n')
6467
f.write(f'google-adk[a2a]=={__version__}\n')
6568

6669

@@ -1013,7 +1016,7 @@ def to_agent_engine(
10131016
if not os.path.exists(requirements_txt_path):
10141017
click.echo(f'Creating {requirements_txt_path}...')
10151018
with open(requirements_txt_path, 'w', encoding='utf-8') as f:
1016-
f.write('google-cloud-aiplatform[agent_engines]\n')
1019+
f.write(f'{_AGENT_ENGINE_REQUIREMENT}\n')
10171020
f.write(f'google-adk[a2a]=={__version__}\n')
10181021
click.echo(f'Using google-adk[a2a]=={__version__} in requirements')
10191022
click.echo(f'Created {requirements_txt_path}')

tests/unittests/cli/utils/test_cli_deploy.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
302302
assert len(create_recorder.calls) == 1
303303
assert str(rmtree_recorder.get_last_call_args()[0]) == str(tmp_dir)
304304

305+
requirements_file = tmp_dir / "agents" / "agent" / "requirements.txt"
306+
assert requirements_file.is_file()
307+
assert (
308+
"google-cloud-aiplatform[adk,agent_engines]"
309+
in requirements_file.read_text()
310+
)
311+
305312

306313
def test_to_agent_engine_raises_when_explicit_config_file_missing(
307314
monkeypatch: pytest.MonkeyPatch,
@@ -682,3 +689,26 @@ def test_cli_deploy_agent_engine_artifact_service_uri(tmp_path: Path):
682689
mock_to_agent_engine.assert_called_once()
683690
_, kwargs = mock_to_agent_engine.call_args
684691
assert kwargs["artifact_service_uri"] == "gs://my-bucket"
692+
693+
694+
def test_ensure_agent_engine_dependency(tmp_path: Path):
695+
"""Tests that _ensure_agent_engine_dependency appends correct extras."""
696+
requirements_file = tmp_path / "requirements.txt"
697+
698+
# Case 1: raises FileNotFoundError when the file doesn't exist
699+
with pytest.raises(FileNotFoundError):
700+
cli_deploy._ensure_agent_engine_dependency(str(requirements_file))
701+
702+
# Case 2: appends google-cloud-aiplatform with 'adk' and 'agent_engines'
703+
# extras and the versioned google-adk requirement.
704+
requirements_file.write_text("")
705+
cli_deploy._ensure_agent_engine_dependency(str(requirements_file))
706+
content = requirements_file.read_text()
707+
assert "google-cloud-aiplatform[adk,agent_engines]\n" in content
708+
assert f"google-adk[a2a]=={cli_deploy.__version__}\n" in content
709+
710+
# Case 3: does not append duplicate if google-cloud-aiplatform already exists
711+
requirements_file.write_text("google-cloud-aiplatform[adk,agent_engines]\n")
712+
cli_deploy._ensure_agent_engine_dependency(str(requirements_file))
713+
content = requirements_file.read_text()
714+
assert content == "google-cloud-aiplatform[adk,agent_engines]\n"

0 commit comments

Comments
 (0)