SQLAlchemySession
Bases:SessionABC
SQLAlchemy implementation of :pyclass:agents.memory.session.Session.
Source code insrc/agents/extensions/memory/sqlalchemy_session.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 | |
engineproperty
Access the underlying SQLAlchemy AsyncEngine.
This property provides direct access to the engine for advanced use cases,such as checking connection pool status, configuring engine settings,or manually disposing the engine when needed.
Returns:
| Name | Type | Description |
|---|---|---|
AsyncEngine | AsyncEngine | The SQLAlchemy async engine instance. |
__init__
__init__(session_id:str,*,engine:AsyncEngine,create_tables:bool=False,sessions_table:str="agent_sessions",messages_table:str="agent_messages",)Initializes a new SQLAlchemySession.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Unique identifier for the conversation. | required |
engine | AsyncEngine | A pre-configured SQLAlchemy async engine. The enginemust be created with an async driver (e.g., 'postgresql+asyncpg://','mysql+aiomysql://', or 'sqlite+aiosqlite://'). | required |
create_tables | bool | Whether to automatically create the requiredtables and indexes. Defaults to False for production use. Set to True fordevelopment and testing when migrations aren't used. | False |
sessions_table | str | Override the default table name for sessions if needed. | 'agent_sessions' |
messages_table | str | Override the default table name for messages if needed. | 'agent_messages' |
Source code insrc/agents/extensions/memory/sqlalchemy_session.py
from_urlclassmethod
from_url(session_id:str,*,url:str,engine_kwargs:dict[str,Any]|None=None,**kwargs:Any,)->SQLAlchemySessionCreate a session from a database URL string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Conversation ID. | required |
url | str | Any SQLAlchemy async URL, e.g. "postgresql+asyncpg://user:pass@host/db". | required |
engine_kwargs | dict[str,Any] | None | Additional keyword arguments forwarded tosqlalchemy.ext.asyncio.create_async_engine. | None |
**kwargs | Any | Additional keyword arguments forwarded to the main constructor(e.g., create_tables, custom table names, etc.). | {} |
Returns:
| Name | Type | Description |
|---|---|---|
SQLAlchemySession | SQLAlchemySession | An instance of SQLAlchemySession connected to the specified database. |
Source code insrc/agents/extensions/memory/sqlalchemy_session.py
get_itemsasync
get_items(limit:int|None=None,)->list[TResponseInputItem]Retrieve the conversation history for this session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit | int | None | Maximum number of items to retrieve. If None, retrieves all items. When specified, returns the latest N items in chronological order. | None |
Returns:
| Type | Description |
|---|---|
list[TResponseInputItem] | List of input items representing the conversation history |
Source code insrc/agents/extensions/memory/sqlalchemy_session.py
add_itemsasync
add_items(items:list[TResponseInputItem])->NoneAdd new items to the conversation history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items | list[TResponseInputItem] | List of input items to add to the history | required |
Source code insrc/agents/extensions/memory/sqlalchemy_session.py
pop_itemasync
pop_item()->TResponseInputItem|NoneRemove and return the most recent item from the session.
Returns:
| Type | Description |
|---|---|
TResponseInputItem | None | The most recent item if it exists, None if the session is empty |
Source code insrc/agents/extensions/memory/sqlalchemy_session.py
clear_sessionasync
Clear all items for this session.