Skip to content

Commit 4a842c5

Browse files
jpantsjohacopybara-github
authored andcommitted
fix(cli): Improve error message when adk web is run in wrong directory
Merge #3196 ## Summary Enhances the `AgentLoader` error message to provide clear guidance when users run `adk web` from incorrect directories. ## Motivation During internal workshops, multiple teams encountered confusion when starting `adk web` from the wrong directory. This often happened when: - Running `adk web my_agent/` instead of `adk web .` - Being inside an agent directory when executing the command - Configuring incorrect start paths during development ## Changes - **Smart detection**: Checks if `agent.py` or `root_agent.yaml` exists in the current directory - **Visual diagram**: Shows expected directory structure with actual agent name - **Explicit command**: Includes `adk web <agents_dir>` usage example - **Conditional hint**: Provides targeted guidance when user is detected to be inside an agent directory ## Example Error Message ### Before ``` ValueError: No root_agent found for 'my_agent'. Searched in 'my_agent.agent.root_agent', 'my_agent.root_agent' and 'my_agent/root_agent.yaml'. Ensure 'path/my_agent' is structured correctly, an .env file can be loaded if present, and a root_agent is exposed. ``` ### After ``` ValueError: No root_agent found for 'my_agent'. Searched in 'my_agent.agent.root_agent', 'my_agent.root_agent' and 'my_agent/root_agent.yaml'. Expected directory structure: <agents_dir>/ my_agent/ agent.py (with root_agent) OR root_agent.yaml Then run: adk web <agents_dir> Ensure 'path/my_agent' is structured correctly, an .env file can be loaded if present, and a root_agent is exposed. HINT: It looks like you might be running 'adk web' from inside an agent directory. Try running 'adk web .' from the parent directory that contains your agent folder, not from within the agent folder itself. ``` ## Testing - ✅ Existing unit tests pass (17/22, with 5 pre-existing failures unrelated to this change) - ✅ `test_agent_not_found_error` passes, confirming error message enhancement works correctly - ✅ Code follows ADK contribution guidelines ## Type - [x] Bug fix (improved error messaging) - [ ] Feature - [ ] Breaking change - [ ] Documentation ## Related Fixes #3195 --- **Tags**: #non-breaking 🤖 Generated with [Claude Code](https://claude.com/claude-code) COPYBARA_INTEGRATE_REVIEW=#3196 from jpantsjoha:fix/improve-adk-web-error-message a73b190 PiperOrigin-RevId: 822186700
1 parent 0bdba30 commit 4a842c5

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,30 @@ def _perform_load(self, agent_name: str) -> Union[BaseAgent, App]:
224224
return root_agent
225225

226226
# If no root_agent was found by any pattern
227+
# Check if user might be in the wrong directory
228+
hint = ""
229+
agents_path = Path(agents_dir)
230+
if (
231+
agents_path.joinpath("agent.py").is_file()
232+
or agents_path.joinpath("root_agent.yaml").is_file()
233+
):
234+
hint = (
235+
"\n\nHINT: It looks like you might be running 'adk web' from inside"
236+
" an agent directory. Try running 'adk web .' from the parent"
237+
" directory that contains your agent folder, not from within the"
238+
" agent folder itself."
239+
)
240+
227241
raise ValueError(
228242
f"No root_agent found for '{agent_name}'. Searched in"
229243
f" '{actual_agent_name}.agent.root_agent',"
230244
f" '{actual_agent_name}.root_agent' and"
231-
f" '{actual_agent_name}/root_agent.yaml'. Ensure"
232-
f" '{agents_dir}/{actual_agent_name}' is structured correctly, an .env"
233-
" file can be loaded if present, and a root_agent is exposed."
245+
f" '{actual_agent_name}/root_agent.yaml'.\n\nExpected directory"
246+
f" structure:\n <agents_dir>/\n {actual_agent_name}/\n "
247+
" agent.py (with root_agent) OR\n root_agent.yaml\n\nThen run:"
248+
f" adk web <agents_dir>\n\nEnsure '{agents_dir}/{actual_agent_name}' is"
249+
" structured correctly, an .env file can be loaded if present, and a"
250+
f" root_agent is exposed.{hint}"
234251
)
235252

236253
def _ensure_app_name_matches(

0 commit comments

Comments
 (0)