Movatterモバイル変換


[0]ホーム

URL:


Skip to content

SQLAlchemy Sessions

SQLAlchemySession uses SQLAlchemy to provide a production-ready session implementation, allowing you to use any database supported by SQLAlchemy (PostgreSQL, MySQL, SQLite, etc.) for session storage.

Installation

SQLAlchemy sessions require thesqlalchemy extra:

pipinstallopenai-agents[sqlalchemy]

Quick start

Using database URL

The simplest way to get started:

importasynciofromagentsimportAgent,Runnerfromagents.extensions.memoryimportSQLAlchemySessionasyncdefmain():agent=Agent("Assistant")# Create session using database URLsession=SQLAlchemySession.from_url("user-123",url="sqlite+aiosqlite:///:memory:",create_tables=True)result=awaitRunner.run(agent,"Hello",session=session)print(result.final_output)if__name__=="__main__":asyncio.run(main())

Using existing engine

For applications with existing SQLAlchemy engines:

importasynciofromagentsimportAgent,Runnerfromagents.extensions.memoryimportSQLAlchemySessionfromsqlalchemy.ext.asyncioimportcreate_async_engineasyncdefmain():# Create your database engineengine=create_async_engine("postgresql+asyncpg://user:pass@localhost/db")agent=Agent("Assistant")session=SQLAlchemySession("user-456",engine=engine,create_tables=True)result=awaitRunner.run(agent,"Hello",session=session)print(result.final_output)# Clean upawaitengine.dispose()if__name__=="__main__":asyncio.run(main())

API Reference


[8]ページ先頭

©2009-2025 Movatter.jp