nikkie-ftnextの日記

イベントレポートや読書メモを発信

Python 版 Agent Development Kit のマイナーバージョンアップに潜む、session 関係のテーブル定義の変更への対処は、samples の migrate_session_db に示されていました

はじめに

七尾百合子さん、お誕生日 266日目 おめでとうございます! nikkieです。

Google による Python 版 Agent Development Kit は現在、隔週でリリースされています。
まとめてバージョンを上げようとしたら、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 state dictionary, the list of all past events (event history), and references to associated artifacts.
It's the primary record of the interaction, managed by the SessionService.

セッションが保持するのは、state、過去のすべてのevents、関連する artifacts への参照。
SessionServiceによって管理されます。

SessionServiceには3つあります。
https://google.github.io/adk-docs/sessions/session/#sessionservice-implementations

  • InMemorySessionService
  • VertexAiSessionService
  • DatabaseSessionService

このうち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.dbSQLite)で動かします。

% 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.olddb_migration.shを適用する前にpython -m google.adk.sessions.migrate_from_sqlalchemy_sqliteするのは

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: events.usage_metadata

となりました。
まずdb_migration.shalembic)でテーブル定義を変えて、その後 SQLite 向けに変換という手順になるようです。

終わりに

ADK Python のマイナーバージョンアップでテーブル定義が変わっている場合には、migrate_session_db samples で示されていたdb_migration.shの出番です。
https://github.com/google/adk-python/blob/v1.20.0/scripts/db_migration.sh

DatabaseSessionServiceMySQLPostgreSQL を使っている場合にも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'.

  1. 参考
  2. おい