Skip to content

fix: register A2A routes from agent.json#6000

Open
he-yufeng wants to merge 1 commit into
google:mainfrom
he-yufeng:fix/a2a-json-import-scope
Open

fix: register A2A routes from agent.json#6000
he-yufeng wants to merge 1 commit into
google:mainfrom
he-yufeng:fix/a2a-json-import-scope

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

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-bot adk-bot added the web [Component] This issue will be transferred to adk-web label Jun 7, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Jun 7, 2026

🔍 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

  1. Core Objective: Removes a redundant inner import json statement inside get_fast_api_app which was causing an UnboundLocalError when setting up A2A routes under CLI, and adds a regression unit test.
  2. Justification & Value: [Justified Fix] - Resolves a critical regression in google-adk==2.2.0 that completely blocks A2A agent routing and discovery during CLI startup.
  3. Alignment with Principles: [Pass] - Follows architectural best practices, does not introduce breaking API changes, and includes high-quality behavior-focused tests with minimal mocking.
  4. 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 fatal UnboundLocalError: 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-local import json later in the function declares json as a local for the entire function body, shadowing the global import and causing a crash when json is used earlier in the function during A2A setup. Reference linked Issue #5994.
  • Implementation Mechanism:
    • Removes the redundant local import json on line 748 of fast_api.py, allowing Python to resolve json to the module-level import at line 19 of fast_api.py.
    • Appends a new unit test test_a2a_agent_json_registers_routes to test_fast_api.py validating that the A2A app starts, loads agent.json, and registers the correct endpoint routes.
  • 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 when a2a=True.
  • 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 annotations at fast_api.py:L15. Strong typing is enforced, and no redundant Any is introduced.
  • Robustness & Edge Cases:
    • Status: Pass
    • Analysis: Because json is 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

web [Component] This issue will be transferred to adk-web

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adk api_server --a2a fails to register A2A routes in google-adk==2.2.0

2 participants