🔴 Required Information
Describe the Bug:
After upgrading google-adk from 2.1.0 to 2.2.0, running adk api_server --a2a --with_ui starts the FastAPI server successfully but fails to register A2A routes for an agent configured with agent.json.
The server logs the following during startup:
Failed to setup A2A agent orchestrator: cannot access local variable 'json' where it is not associated with a value
As a result, the expected A2A agent card endpoint is not registered and returns 404:
GET /a2a/orchestrator/.well-known/agent-card.json HTTP/1.1" 404 Not Found
This appears to be caused by a function-local import json inside get_fast_api_app() in google/adk/cli/fast_api.py. The function uses json.load(...) earlier during A2A route setup, but because json is later imported inside the same function, Python treats json as a local variable throughout the whole function scope. This causes an UnboundLocalError before the local import has executed.
Steps to Reproduce:
-
Install google-adk==2.2.0.
-
Create an ADK agents directory containing an agent subdirectory with an agent.json file.
Example structure:
orchestrator_demo/
orchestrator/
agent.json
-
Start the ADK API server with A2A enabled.
uv run adk api_server --a2a --with_ui orchestrator_demo \
--host 127.0.0.1 \
--port 8000 \
--session_service_uri sqlite:///sessions.sqlite \
--artifact_service_uri file:///artifacts
-
Observe the server startup logs.
Setting up A2A agent: orchestrator
Failed to setup A2A agent orchestrator: cannot access local variable 'json' where it is not associated with a value
Application startup complete.
-
Attempt to access the expected A2A agent card endpoint.
curl http://127.0.0.1:8000/a2a/orchestrator/.well-known/agent-card.json
-
Observe that the endpoint returns 404 Not Found.
Expected Behavior:
ADK should load:
orchestrator_demo/orchestrator/agent.json
Then create the A2A application and register A2A routes, including:
/a2a/orchestrator
/a2a/orchestrator/.well-known/agent-card.json
The agent card endpoint should return the configured A2A agent card instead of 404.
Observed Behavior:
The FastAPI server starts and reports startup completion, but A2A route setup fails internally.
Relevant startup log:
Setting up A2A agent: orchestrator
Failed to setup A2A agent orchestrator: cannot access local variable 'json' where it is not associated with a value
Application startup complete.
The expected A2A route is not registered:
GET /a2a/orchestrator/.well-known/agent-card.json HTTP/1.1" 404 Not Found
The server appears healthy, but A2A discovery and message flow are unavailable because the A2A endpoints are missing.
Environment Details:
- ADK Library Version (
pip show google-adk): 2.2.0
- Desktop OS: N/A
- Python Version (
python -V): Python 3.11
Model Information:
- Are you using LiteLLM: No
- Which model is being used: N/A
🟡 Optional Information
Regression:
Yes. This worked in google-adk==2.1.0 and fails after upgrading to google-adk==2.2.0.
Logs:
Setting up A2A agent: orchestrator
Failed to setup A2A agent orchestrator: cannot access local variable 'json' where it is not associated with a value
Application startup complete.
GET /a2a/orchestrator/.well-known/agent-card.json HTTP/1.1" 404 Not Found
Screenshots / Video:
N/A
Additional Context:
The suspected root cause is in google/adk/cli/fast_api.py, inside get_fast_api_app().
The function uses json.load(...) while setting up A2A routes. Later in the same function, there is a function-local import:
Because Python treats any name assigned or imported inside a function as local to the entire function scope, the earlier json.load(...) resolves json as a local variable before it has been assigned. This causes:
UnboundLocalError: cannot access local variable 'json' where it is not associated with a value
This can occur even if the later conditional branch containing import json is not executed, because Python determines local variables at function compile time.
The module already imports json at module scope, so the later function-local import json appears unnecessary.
Suggested fix:
- import inspect
- import json
+ import inspect
Alternatively, move all required imports to module scope and rely on the existing module-level json import.
A regression test could call get_fast_api_app(..., a2a=True, web=True) against a temporary agents directory containing:
Then assert that the generated FastAPI app includes routes for:
/a2a/orchestrator
/a2a/orchestrator/.well-known/agent-card.json
Minimal Reproduction Code:
# Minimal reproduction is primarily CLI-based.
# Directory structure:
#
# orchestrator_demo/
# orchestrator/
# agent.json
#
# Then run:
#
# uv run adk api_server --a2a --with_ui orchestrator_demo \
# --host 127.0.0.1 \
# --port 8000 \
# --session_service_uri sqlite:///sessions.sqlite \
# --artifact_service_uri file:///artifacts
#
# Then request:
#
# curl http://127.0.0.1:8000/a2a/orchestrator/.well-known/agent-card.json
#
# Expected:
# Agent card JSON
#
# Actual:
# 404 Not Found
How often has this issue occurred?:
🔴 Required Information
Describe the Bug:
After upgrading
google-adkfrom2.1.0to2.2.0, runningadk api_server --a2a --with_uistarts the FastAPI server successfully but fails to register A2A routes for an agent configured withagent.json.The server logs the following during startup:
As a result, the expected A2A agent card endpoint is not registered and returns
404:This appears to be caused by a function-local
import jsoninsideget_fast_api_app()ingoogle/adk/cli/fast_api.py. The function usesjson.load(...)earlier during A2A route setup, but becausejsonis later imported inside the same function, Python treatsjsonas a local variable throughout the whole function scope. This causes anUnboundLocalErrorbefore the local import has executed.Steps to Reproduce:
Install
google-adk==2.2.0.Create an ADK agents directory containing an agent subdirectory with an
agent.jsonfile.Example structure:
Start the ADK API server with A2A enabled.
Observe the server startup logs.
Attempt to access the expected A2A agent card endpoint.
Observe that the endpoint returns
404 Not Found.Expected Behavior:
ADK should load:
Then create the A2A application and register A2A routes, including:
The agent card endpoint should return the configured A2A agent card instead of
404.Observed Behavior:
The FastAPI server starts and reports startup completion, but A2A route setup fails internally.
Relevant startup log:
The expected A2A route is not registered:
The server appears healthy, but A2A discovery and message flow are unavailable because the A2A endpoints are missing.
Environment Details:
pip show google-adk):2.2.0python -V):Python 3.11Model Information:
🟡 Optional Information
Regression:
Yes. This worked in
google-adk==2.1.0and fails after upgrading togoogle-adk==2.2.0.Logs:
Screenshots / Video:
N/A
Additional Context:
The suspected root cause is in
google/adk/cli/fast_api.py, insideget_fast_api_app().The function uses
json.load(...)while setting up A2A routes. Later in the same function, there is a function-local import:Because Python treats any name assigned or imported inside a function as local to the entire function scope, the earlier
json.load(...)resolvesjsonas a local variable before it has been assigned. This causes:This can occur even if the later conditional branch containing
import jsonis not executed, because Python determines local variables at function compile time.The module already imports
jsonat module scope, so the later function-localimport jsonappears unnecessary.Suggested fix:
Alternatively, move all required imports to module scope and rely on the existing module-level
jsonimport.A regression test could call
get_fast_api_app(..., a2a=True, web=True)against a temporary agents directory containing:Then assert that the generated FastAPI app includes routes for:
Minimal Reproduction Code:
How often has this issue occurred?: