Skip to content

Commit 00ec4b4

Browse files
markadelnawarpandego
authored andcommitted
fix: filter non-agent directoris from list_agents()
Merge google#4648 **Please ensure you have read the [contribution guide](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) before creating a pull request.** ### Link to Issue or Description of Change **1. Link to an existing issue (if applicable):** - Closes: google#4647 - Related: google#3429, google#3430 **2. Or, if no issue exists, describe the change:** **Problem:** `AgentLoader.list_agents()` returns every non-hidden subdirectory in the agents directory, regardless of whether it contains a valid agent definition. This causes non-agent directories (e.g. `tmp/`, `data/`, `utils/`) to appear in the `/list-apps` API response. This affects both the ADK web UI agent selector and any production deployment depending on this API. **Solution:** Reuse the existing `_determine_agent_language()` method inside `list_agents()` to verify each candidate directory contains at least one recognized agent file (`root_agent.yaml`, `agent.py`, or `__init__.py`). Directories that fail this check are excluded from the result. This avoids introducing any new methods or abstractions and keeps the check lightweight (filesystem only, no agent imports). ### Testing Plan **Unit Tests:** - [x] I have added or updated unit tests for my change. - [x] All unit tests pass locally. 27 passed in 2.85s: pytest tests/unittests/cli/utils/test_agent_loader.py -v ======================= 27 passed, 14 warnings in 2.85s ======================== Added `test_list_agents_excludes_non_agent_directories` which creates a temp directory with three valid agent types (package with `__init__.py`, module with `agent.py`, YAML with `root_agent.yaml`) and three non-agent directories, and asserts only the valid agents are listed. **Screenshots / Video:** | Before (non-agent directories listed) | After (only valid agents listed) | |----------------------------------------|----------------------------------| |<img width="566" height="553" alt="Image" src="https://github.com/user-attachments/assets/0f50084b-319f-480e-8d8a-051c28d4a7e7" />|<img width="567" height="532" alt="Image" src="https://github.com/user-attachments/assets/52d3543f-4c4c-4ff3-a6dd-7d5ce3f19bb2" />| **Manual End-to-End (E2E) Tests:** 1. Create a project directory containing both valid agent subdirectories and non-agent subdirectories 2. Run `adk web .` 3. Open the web UI and verify only valid agents appear in the agent selector 4. See screenshots below for before/after comparison ### Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [x] I have performed a self-review of my own code. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have added tests that prove my fix is effective or that my feature works. - [x] New and existing unit tests pass locally with my changes. - [x] I have manually tested my changes end-to-end. - [ ] Any dependent changes have been merged and published in downstream modules. ### Additional context COPYBARA_INTEGRATE_REVIEW=google#4648 from markadelnawar:fix/list-agents-filter-non-agents-dirs 0418956 PiperOrigin-RevId: 878674609
1 parent 11a417c commit 00ec4b4

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

src/google/adk/cli/utils/agent_loader.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,18 @@ def load_agent(self, agent_name: str) -> Union[BaseAgent, App]:
335335
def list_agents(self) -> list[str]:
336336
"""Lists all agents available in the agent loader (sorted alphabetically)."""
337337
base_path = Path.cwd() / self.agents_dir
338-
agent_names = [
339-
x
340-
for x in os.listdir(base_path)
341-
if os.path.isdir(os.path.join(base_path, x))
342-
and not x.startswith(".")
343-
and x != "__pycache__"
344-
]
338+
agent_names = []
339+
for x in os.listdir(base_path):
340+
if (
341+
os.path.isdir(os.path.join(base_path, x))
342+
and not x.startswith(".")
343+
and x != "__pycache__"
344+
):
345+
try:
346+
self._determine_agent_language(x)
347+
agent_names.append(x)
348+
except ValueError:
349+
continue
345350
agent_names.sort()
346351
return agent_names
347352

tests/unittests/cli/utils/test_agent_loader.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,48 @@ def __init__(self):
993993
assert len(detailed_list) == 1
994994
assert detailed_list[0]["name"] == agent_name
995995
assert not detailed_list[0]["is_computer_use"]
996+
997+
def test_list_agents_excludes_non_agent_directories(self):
998+
"""Test that list_agents filters out directories without agent definitions."""
999+
with tempfile.TemporaryDirectory() as temp_dir:
1000+
temp_path = Path(temp_dir)
1001+
1002+
valid_package = temp_path / "valid_agent"
1003+
valid_package.mkdir()
1004+
(valid_package / "__init__.py").write_text(dedent("""
1005+
from google.adk.agents.base_agent import BaseAgent
1006+
1007+
class ValidAgent(BaseAgent):
1008+
def __init__(self):
1009+
super().__init__(name="valid_agent")
1010+
1011+
root_agent = ValidAgent()
1012+
"""))
1013+
1014+
valid_module = temp_path / "module_agent"
1015+
valid_module.mkdir()
1016+
(valid_module / "agent.py").write_text(dedent("""
1017+
from google.adk.agents.base_agent import BaseAgent
1018+
1019+
class ModuleAgent(BaseAgent):
1020+
def __init__(self):
1021+
super().__init__(name="module_agent")
1022+
1023+
root_agent = ModuleAgent()
1024+
"""))
1025+
1026+
valid_yaml = temp_path / "yaml_agent"
1027+
valid_yaml.mkdir()
1028+
(valid_yaml / "root_agent.yaml").write_text("name: yaml_agent\n")
1029+
1030+
(temp_path / "random_folder").mkdir()
1031+
(temp_path / "data").mkdir()
1032+
(temp_path / "tmp").mkdir()
1033+
1034+
loader = AgentLoader(str(temp_path))
1035+
agents = loader.list_agents()
1036+
1037+
assert agents == ["module_agent", "valid_agent", "yaml_agent"]
1038+
assert "random_folder" not in agents
1039+
assert "data" not in agents
1040+
assert "tmp" not in agents

0 commit comments

Comments
 (0)