fix: Return 404 instead of 500 when app_name does not match any agent#6376
Open
ItsMacto wants to merge 1 commit into
Open
fix: Return 404 instead of 500 when app_name does not match any agent#6376ItsMacto wants to merge 1 commit into
ItsMacto wants to merge 1 commit into
Conversation
Catch the ValueError raised by AgentLoader.load_agent in get_runner_async
and the app-info endpoint and convert it to HTTPException(404), matching
the dev server behavior. Affects /run, /run_sse, and /apps/{app_name}/app-info.
Fixes google#5374
Contributor
Author
|
/gemini review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
adk_web_server.pyshim; this PR applies the fix to the currentapi_server.py)2. Or, if no issue exists, describe the change:
Problem:
AgentLoader.load_agentraisesValueError("Agent not found: ...")for an unknown app name, and neitherget_runner_asyncnor the/apps/{app_name}/app-infohandler incli/api_server.pycatches it. A request with an invalidapp_nameto/run,/run_sse, or/apps/{app_name}/app-infotherefore returns an unhandled 500 instead of a 404.Solution:
Catch
ValueErrortightly around the twoload_agentcall sites and convert it toHTTPException(status_code=404, detail=str(ve)), chained withfrom ve— the same pattern the dev server already uses. The try blocks wrap only theload_agentcall, so unrelatedValueErrors (plugin loading, YAML parsing, agent construction) still fail loudly. Since/runand/run_sseboth resolve their runner throughget_runner_async, all three reported endpoints are covered.Testing Plan
Unit Tests:
Three endpoint-level regression tests added to
tests/unittests/cli/test_fast_api.py, each driving the real endpoint through FastAPI'sTestClientwith a nonexistent app name and asserting a 404 whose detail carries the "Agent not found" message:POST /run→ 404POST /run_sse→ 404GET /apps/{app_name}/app-info→ 404All three fail on the unfixed code (unhandled
ValueError) and pass with the fix:pyink --checkandisort --check-onlyare clean on both touched files.Manual End-to-End (E2E) Tests:
To reproduce: start
adk api_serverin any agents directory, thencurl -X POST localhost:8000/run -H 'Content-Type: application/json' -d '{"app_name": "no_such_app", "user_id": "u", "session_id": "s", "new_message": {"role": "user", "parts": [{"text": "hi"}]}}'. Before this change the server returns a 500 with a traceback in the logs; with it, a 404 with"Agent not found: 'no_such_app'..."in the detail. Same forGET /apps/no_such_app/app-info.Checklist
Additional context
get_runner_asynchas callers beyond the three endpoints (websocket/run_live, trigger routes); all were checked. The websocket path leaves an unknown app equally unhandled before and after this change (pre-existing, out of scope), and the trigger path's retry loop treatsHTTPExceptionexactly as it treatedValueError(non-transient, immediate re-raise). No caller depended on catchingValueError.