fix(cli): resolve UnboundLocalError for json when serving A2A agents - #6285
fix(cli): resolve UnboundLocalError for json when serving A2A agents#6285RUI-LONG wants to merge 3 commits into
json when serving A2A agents#6285Conversation
|
Hi @RUI-LONG , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share. |
|
Hi @wyf7107 , can you please review this. The problem resolves with this fix. LGTM. |
|
Response from ADK Triaging Agent Hello @RUI-LONG, thank you for your contribution and for the detailed explanation of this issue! To ensure high code quality and prevent future regressions, could you please add a unit test for this change? As you noted in your testing plan, a regression test under Please refer to our testing guidelines for more details. This will help our reviewers process your PR much faster. Thanks! |
|
Thanks for the cleanup! That duplicate local import json has already been removed in main as part of a later refactor, so this is covered. Closing it out, appreciate the sharp eye. |
I am following the official document, but the A2A server fails to expose any agent endpoint.
What I did
Following the quickstart, I started the API server with A2A enabled from an agent directory that contains an agent.json card:
adk api_server --a2a --port 8001 .What I expected
The A2A agent to be registered and its endpoints (/a2a/<app_name> RPC route and /a2a/<app_name>/.well-known/agent-card.json) to be served, as described in the doc.
What actually happened
The server starts, but every A2A agent fails to register. The relevant log:
Problem:
Starting the API server with A2A enabled fails to register any A2A agent. Every
agent that ships an
agent.jsoncard is silently skipped, so its/a2a/<app_name>RPC route and the corresponding/.well-known/agent-card.jsonendpoint are never mounted.Repro (a single agent directory containing
agent.json):Root cause is a Python scoping issue in
get_fast_api_app().jsonis importedat module level, but the function also imports it locally inside the
if gemini_enterprise_app_name:branch:Because Python binds any name that is assigned anywhere in a function
(
importcounts as an assignment) as local to the entire function, themodule-level
jsonis shadowed throughoutget_fast_api_app(). At (1) the A2Ablock runs
json.load(f)before the localimport jsonat (2) has executed, soit dereferences an unbound local and raises:
The
except Exceptionguarding A2A setup swallows this into theFailed to setup A2A agent ...log line, which is why the server still startsbut exposes no A2A endpoints — making the failure easy to miss.
Solution:
Remove the redundant local
import json. It duplicates the module-level importon line 19; deleting it stops
jsonfrom being treated as a function-local andlets
json.load/json.dumpsresolve to the module-leveljsonas intended.This is the minimal, single-line fix and keeps the imports in one place rather
than adding yet another local re-import to work around the shadowing.
import inspect - import json from google.adk.agents import AgentTesting Plan
Unit Tests:
Not added yet — happy to add a regression test under
tests/unittests/cli/that callsget_fast_api_app(..., a2a=True)against atemp agents dir with an
agent.jsonand asserts the/a2a/<app>route ismounted (it currently would not be). Guidance on the preferred fixture welcome.
Checklist