Skip to content

Commit 95921e5

Browse files
committed
fix: filter non-agent directoris from list_agents()
1 parent dff4c44 commit 95921e5

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 sorted(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)