mem0ai/mem0 · critical · ImportError

The 'pymongo' library is required. Please install it using '

Error message

The 'pymongo' library is required. Please install it using 'pip install pymongo'.

What it means

Module-level ImportError raised when mem0.vector_stores.mongodb is imported but the pymongo package (with its driver_info module, i.e. pymongo 4.x+) is not installed. Because it fires at import time, simply having the MongoDB backend configured causes the failure before any connection is attempted.

Source

Thrown at mem0/vector_stores/mongodb.py:13

import logging
from importlib.metadata import version
from typing import Any, Dict, List, Optional

from pydantic import BaseModel

try:
    from pymongo import MongoClient
    from pymongo.driver_info import DriverInfo
    from pymongo.errors import PyMongoError
    from pymongo.operations import SearchIndexModel
except ImportError:
    raise ImportError("The 'pymongo' library is required. Please install it using 'pip install pymongo'.")

from mem0.vector_stores.base import VectorStoreBase

logger = logging.getLogger(__name__)

_DRIVER_METADATA = DriverInfo(name="Mem0", version=version("mem0ai"))

class OutputData(BaseModel):
    id: Optional[str]
    score: Optional[float]
    payload: Optional[dict]


class MongoDB(VectorStoreBase):
    VECTOR_TYPE = "vector"
    SIMILARITY_METRIC = "cosine"

    def __init__(self, db_name: str, collection_name: str, embedding_model_dims: int, mongo_uri: str):

View on GitHub (pinned to 001c235229)

Solutions

  1. pip install pymongo (4.x or later, since driver_info is imported)
  2. If a pymongo 3.x pin exists from another dependency, upgrade it or relax the pin so 4.x resolves
  3. For repeatable envs, add pymongo to the project's requirements/pyproject dependency group for vector stores

Example fix

# before
pip install mem0ai
# config: provider "mongodb" -> ImportError

# after
pip install "pymongo>=4.9"
Defensive patterns

Strategy: validation

Validate before calling

try:
    import pymongo  # noqa
    from pymongo.driver_info import DriverInfo  # noqa
except ImportError:
    raise SystemExit("pymongo>=4 required for the mongodb vector store")

Prevention

When it happens

Trigger: Configuring vector_store.provider="mongodb" without having run pip install "pymongo>=4" ; installing an ancient pymongo (<4) that lacks pymongo.driver_info, which also trips this except clause; importing the module directly in a test without the extra installed.

Common situations: Using mem0ai extras (e.g. pip install mem0ai without [mongodb]-style extra) and switching the config to MongoDB; CI environments that only install base deps; a requirements pin on pymongo 3.x from another library.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/e308c7cbe5e17101. Report an issue: GitHub.