Skip to content

Commit 1821bf8

Browse files
authored
docs: add MongoDB session documentation (#3015)
1 parent 071e2b6 commit 1821bf8

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `MongoDBSession`
2+
3+
::: agents.extensions.memory.mongodb_session.MongoDBSession

docs/sessions/index.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ Use this table to pick a starting point before reading the detailed examples bel
206206
| `AsyncSQLiteSession` | Async SQLite with `aiosqlite` | Extension backend with async driver support |
207207
| `RedisSession` | Shared memory across workers/services | Good for low-latency distributed deployments |
208208
| `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 |
209210
| `DaprSession` | Cloud-native deployments with Dapr sidecars | Supports multiple state stores plus TTL and consistency controls |
210211
| `OpenAIConversationsSession` | Server-managed storage in OpenAI | OpenAI Conversations API-backed history |
211212
| `OpenAIResponsesCompactionSession` | Long conversations with automatic compaction | Wrapper around another session backend |
@@ -416,6 +417,38 @@ Notes:
416417
- 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.
417418

418419

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+
419452
### Advanced SQLite sessions
420453

421454
Enhanced SQLite sessions with conversation branching, usage analytics, and structured queries:
@@ -488,6 +521,7 @@ Use meaningful session IDs that help you organize conversations:
488521
- Use async SQLite (`AsyncSQLiteSession("session_id", db_path="...")`) when you need an `aiosqlite`-based implementation
489522
- Use Redis-backed sessions (`RedisSession.from_url("session_id", url="redis://...")`) for shared, low-latency session memory
490523
- 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
491525
- 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
492526
- Use OpenAI-hosted storage (`OpenAIConversationsSession()`) when you prefer to store history in the OpenAI Conversations API
493527
- 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:
667701
- [`AsyncSQLiteSession`][agents.extensions.memory.async_sqlite_session.AsyncSQLiteSession] - Async SQLite implementation based on `aiosqlite`
668702
- [`RedisSession`][agents.extensions.memory.redis_session.RedisSession] - Redis-backed session implementation
669703
- [`SQLAlchemySession`][agents.extensions.memory.sqlalchemy_session.SQLAlchemySession] - SQLAlchemy-powered implementation
704+
- [`MongoDBSession`][agents.extensions.memory.mongodb_session.MongoDBSession] - MongoDB-backed session implementation
670705
- [`DaprSession`][agents.extensions.memory.dapr_session.DaprSession] - Dapr state store implementation
671706
- [`AdvancedSQLiteSession`][agents.extensions.memory.advanced_sqlite_session.AdvancedSQLiteSession] - Enhanced SQLite with branching and analytics
672707
- [`EncryptedSession`][agents.extensions.memory.encrypt_session.EncryptedSession] - Encrypted wrapper for any session

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ plugins:
190190
- SQLAlchemySession: ref/extensions/memory/sqlalchemy_session.md
191191
- Async SQLite session: ref/extensions/memory/async_sqlite_session.md
192192
- RedisSession: ref/extensions/memory/redis_session.md
193+
- MongoDBSession: ref/extensions/memory/mongodb_session.md
193194
- DaprSession: ref/extensions/memory/dapr_session.md
194195
- EncryptedSession: ref/extensions/memory/encrypt_session.md
195196
- AdvancedSQLiteSession: ref/extensions/memory/advanced_sqlite_session.md

0 commit comments

Comments
 (0)