fix(cli): resolve UnboundLocalError for json when serving A2A agents#6284
Closed
RUI-LONG wants to merge 1 commit into
Closed
fix(cli): resolve UnboundLocalError for json when serving A2A agents#6284RUI-LONG wants to merge 1 commit into
RUI-LONG wants to merge 1 commit into
Conversation
`get_fast_api_app` imports `json` at module level, but also re-imports it
locally inside the `gemini_enterprise_app_name` branch. Because Python
treats any name assigned (including via `import`) anywhere in a function
as local to the entire function, the module-level `json` is shadowed
throughout `get_fast_api_app`.
When A2A is enabled (`adk api_server --a2a`), the A2A setup block runs
`json.load(f)` to read each agent's `agent.json` *before* the local
`import json` executes, raising:
cannot access local variable 'json' where it is not associated with a value
so every A2A agent fails to register (logged as "Failed to setup A2A
agent ...").
The local `import json` is redundant with the module-level import, so
this removes it. `json.load`/`json.dumps` now resolve to the module-level
`json` as intended.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
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