Skip to content

UnboundLocalError on ApiServer in cli/fast_api.py when running adk web --reload_agents on 2.0.0 #5765

@eliasecchig

Description

@eliasecchig

Bug

adk web . --reload_agents crashes on google-adk==2.0.0:

File ".../google/adk/cli/fast_api.py", line 582, in get_fast_api_app
    def setup_observer(observer: Observer, adk_web_server: ApiServer):
UnboundLocalError: cannot access local variable 'ApiServer' where it is not associated with a value

Bare adk web . is fine (--reload_agents is opt-in).

Cause

fast_api.py imports ApiServer at the module top (line 42) and re-imports it inside get_fast_api_app at lines 529 and 533. The nested imports make ApiServer function-local. On the web=True happy path, DevServer imports successfully so neither nested import runs, leaving the local ApiServer unbound. The setup_observer annotation at line 582 then triggers the error (annotations are evaluated eagerly — no from __future__ import annotations in this file).

Fix

Delete the two redundant nested imports — the module-level import already provides ApiServer:

     except ImportError:
       logger.warning(...)
-      from .api_server import ApiServer
-
       ServerClass = ApiServer
   else:
-    from .api_server import ApiServer
-
     ServerClass = ApiServer

Verified locally: patched file, adk web . --reload_agents starts cleanly.

Workaround

Omit --reload_agents. (adk web has no --no-reload_agents flag.)

Env

google-adk==2.0.0, Python 3.12, macOS.

Metadata

Metadata

Assignees

Labels

v2Affects only 2.0 versionweb[Component] This issue will be transferred to adk-web

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions