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/index.md
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -206,6 +206,7 @@ Use this table to pick a starting point before reading the detailed examples bel
206
206
|`AsyncSQLiteSession`| Async SQLite with `aiosqlite`| Extension backend with async driver support |
207
207
|`RedisSession`| Shared memory across workers/services | Good for low-latency distributed deployments |
208
208
|`SQLAlchemySession`| Production apps with existing databases | Works with SQLAlchemy-supported databases |
209
+
|`MongoDBSession`| Apps already using MongoDB or needing multi-process storage | Async pymongo; atomic sequence counter for ordering |
209
210
|`DaprSession`| Cloud-native deployments with Dapr sidecars | Supports multiple state stores plus TTL and consistency controls |
210
211
|`OpenAIConversationsSession`| Server-managed storage in OpenAI | OpenAI Conversations API-backed history |
211
212
|`OpenAIResponsesCompactionSession`| Long conversations with automatic compaction | Wrapper around another session backend |
@@ -416,6 +417,38 @@ Notes:
416
417
- See [`examples/memory/dapr_session_example.py`](https://github.com/openai/openai-agents-python/tree/main/examples/memory/dapr_session_example.py) for a full setup walkthrough, including local components and troubleshooting.
417
418
418
419
420
+
### MongoDB sessions
421
+
422
+
Use `MongoDBSession` for applications that already use MongoDB or need horizontally-scalable, multi-process session storage.
423
+
424
+
```bash
425
+
pip install openai-agents[mongodb]
426
+
```
427
+
428
+
```python
429
+
from agents import Agent, Runner
430
+
from agents.extensions.memory import MongoDBSession
431
+
432
+
agent = Agent(name="Assistant")
433
+
434
+
# Create from URI — owns the client and closes it when session.close() is called
435
+
session = MongoDBSession.from_uri(
436
+
"user-123",
437
+
uri="mongodb://localhost:27017",
438
+
database="agents",
439
+
)
440
+
result =await Runner.run(agent, "Hello", session=session)
441
+
print(result.final_output)
442
+
await session.close()
443
+
```
444
+
445
+
Notes:
446
+
447
+
-`from_uri(...)` creates and owns the `AsyncMongoClient` and closes it on `session.close()`. If your application already manages a client, construct `MongoDBSession(...)` directly with `client=...`; in that case `session.close()` is a no-op and lifecycle stays with the caller.
448
+
- Connect to [MongoDB Atlas](https://www.mongodb.com/products/platform) by passing an `mongodb+srv://user:password@cluster.example.mongodb.net` URI to `from_uri(...)` with no other changes.
449
+
- Two collections are used and both names are configurable via `sessions_collection=` (default `agent_sessions`) and `messages_collection=` (default `agent_messages`). Indexes are created automatically on first use. Each message document carries a monotonically increasing `seq` counter that preserves ordering across concurrent writers and processes.
450
+
- Use `await session.ping()` to verify connectivity before your first run.
451
+
419
452
### Advanced SQLite sessions
420
453
421
454
Enhanced SQLite sessions with conversation branching, usage analytics, and structured queries:
@@ -488,6 +521,7 @@ Use meaningful session IDs that help you organize conversations:
488
521
- Use async SQLite (`AsyncSQLiteSession("session_id", db_path="...")`) when you need an `aiosqlite`-based implementation
489
522
- Use Redis-backed sessions (`RedisSession.from_url("session_id", url="redis://...")`) for shared, low-latency session memory
490
523
- Use SQLAlchemy-powered sessions (`SQLAlchemySession("session_id", engine=engine, create_tables=True)`) for production systems with existing databases supported by SQLAlchemy
524
+
- Use MongoDB sessions (`MongoDBSession.from_uri("session_id", uri="mongodb://localhost:27017")`) for applications already using MongoDB or needing multi-process, horizontally-scalable session storage
491
525
- Use Dapr state store sessions (`DaprSession.from_address("session_id", state_store_name="statestore", dapr_address="localhost:50001")`) for production cloud-native deployments with support for 30+ database backends with built-in telemetry, tracing, and data isolation
492
526
- Use OpenAI-hosted storage (`OpenAIConversationsSession()`) when you prefer to store history in the OpenAI Conversations API
493
527
- Use encrypted sessions (`EncryptedSession(session_id, underlying_session, encryption_key)`) to wrap any session with transparent encryption and TTL-based expiration
@@ -667,6 +701,7 @@ For detailed API documentation, see:
667
701
-[`AsyncSQLiteSession`][agents.extensions.memory.async_sqlite_session.AsyncSQLiteSession] - Async SQLite implementation based on `aiosqlite`
0 commit comments