Skip to main content

Overview

AsyncMongoStorage provides an asynchronous MongoDB-based storage backend. Supports both Motor (legacy) and PyMongo async (recommended) clients. Ideal for document-based applications, flexible schemas, and horizontally scalable deployments.

Install

Install the MongoDB storage optional dependency group:
uv pip install "upsonic[mongo-storage]"

Basic Usage

import asyncio
from upsonic import Agent, Task
from upsonic.storage.memory import Memory
from upsonic.storage.mongo import AsyncMongoStorage

async def main():
    storage = AsyncMongoStorage(
        db_url="mongodb://localhost:27017",
        db_name="agent_memory"
    )

    memory = Memory(
        storage=storage,
        session_id="session_001",
        user_id="user_123",
        full_session_memory=True,
        summary_memory=True,
        user_analysis_memory=True,
        model="anthropic/claude-sonnet-4-5"
    )

    agent = Agent("anthropic/claude-sonnet-4-5", memory=memory)

    result1 = await agent.do_async(Task("My name is Alice"))
    result2 = await agent.do_async(Task("What's my name?"))
    print(result2)  # "Your name is Alice"

asyncio.run(main())

Parameters

ParameterTypeDefaultDescription
db_urlstrNoneMongoDB connection URL
db_namestr"upsonic"Database name to use
db_clientAsyncIOMotorClient or AsyncMongoClientNonePre-configured async MongoDB client
session_collectionstrNoneCustom name for the session collection
user_memory_collectionstrNoneCustom name for the user memory collection
knowledge_collectionstrNoneCustom name for the knowledge registry collection (used by KnowledgeBase)
idstrNoneUnique identifier for this storage instance
When both Motor and PyMongo async are available, PyMongo async is preferred. Install using uv pip install -U 'pymongo>=4.9' (recommended) or uv pip install -U motor (legacy).