fix: register A2A routes from agent.json
Summary
Fixes #5994.
get_fast_api_app() already imports json at module scope, but a later optional Gemini Enterprise block imported json again inside the function. That makes Python treat json as a local name for the whole function, so the earlier A2A agent.json loader can hit UnboundLocalError before A2A routes are registered.
This removes the inner import and adds a focused regression test that verifies an A2A agent.json is loaded and its routes are registered.
Testing
python -m pytest tests\unittests\cli\test_fast_api.py -q -k "a2a_agent_json_registers_routes"
python -m py_compile src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.py
python -m pyink --check src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.py
git diff --check
I also tried a broader local Windows A2A selection with a2a_agent_discovery and a2a_request_handler_uses_push_config_store; the assertions passed, but teardown hit an existing Windows TemporaryDirectory cleanup issue because those tests leave cwd inside the temp agent directory.
🔍 ADK Pull Request Analysis: PR #6000
Title: fix: register A2A routes from agent.json
Author: @he-yufeng
Status: OPEN
Impact: 79 additions, 2 deletions across 2 files
Executive Summary
- Core Objective: Removes a redundant inner
import jsonstatement inside get_fast_api_app which was causing anUnboundLocalErrorwhen setting up A2A routes under CLI, and adds a regression unit test. - Justification & Value: [Justified Fix] - Resolves a critical regression in
google-adk==2.2.0that completely blocks A2A agent routing and discovery during CLI startup. - Alignment with Principles: [Pass] - Follows architectural best practices, does not introduce breaking API changes, and includes high-quality behavior-focused tests with minimal mocking.
- Recommendation: Approve
Detailed Findings & Analysis
1. Objectives & Impact ("What does it do?")
- Context & Background: In
google-adk==2.2.0, starting the API server with A2A enabled throws a fatalUnboundLocalError:cannot access local variable 'json' where it is not associated with a value. This is caused by Python's static scope-lookup rules: a function-localimport jsonlater in the function declaresjsonas a local for the entire function body, shadowing the global import and causing a crash whenjsonis used earlier in the function during A2A setup. Reference linked Issue #5994. - Implementation Mechanism:
- Removes the redundant local
import jsonon line 748 of fast_api.py, allowing Python to resolvejsonto the module-level import at line 19 of fast_api.py. - Appends a new unit test
test_a2a_agent_json_registers_routesto test_fast_api.py validating that the A2A app starts, loadsagent.json, and registers the correct endpoint routes.
- Removes the redundant local
- Affected Surface: No public interfaces or CLI options are mutated. It alters only internal, private execution paths inside
get_fast_api_app.
2. Justification & Value ("Is it a valid and useful change?")
- Workspace Verification:
- Investigated fast_api.py:L719-721 where
json.load(f)is invoked to parse agent settings. - Concurred that a secondary optional conditional block at fast_api.py:L748 has a local
import json, which triggers the reported runtime crash whena2a=True.
- Investigated fast_api.py:L719-721 where
- Value Assessment: Highly essential regression fix. It eliminates a critical blocker preventing standard developer discovery and messaging flow via A2A.
- Alternative Approaches: The PR's solution is the absolute cleanest one. Eliminating the duplicate local import and reusing the top-level import is elegant and standard.
- Scope & Depth: [Systematic Fix] & [Root Cause] (The code directly targets the underlying name-shading bug rather than just introducing an ad-hoc point catch).
3. Principle & Style Alignment Checklist ("Does it follow rules?")
- Public API & Visibility Boundaries:
- Status: Pass
- Analysis: No public classes, methods, or parameters are altered or exposed.
- Code Quality, Typing & Conventions:
- Status: Pass
- Analysis: Source file retains
from __future__ import annotationsat fast_api.py:L15. Strong typing is enforced, and no redundantAnyis introduced.
- Robustness & Edge Cases:
- Status: Pass
- Analysis: Because
jsonis imported globally at fast_api.py:L19, removing the function local import preserves safety and robustness under all conditions.
- Test Integrity & Quality:
- Status: Pass
- Analysis: The regression test follows the Arrange-Act-Assert pattern. It mocks out only the external A2A library boundaries/Starlette applications and database service configurations, maintaining clear logical separation blocks.
/adk-pr-analyze
Hi @he-yufeng , 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. Can you please fix the formatting errors meanwhile.
Hi @GWeale , can you please review this.
Pushed 166eff1c to apply the import formatting requested by pre-commit.
Validation:
python -m py_compile src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.pypython -m pre_commit run isort --files src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.pypython -m pre_commit run pyink --files src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.pygit diff --check
The local targeted pytest for this A2A path needs the optional A2A/Google dependency stack on Windows; the existing CI test matrix already passed on the previous commit, and this push only changes one import blank line.
Rebased this on current main and re-ran the focused local checks:
python -m py_compileon the touched Python filespre_commit run isorton the touched filespython -m pyink --checkon the touched filesgit diff --check upstream/main..HEAD
All passed locally.
Rebased this onto current main again. The diff is still limited to the A2A agent.json route-registration fix and the small formatting follow-up.
Current head: 0236a24a
Validation run locally:
python -m py_compile src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.pypython -m pre_commit run isort --files src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.pypython -m pyink --check src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.pygit diff --check upstream/main..HEAD
I also tried the focused A2A pytest locally. It still cannot run in this Windows checkout without the optional a2a dependency stack installed; collection reaches the A2A mocks and fails before exercising the assertion path. The file-level checks above are clean after the rebase.
Thanks for this! The A2A agent.json route registration has since landed in main, so the remaining change here is already covered. Closing as done, appreciate the contribution.