You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sessions.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -164,6 +164,64 @@ result2 = await Runner.run(
164
164
)
165
165
```
166
166
167
+
### SQLAlchemy-powered sessions
168
+
169
+
For more advanced use cases, you can use a SQLAlchemy-powered session backend. This allows you to use any database supported by SQLAlchemy (PostgreSQL, MySQL, SQLite, etc.) for session storage.
170
+
171
+
**Example 1: Using `from_url` with in-memory SQLite**
172
+
173
+
This is the simplest way to get started, ideal for development and testing.
174
+
175
+
```python
176
+
import asyncio
177
+
from agents import Agent, Runner
178
+
from agents.extensions.memory.sqlalchemy_session import SQLAlchemySession
179
+
180
+
asyncdefmain():
181
+
agent = Agent("Assistant")
182
+
session = SQLAlchemySession.from_url(
183
+
"user-123",
184
+
url="sqlite+aiosqlite:///:memory:",
185
+
create_tables=True, # Auto-create tables for the demo
186
+
)
187
+
188
+
result =await Runner.run(agent, "Hello", session=session)
189
+
190
+
if__name__=="__main__":
191
+
asyncio.run(main())
192
+
```
193
+
194
+
**Example 2: Using an existing SQLAlchemy engine**
195
+
196
+
In a production application, you likely already have a SQLAlchemy `AsyncEngine` instance. You can pass it directly to the session.
197
+
198
+
```python
199
+
import asyncio
200
+
from agents import Agent, Runner
201
+
from agents.extensions.memory.sqlalchemy_session import SQLAlchemySession
202
+
from sqlalchemy.ext.asyncio import create_async_engine
203
+
204
+
asyncdefmain():
205
+
# In your application, you would use your existing engine
0 commit comments