Repository navigation
fix(cli): validate Python identifier for code-based agent creation - #7496
philipp-horstenkamp wants to merge 3 commits into
Conversation
Code-based agents created via adk create require dynamic module imports (e.g. importlib.import_module), which fail with ModuleNotFoundError when given kebab-case names or Python keywords. Add validation in cli_create.run_cmd ensuring code-based agent names are valid Python identifiers and not reserved keywords, aborting before any interactive dialogs. Leaves validate_app_name and config-based YAML agents unchanged to avoid breaking changes.
BichengWang
left a comment
There was a problem hiding this comment.
[P2] Preserve creation of keyword-named applications
keyword.iskeyword(app_name) rejects application folder names that the current loader supports. At base 3c31fa5100a617d03a19f8a9495b8b93d2e47628, the real CLI command adk create class --type code --model gemini-2.5-flash --api_key offline-placeholder generates an application that AgentLoader(<parent>).load_agent('class') successfully loads as an LlmAgent named root_agent; import works too. At this head, both create commands exit 2 before generating files, while the head loader still successfully loads those base-generated applications. Dynamic imports accept keyword strings, and the generated agent's name remains root_agent, so this introduces a compatibility restriction on working local applications. Please remove the keyword check while retaining isidentifier() for hyphens, and cover creation followed by real loading for a keyword folder. Verified offline with fake credentials and a socket/DNS/subprocess guard; model inference and deployment were not tested.
…tifiers Remove keyword.iskeyword check in cli_create to allow applications named after Python keywords, which AgentLoader and dynamic imports support. Retain isidentifier check to reject hyphenated kebab-case folder names.
|
@BichengWang i remove the keyword part. Sorry for it. That comes from listening to AI and not just doing what the original plan was. |
Code-based agents created via adk create require dynamic module imports (e.g. importlib.import_module), which fail with ModuleNotFoundError when given kebab-case names or Python keywords.
Add validation in cli_create.run_cmd ensuring code-based agent names are valid Python identifiers and not reserved keywords, aborting before any interactive dialogs. Leaves validate_app_name and config-based YAML agents unchanged to avoid breaking changes.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
N/A (no existing issue filed yet)
2. Or, if no issue exists, describe the change:
Problem:
When running
adk create my-agent(which defaults to--type=code), the CLI accepts the agent name without warning and generates the directorymy-agent/withagent.pyand__init__.py.However, because hyphens (
-) are invalid in Python identifiers, subsequent commands likeadk run my-agentoradk deployattempt dynamic module imports viaimportlib.import_module(f"{agent_name}.agent")and fail immediately:Currently,
adk createrelies exclusively onvalidate_app_namefromgoogle.adk.apps.app, which permits hyphens (_VALID_APP_NAME_RE = re.compile(r"^[a-zA-Z][a-zA-Z0-9_-]*$")).Solution:
Rather than modifying
validate_app_name(which would be a breaking change for existing applications using hyphens inApp(name="...")session keys, artifact storage, and telemetry), this PR scopes Python identifier validation to code-based agent creation incli_create.py:Invalid agent name '<name>': code-based agents must be a valid Python identifier (e.g. 'my_agent', not 'my-agent').snake_case,PascalCase, andcamelCase).root_agent.yaml) are loaded by filesystem path rather thanimportlib, so kebab-case folder names remain supported for--type=config.Testing Plan
Unit Tests:
Summary of passed
pytestresults:Added tests in
tests/unittests/cli/utils/test_cli_create.py:test_run_cmd_rejects_non_identifier_or_keyword_agent_name: Verifies rejection of kebab-case and reserved keywords, and confirms that interactive prompts (e.g._prompt_for_model) are never reached.test_run_cmd_accepts_valid_identifier_names: Verifies thatsnake_case,PascalCase, andcamelCasesucceed and createagent.py.test_run_cmd_allows_kebab_case_for_config_agents: Verifies that--type=configcontinues to support kebab-case and generatesroot_agent.yamlwith expected contents.test_run_cmd_rejects_kebab_case_when_prompted_as_code: Verifies that choosingCODEin the interactive prompt still triggers validation.Manual End-to-End (E2E) Tests:
adk create my-agent:Error: Invalid agent name 'my-agent': code-based agents must be a valid Python identifier (e.g. 'my_agent', not 'my-agent') and cannot be a Python keyword.adk create my-agent --type=config:my-agent/root_agent.yaml.adk create my_agent:my_agent/agent.py.Checklist
Additional context
This PR is intentionally designed as a zero-breaking hotfix. If maintainers prefer to unify application naming rules framework-wide or enforce strict lowercase
snake_case(PEP 8) for all agents, that broader design discussion should follow on maintainer level.