adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

fix: register A2A routes from agent.json

Open he-yufeng opened this issue 5 months ago • 6 comments

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.

he-yufeng avatar Jun 07 '26 03:06 he-yufeng

🔍 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.

adk-bot avatar Jun 07 '26 03:06 adk-bot

/adk-pr-analyze

rohityan avatar Jun 09 '26 18:06 rohityan

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.

rohityan avatar Jun 09 '26 18:06 rohityan

Hi @GWeale , can you please review this.

rohityan avatar Jun 09 '26 18:06 rohityan

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.py
  • python -m pre_commit run isort --files src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.py
  • python -m pre_commit run pyink --files src\google\adk\cli\fast_api.py tests\unittests\cli\test_fast_api.py
  • git 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.

he-yufeng avatar Jun 10 '26 13:06 he-yufeng

Rebased this on current main and re-ran the focused local checks:

  • python -m py_compile on the touched Python files
  • pre_commit run isort on the touched files
  • python -m pyink --check on the touched files
  • git diff --check upstream/main..HEAD

All passed locally.

he-yufeng avatar Jun 12 '26 13:06 he-yufeng

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.py
  • python -m pre_commit run isort --files 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 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.

he-yufeng avatar Jun 15 '26 14:06 he-yufeng

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.

GWeale avatar Jul 15 '26 18:07 GWeale