Skip to content

Commit 338c3c8

Browse files
dongyuj1copybara-github
authored andcommitted
fix: Add example and fix for loading and upgrading old ADK session databases
This change introduces a sample (`migration_session_db`) demonstrating how to load a session database created with an older version of ADK (e.g., 1.15.0) and make it compatible with the current version. It includes a script (`db_migration.sh`) to alter the SQLite schema automatically. to_event is updated to handle potential discrepancies in pickled `EventActions` by using `model_copy` to ensure compatibility with the latest `EventActions` model definition. Related to #3272 #3197, Closes #3197 #3272 Co-authored-by: Dongyu Jia <[email protected]> PiperOrigin-RevId: 826524368
1 parent a2c6a8a commit 338c3c8

File tree

12 files changed

+653
-1
lines changed

12 files changed

+653
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Loading and Upgrading Old Session Databases
2+
3+
This example demonstrates how to upgrade a session database created with an older version of ADK to be compatible with the current version.
4+
5+
## Sample Database
6+
7+
This sample includes `dnd_sessions.db`, a database created with ADK v1.15.0. The following steps show how to run into a schema error and then resolve it using the migration script.
8+
9+
## 1. Reproduce the Error
10+
11+
First, copy the old database to `sessions.db`, which is the file the sample application expects.
12+
13+
```bash
14+
cp dnd_sessions.db sessions.db
15+
python main.py
16+
```
17+
18+
Running the application against the old database will fail with a schema mismatch error, as the `events` table is missing a column required by newer ADK versions:
19+
20+
```
21+
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: events.usage_metadata
22+
```
23+
24+
## 2. Upgrade the Database Schema
25+
26+
ADK provides a migration script to update the database schema. Run the following command to download and execute it.
27+
28+
```bash
29+
# Clean up the previous run before executing the migration
30+
cp dnd_sessions.db sessions.db
31+
32+
# Download and run the migration script
33+
curl -fsSL https://raw.githubusercontent.com/google/adk-python/main/scripts/db_migration.sh | sh -s -- "sqlite:///%(here)s/sessions.db" "google.adk.sessions.database_session_service"
34+
```
35+
36+
This script uses `alembic` to compare the existing schema against the current model definition and automatically generates and applies the necessary migrations.
37+
38+
**Note on generated files:**
39+
* The script will create an `alembic.ini` file and an `alembic/` directory. You must delete these before re-running the script.
40+
* The `sample-output` directory in this example contains a reference of the generated files for your inspection.
41+
* The `%(here)s` variable in the database URL is an `alembic` placeholder that refers to the current directory.
42+
43+
## 3. Run the Agent Successfully
44+
45+
With the database schema updated, the application can now load the session correctly.
46+
47+
```bash
48+
python main.py
49+
```
50+
51+
You should see output indicating that the old session was successfully loaded.
52+
53+
## Limitations
54+
55+
The migration script is designed to add new columns that have been introduced in newer ADK versions. It does not handle more complex schema changes, such as modifying a column's data type (e.g., from `int` to `string`) or altering the internal structure of stored data.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from . import agent
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import random
17+
18+
from google.adk.agents.llm_agent import Agent
19+
20+
21+
def roll_die(sides: int) -> int:
22+
"""Roll a die and return the rolled result.
23+
24+
Args:
25+
sides: The integer number of sides the die has.
26+
27+
Returns:
28+
An integer of the result of rolling the die.
29+
"""
30+
return random.randint(1, sides)
31+
32+
33+
async def check_prime(nums: list[int]) -> str:
34+
"""Check if a given list of numbers are prime.
35+
36+
Args:
37+
nums: The list of numbers to check.
38+
39+
Returns:
40+
A str indicating which number is prime.
41+
"""
42+
primes = set()
43+
for number in nums:
44+
number = int(number)
45+
if number <= 1:
46+
continue
47+
is_prime = True
48+
for i in range(2, int(number**0.5) + 1):
49+
if number % i == 0:
50+
is_prime = False
51+
break
52+
if is_prime:
53+
primes.add(number)
54+
return (
55+
"No prime numbers found."
56+
if not primes
57+
else f"{', '.join(str(num) for num in primes)} are prime numbers."
58+
)
59+
60+
61+
root_agent = Agent(
62+
model="gemini-2.0-flash",
63+
name="migrate_session_db_agent",
64+
description=(
65+
"hello world agent that can roll a dice of 8 sides and check prime"
66+
" numbers."
67+
),
68+
instruction="""
69+
You roll dice and answer questions about the outcome of the dice rolls.
70+
You can roll dice of different sizes.
71+
You can use multiple tools in parallel by calling functions in parallel(in one request and in one round).
72+
It is ok to discuss previous dice roles, and comment on the dice rolls.
73+
When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string.
74+
You should never roll a die on your own.
75+
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
76+
You should not check prime numbers before calling the tool.
77+
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
78+
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
79+
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
80+
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
81+
3. When you respond, you must include the roll_die result from step 1.
82+
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
83+
You should not rely on the previous history on prime results.
84+
""",
85+
tools=[
86+
roll_die,
87+
check_prime,
88+
],
89+
)
48 KB
Binary file not shown.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import asyncio
17+
import time
18+
19+
import agent
20+
from dotenv import load_dotenv
21+
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService
22+
from google.adk.cli.utils import logs
23+
from google.adk.runners import Runner
24+
from google.adk.sessions.database_session_service import DatabaseSessionService
25+
from google.adk.sessions.session import Session
26+
from google.genai import types
27+
28+
load_dotenv(override=True)
29+
logs.log_to_tmp_folder()
30+
31+
32+
async def main():
33+
app_name = 'migrate_session_db_app'
34+
user_id_1 = 'user1'
35+
session_service = DatabaseSessionService('sqlite:///./sessions.db')
36+
artifact_service = InMemoryArtifactService()
37+
runner = Runner(
38+
app_name=app_name,
39+
agent=agent.root_agent,
40+
artifact_service=artifact_service,
41+
session_service=session_service,
42+
)
43+
session_11 = await session_service.get_session(
44+
app_name=app_name,
45+
user_id=user_id_1,
46+
session_id='aee03f34-32ef-432b-b1bb-e66a3a79dd5b',
47+
)
48+
print('Session 11 loaded:', session_11.id)
49+
50+
async def run_prompt(session: Session, new_message: str):
51+
content = types.Content(
52+
role='user', parts=[types.Part.from_text(text=new_message)]
53+
)
54+
print('** User says:', content.model_dump(exclude_none=True))
55+
async for event in runner.run_async(
56+
user_id=user_id_1,
57+
session_id=session.id,
58+
new_message=content,
59+
):
60+
if event.content.parts and event.content.parts[0].text:
61+
print(f'** {event.author}: {event.content.parts[0].text}')
62+
63+
start_time = time.time()
64+
print('Start time:', start_time)
65+
print('------------------------------------')
66+
await run_prompt(session_11, 'Hi, introduce yourself.')
67+
await run_prompt(
68+
session_11, 'Roll a die with 100 sides and check if it is prime'
69+
)
70+
await run_prompt(session_11, 'Roll it again.')
71+
await run_prompt(session_11, 'What numbers did I got?')
72+
end_time = time.time()
73+
print('------------------------------------')
74+
print('End time:', end_time)
75+
print('Total time:', end_time - start_time)
76+
77+
78+
if __name__ == '__main__':
79+
asyncio.run(main())
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts.
5+
# this is typically a path given in POSIX (e.g. forward slashes)
6+
# format, relative to the token %(here)s which refers to the location of this
7+
# ini file
8+
script_location = %(here)s/alembic
9+
10+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
11+
# Uncomment the line below if you want the files to be prepended with date and time
12+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
13+
# for all available tokens
14+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
15+
16+
# sys.path path, will be prepended to sys.path if present.
17+
# defaults to the current working directory. for multiple paths, the path separator
18+
# is defined by "path_separator" below.
19+
prepend_sys_path = .
20+
21+
22+
# timezone to use when rendering the date within the migration file
23+
# as well as the filename.
24+
# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.
25+
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
26+
# string value is passed to ZoneInfo()
27+
# leave blank for localtime
28+
# timezone =
29+
30+
# max length of characters to apply to the "slug" field
31+
# truncate_slug_length = 40
32+
33+
# set to 'true' to run the environment during
34+
# the 'revision' command, regardless of autogenerate
35+
# revision_environment = false
36+
37+
# set to 'true' to allow .pyc and .pyo files without
38+
# a source .py file to be detected as revisions in the
39+
# versions/ directory
40+
# sourceless = false
41+
42+
# version location specification; This defaults
43+
# to <script_location>/versions. When using multiple version
44+
# directories, initial revisions must be specified with --version-path.
45+
# The path separator used here should be the separator specified by "path_separator"
46+
# below.
47+
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
48+
49+
# path_separator; This indicates what character is used to split lists of file
50+
# paths, including version_locations and prepend_sys_path within configparser
51+
# files such as alembic.ini.
52+
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
53+
# to provide os-dependent path splitting.
54+
#
55+
# Note that in order to support legacy alembic.ini files, this default does NOT
56+
# take place if path_separator is not present in alembic.ini. If this
57+
# option is omitted entirely, fallback logic is as follows:
58+
#
59+
# 1. Parsing of the version_locations option falls back to using the legacy
60+
# "version_path_separator" key, which if absent then falls back to the legacy
61+
# behavior of splitting on spaces and/or commas.
62+
# 2. Parsing of the prepend_sys_path option falls back to the legacy
63+
# behavior of splitting on spaces, commas, or colons.
64+
#
65+
# Valid values for path_separator are:
66+
#
67+
# path_separator = :
68+
# path_separator = ;
69+
# path_separator = space
70+
# path_separator = newline
71+
#
72+
# Use os.pathsep. Default configuration used for new projects.
73+
path_separator = os
74+
75+
# set to 'true' to search source files recursively
76+
# in each "version_locations" directory
77+
# new in Alembic version 1.10
78+
# recursive_version_locations = false
79+
80+
# the output encoding used when revision files
81+
# are written from script.py.mako
82+
# output_encoding = utf-8
83+
84+
# database URL. This is consumed by the user-maintained env.py script only.
85+
# other means of configuring database URLs may be customized within the env.py
86+
# file.
87+
sqlalchemy.url = sqlite:///%(here)s/sessions.db
88+
89+
90+
[post_write_hooks]
91+
# post_write_hooks defines scripts or Python functions that are run
92+
# on newly generated revision scripts. See the documentation for further
93+
# detail and examples
94+
95+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
96+
# hooks = black
97+
# black.type = console_scripts
98+
# black.entrypoint = black
99+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
100+
101+
# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module
102+
# hooks = ruff
103+
# ruff.type = module
104+
# ruff.module = ruff
105+
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
106+
107+
# Alternatively, use the exec runner to execute a binary found on your PATH
108+
# hooks = ruff
109+
# ruff.type = exec
110+
# ruff.executable = ruff
111+
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
112+
113+
# Logging configuration. This is also consumed by the user-maintained
114+
# env.py script only.
115+
[loggers]
116+
keys = root,sqlalchemy,alembic
117+
118+
[handlers]
119+
keys = console
120+
121+
[formatters]
122+
keys = generic
123+
124+
[logger_root]
125+
level = WARNING
126+
handlers = console
127+
qualname =
128+
129+
[logger_sqlalchemy]
130+
level = WARNING
131+
handlers =
132+
qualname = sqlalchemy.engine
133+
134+
[logger_alembic]
135+
level = INFO
136+
handlers =
137+
qualname = alembic
138+
139+
[handler_console]
140+
class = StreamHandler
141+
args = (sys.stderr,)
142+
level = NOTSET
143+
formatter = generic
144+
145+
[formatter_generic]
146+
format = %(levelname)-5.5s [%(name)s] %(message)s
147+
datefmt = %H:%M:%S
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

0 commit comments

Comments
 (0)