はじめに
七尾百合子さん、お誕生日 266日目 おめでとうございます! nikkieです。
Google による Python 版 Agent Development Kit は現在、隔週でリリースされています。
まとめてバージョンを上げようとしたら、session まわりでつまづきました。
目次
- はじめに
- 目次
- ADK の session
- session に関するテーブル定義の変更
- samples の migrate_session_db に示されていた
- SQLite を例に動作確認
- 終わりに
- P.S. adk migrate session?
ADK の session
https://google.github.io/adk-docs/runtime/#session
Role: A data container holding the state and history for one specific conversation between a user and the application.
session はデータコンテナ。
ユーザとアプリケーションの一連の会話の state と history を保持します。
ADK の Web UI でユーザとエージェントが何往復かやり取りできますが、それを保持します(1往復でもいいし、何十往復してもよい)。
LLM 自体は状態を持たず、これまでの履歴込みで入力する1ことで、アプリケーションは過去の会話を踏まえて応答できます。
全く新しい会話に移行するときは session を新しくします。
Function: Stores the current
statedictionary, the list of all pastevents(event history), and references to associated artifacts.
It's the primary record of the interaction, managed by theSessionService.
セッションが保持するのは、state、過去のすべてのevents、関連する artifacts への参照。
SessionServiceによって管理されます。
SessionServiceには3つあります。
https://google.github.io/adk-docs/sessions/session/#sessionservice-implementations
InMemorySessionServiceVertexAiSessionServiceDatabaseSessionService
このうちDatabaseSessionServiceでハマりました。
session に関するテーブル定義の変更
隔週のマイナーバージョンアップの中で、Google はしれっとテーブルにカラムを追加しています。
一例:https://github.com/google/adk-python/releases/tag/v1.14.0
NOTE: This requires DB migration, run
ALTER TABLE events ADD COLUMN custom_metadata JSON;to migrate existing database tables.
これはまだいい方で、リリースに書かれないものが大半です2。
ADK のバージョンを上げて動かしたときに、/run呼び出しで落ちます。
SELECT文に指定したカラムがテーブルになくて、エラーを送出していました。
samples の migrate_session_db に示されていた
どうやら ADK Python 開発陣は samples で示しているようです(合わせてリリースノートにも毎回書いてくれ!)
https://github.com/google/adk-python/tree/v1.20.0/contributing/samples/migrate_session_db
This example demonstrates how to upgrade a session database created with an older version of ADK to be compatible with the current version.
これをコミットされているdnd_sessions.db(SQLite)で動かします。
% python -V Python 3.13.8 % adk --version adk, version 1.20.0 % alembic --version # google-adk の依存で入ります alembic 1.17.2
SQLite を例に動作確認
v1.19.0 で入った SqliteSessionService の影響か、少し変えました
Add SqliteSessionService and a migration script to migrate existing DB using DatabaseSessionService to SqliteSessionService
+from google.adk.sessions.sqlite_session_service import SqliteSessionService async def main(): - session_service = DatabaseSessionService('sqlite:///./sessions.db') + session_service = SqliteSessionService('./sessions.db')
% cp dnd_sessions.db sessions.db.old % curl -fsSL https://raw.githubusercontent.com/google/adk-python/refs/tags/v1.20.0/scripts/db_migration.sh | sh -s -- "sqlite:///$(pwd)/sessions.db.old" "google.adk.sessions.database_session_service" % python -m google.adk.sessions.migrate_from_sqlalchemy_sqlite --source_db_path ./sessions.db.old --dest_db_path ./sessions.db % rm sessions.db.old
これでpython main.pyが動くようになります
(Gemini API のキーを環境変数GEMINI_API_KEY(またはGOOGLE_API_KEY)に指定しましょう)
sessions.db.oldにdb_migration.shを適用する前にpython -m google.adk.sessions.migrate_from_sqlalchemy_sqliteするのは
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: events.usage_metadata
となりました。
まずdb_migration.sh(alembic)でテーブル定義を変えて、その後 SQLite 向けに変換という手順になるようです。
終わりに
ADK Python のマイナーバージョンアップでテーブル定義が変わっている場合には、migrate_session_db samples で示されていたdb_migration.shの出番です。
https://github.com/google/adk-python/blob/v1.20.0/scripts/db_migration.sh
DatabaseSessionServiceで MySQL や PostgreSQL を使っている場合にもdb_migration.shが適用できるのかは未検証です(宿題事項)
P.S. adk migrate session?
記事を書き上げた後で気づきました。
ただ、ADK v1.20.0 にはまだ入っていなさそうです
% uvx --from google-adk adk migrate session --help Usage: adk [OPTIONS] COMMAND [ARGS]... Try 'adk --help' for help. Error: No such command 'migrate'.