You've already forked adk-python
mirror of
https://github.com/encounter/adk-python.git
synced 2026-03-30 10:57:20 -07:00
feat(cli): Add --auto_create_session flag to adk api_server CLI
Merge https://github.com/google/adk-python/pull/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=https://github.com/google/adk-python/pull/4288 from ekimcodes:main 3c8d299a88b21789431dbda488befba7ee3ace81
PiperOrigin-RevId: 868361303
This commit is contained in:
committed by
Copybara-Service
parent
2010569010
commit
40c15d0595
@@ -494,6 +494,7 @@ class AdkWebServer:
|
||||
logo_text: Optional[str] = None,
|
||||
logo_image_url: Optional[str] = None,
|
||||
url_prefix: Optional[str] = None,
|
||||
auto_create_session: bool = False,
|
||||
):
|
||||
self.agent_loader = agent_loader
|
||||
self.session_service = session_service
|
||||
@@ -511,6 +512,7 @@ class AdkWebServer:
|
||||
self.current_app_name_ref: SharedValue[str] = SharedValue(value="")
|
||||
self.runner_dict = {}
|
||||
self.url_prefix = url_prefix
|
||||
self.auto_create_session = auto_create_session
|
||||
|
||||
async def get_runner_async(self, app_name: str) -> Runner:
|
||||
"""Returns the cached runner for the given app."""
|
||||
@@ -560,6 +562,7 @@ class AdkWebServer:
|
||||
session_service=self.session_service,
|
||||
memory_service=self.memory_service,
|
||||
credential_service=self.credential_service,
|
||||
auto_create_session=self.auto_create_session,
|
||||
)
|
||||
|
||||
def _instantiate_extra_plugins(self) -> list[BasePlugin]:
|
||||
|
||||
@@ -1419,6 +1419,14 @@ def cli_web(
|
||||
@fast_api_common_options()
|
||||
@adk_services_options(default_use_local_storage=True)
|
||||
@deprecated_adk_services_options()
|
||||
@click.option(
|
||||
"--auto_create_session",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
help=(
|
||||
"Automatically create a session if it doesn't exist when calling /run."
|
||||
),
|
||||
)
|
||||
def cli_api_server(
|
||||
agents_dir: str,
|
||||
eval_storage_uri: Optional[str] = None,
|
||||
@@ -1439,6 +1447,7 @@ def cli_api_server(
|
||||
a2a: bool = False,
|
||||
reload_agents: bool = False,
|
||||
extra_plugins: Optional[list[str]] = None,
|
||||
auto_create_session: bool = False,
|
||||
):
|
||||
"""Starts a FastAPI server for agents.
|
||||
|
||||
@@ -1471,6 +1480,7 @@ def cli_api_server(
|
||||
url_prefix=url_prefix,
|
||||
reload_agents=reload_agents,
|
||||
extra_plugins=extra_plugins,
|
||||
auto_create_session=auto_create_session,
|
||||
),
|
||||
host=host,
|
||||
port=port,
|
||||
|
||||
@@ -91,6 +91,7 @@ def get_fast_api_app(
|
||||
extra_plugins: Optional[list[str]] = None,
|
||||
logo_text: Optional[str] = None,
|
||||
logo_image_url: Optional[str] = None,
|
||||
auto_create_session: bool = False,
|
||||
) -> FastAPI:
|
||||
|
||||
# Set up eval managers.
|
||||
@@ -153,6 +154,7 @@ def get_fast_api_app(
|
||||
logo_text=logo_text,
|
||||
logo_image_url=logo_image_url,
|
||||
url_prefix=url_prefix,
|
||||
auto_create_session=auto_create_session,
|
||||
)
|
||||
|
||||
# Callbacks & other optional args for when constructing the FastAPI instance
|
||||
|
||||
Reference in New Issue
Block a user