Skip to content

Commit a1a6dee

Browse files
committed
fix(deploy): add --agent_module flag to adk deploy agent_engine
The generated agent_engine_app.py hardcoded `from .agent import ...`, breaking deployments where the entry point is not named agent.py (e.g. core.py, adk_agent.py). Fixes #5192. Changes: - Add `agent_module` param to `to_agent_engine()` (default: 'agent') - Add `--agent_module` CLI option to `adk deploy agent_engine` - Pass `agent_module` through to the app template and import validation - Update error message in `_validate_agent_import` to reference the configurable module name and suggest --agent_module on failure Built by Rudrendu Paul, developed with Claude Code
1 parent dcc485b commit a1a6dee

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/google/adk/cli/cli_deploy.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
112112
config_path = os.path.join(os.path.dirname(__file__), "root_agent.yaml")
113113
root_agent = config_agent_utils.from_config(config_path)
114114
else:
115-
from .agent import {adk_app_object}
115+
from .{agent_module} import {adk_app_object}
116116
117117
if {express_mode}: # Whether or not to use Express Mode
118118
vertexai.init(api_key=os.environ.get("GOOGLE_API_KEY"))
@@ -473,6 +473,7 @@ def _validate_agent_import(
473473
agent_src_path: str,
474474
adk_app_object: str,
475475
is_config_agent: bool,
476+
agent_module: str = 'agent',
476477
) -> None:
477478
"""Validates that the agent module can be imported successfully.
478479
@@ -485,6 +486,8 @@ def _validate_agent_import(
485486
agent_src_path: Path to the staged agent source code.
486487
adk_app_object: The Python object name to import ('root_agent' or 'app').
487488
is_config_agent: Whether this is a config-based agent.
489+
agent_module: The Python module name containing the agent object.
490+
Defaults to 'agent'.
488491
489492
Raises:
490493
click.ClickException: If the agent module cannot be imported.
@@ -493,11 +496,12 @@ def _validate_agent_import(
493496
# Config agents are loaded from YAML, skip Python import validation
494497
return
495498

496-
agent_module_path = os.path.join(agent_src_path, 'agent.py')
499+
agent_module_path = os.path.join(agent_src_path, f'{agent_module}.py')
497500
if not os.path.exists(agent_module_path):
498501
raise click.ClickException(
499502
f'Agent module not found at {agent_module_path}. '
500-
'Please ensure your agent folder contains an agent.py file.'
503+
f'Please ensure your agent folder contains a {agent_module}.py file,'
504+
' or use --agent_module to specify a different module name.'
501505
)
502506

503507
# Add the parent directory to sys.path temporarily for import resolution
@@ -818,6 +822,7 @@ def to_agent_engine(
818822
otel_to_cloud: Optional[bool] = None,
819823
api_key: Optional[str] = None,
820824
adk_app_object: Optional[str] = None,
825+
agent_module: Optional[str] = None,
821826
agent_engine_id: Optional[str] = None,
822827
absolutize_imports: bool = True,
823828
project: Optional[str] = None,
@@ -868,6 +873,9 @@ def to_agent_engine(
868873
will be used. It will only be used if GOOGLE_GENAI_USE_VERTEXAI is true.
869874
adk_app_object (str): Optional. The Python object corresponding to the root
870875
ADK agent or app. Defaults to `root_agent` if not specified.
876+
agent_module (str): Optional. The Python module name (without .py) that
877+
contains the agent object. Defaults to `agent`. Use this when your entry
878+
point is not named `agent.py` (e.g., `core.py` or `adk_agent.py`).
871879
agent_engine_id (str): Optional. The ID of the Agent Engine instance to
872880
update. If not specified, a new Agent Engine instance will be created.
873881
absolutize_imports (bool): Optional. Default is True. Whether to absolutize
@@ -900,6 +908,7 @@ def to_agent_engine(
900908
display_name = display_name or app_name
901909
parent_folder = os.path.dirname(agent_folder)
902910
adk_app_object = adk_app_object or 'root_agent'
911+
agent_module = agent_module or 'agent'
903912
if adk_app_object not in ['root_agent', 'app']:
904913
click.echo(
905914
f'Invalid adk_app_object: {adk_app_object}. Please use "root_agent"'
@@ -1105,7 +1114,9 @@ def to_agent_engine(
11051114
# Validate that the agent module can be imported before deployment.
11061115
if not skip_agent_import_validation:
11071116
click.echo('Validating agent module...')
1108-
_validate_agent_import(agent_src_path, adk_app_object, is_config_agent)
1117+
_validate_agent_import(
1118+
agent_src_path, adk_app_object, is_config_agent, agent_module
1119+
)
11091120

11101121
adk_app_file = os.path.join(temp_folder, f'{adk_app}.py')
11111122
if adk_app_object == 'root_agent':
@@ -1126,6 +1137,7 @@ def to_agent_engine(
11261137
is_config_agent=is_config_agent,
11271138
agent_folder=f'./{temp_folder}',
11281139
adk_app_object=adk_app_object,
1140+
agent_module=agent_module,
11291141
adk_app_type=adk_app_type,
11301142
express_mode=api_key is not None,
11311143
)

src/google/adk/cli/cli_tools_click.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,16 @@ def cli_migrate_session(
21972197
" It can only be `root_agent` or `app`. (default: `root_agent`)"
21982198
),
21992199
)
2200+
@click.option(
2201+
"--agent_module",
2202+
type=str,
2203+
default=None,
2204+
help=(
2205+
"Optional. Python module name (without .py) containing the agent"
2206+
" object. Use this when your entry point is not named `agent.py`"
2207+
" (e.g. `core` or `adk_agent`). (default: `agent`)"
2208+
),
2209+
)
22002210
@click.option(
22012211
"--env_file",
22022212
type=str,
@@ -2271,6 +2281,7 @@ def cli_deploy_agent_engine(
22712281
description: str,
22722282
adk_app: str,
22732283
adk_app_object: Optional[str],
2284+
agent_module: Optional[str],
22742285
temp_folder: Optional[str],
22752286
env_file: str,
22762287
requirements_file: str,
@@ -2308,6 +2319,7 @@ def cli_deploy_agent_engine(
23082319
otel_to_cloud=otel_to_cloud,
23092320
api_key=api_key,
23102321
adk_app_object=adk_app_object,
2322+
agent_module=agent_module,
23112323
display_name=display_name,
23122324
description=description,
23132325
adk_app=adk_app,

0 commit comments

Comments
 (0)