Skip to content

Commit 34da2d5

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: enable dependency injection for agent loader in FastAPI app gen
This enables to re-use the ADK web interface in other contexts more easily. For example, when having an own run-time the web interface can be exposed for visualization during development. Also add documentation for function. PiperOrigin-RevId: 868848338
1 parent bcbfeba commit 34da2d5

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/google/adk/cli/fast_api.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from .utils import evals
4646
from .utils.agent_change_handler import AgentChangeEventHandler
4747
from .utils.agent_loader import AgentLoader
48+
from .utils.base_agent_loader import BaseAgentLoader
4849
from .utils.service_factory import create_artifact_service_from_options
4950
from .utils.service_factory import create_memory_service_from_options
5051
from .utils.service_factory import create_session_service_from_options
@@ -72,6 +73,7 @@ def __getattr__(name: str):
7273
def get_fast_api_app(
7374
*,
7475
agents_dir: str,
76+
agent_loader: Optional[BaseAgentLoader] = None,
7577
session_service_uri: Optional[str] = None,
7678
session_db_kwargs: Optional[Mapping[str, Any]] = None,
7779
artifact_service_uri: Optional[str] = None,
@@ -93,6 +95,52 @@ def get_fast_api_app(
9395
logo_image_url: Optional[str] = None,
9496
auto_create_session: bool = False,
9597
) -> FastAPI:
98+
"""Constructs and returns a FastAPI application for serving ADK agents.
99+
100+
This function orchestrates the initialization of core ADK services (Session,
101+
Artifact, Memory, and Credential) based on the provided configuration,
102+
configures the ADK Web Server, and optionally enables advanced features
103+
like Agent-to-Agent (A2A) protocol support and cloud telemetry.
104+
105+
Args:
106+
agents_dir: The root directory containing agent definitions. This path is
107+
used to discover agents, load custom service registrations (via
108+
services.py/yaml), and as a base for local storage.
109+
agent_loader: An optional custom loader for retrieving agent instances. If
110+
not provided, a default AgentLoader targeting agents_dir is used.
111+
session_service_uri: A URI defining the backend for session persistence.
112+
Supports schemes like 'memory://', 'sqlite://', 'postgresql://',
113+
'mysql://', or 'agentengine://'. Defaults to per-agent local SQLite
114+
storage if None.
115+
session_db_kwargs: Optional keyword arguments for custom session service
116+
initialization. These are passed to the service factory along with the
117+
URI.
118+
artifact_service_uri: URI for the artifact service. Uses local artifact
119+
service if None.
120+
memory_service_uri: URI for the memory service. Uses local memory service if
121+
None.
122+
use_local_storage: Whether to use local storage for session and artifacts.
123+
eval_storage_uri: URI for evaluation storage. If provided, uses GCS
124+
managers.
125+
allow_origins: List of allowed origins for CORS.
126+
web: Whether to enable the web UI and serve its assets.
127+
a2a: Whether to enable Agent-to-Agent (A2A) protocol support.
128+
host: Host address for the server (defaults to 127.0.0.1).
129+
port: Port number for the server (defaults to 8000).
130+
url_prefix: Optional prefix for all URL routes.
131+
trace_to_cloud: Whether to export traces to Google Cloud Trace.
132+
otel_to_cloud: Whether to export OpenTelemetry data to Google Cloud.
133+
reload_agents: Whether to watch for file changes and reload agents.
134+
lifespan: Optional FastAPI lifespan context manager.
135+
extra_plugins: List of extra plugin names to load.
136+
logo_text: Text to display in the web UI logo area.
137+
logo_image_url: URL for an image to display in the web UI logo area.
138+
auto_create_session: Whether to automatically create a session when
139+
not found.
140+
141+
Returns:
142+
The configured FastAPI application instance.
143+
"""
96144

97145
# Set up eval managers.
98146
if eval_storage_uri:
@@ -105,8 +153,10 @@ def get_fast_api_app(
105153
eval_sets_manager = LocalEvalSetsManager(agents_dir=agents_dir)
106154
eval_set_results_manager = LocalEvalSetResultsManager(agents_dir=agents_dir)
107155

108-
# initialize Agent Loader
109-
agent_loader = AgentLoader(agents_dir)
156+
# initialize Agent Loader if not passed as argument
157+
if agent_loader is None:
158+
agent_loader = AgentLoader(agents_dir)
159+
110160
# Load services.py from agents_dir for custom service registration.
111161
load_services_module(agents_dir)
112162

0 commit comments

Comments
 (0)