{"record":{"id":"e308c7cbe5e17101","repo":"mem0ai/mem0","slug":"the-pymongo-library-is-required-please-install","errorCode":null,"errorMessage":"The 'pymongo' library is required. Please install it using 'pip install pymongo'.","messagePattern":"The 'pymongo' library is required\\. Please install it using 'pip install pymongo'\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"critical","filePath":"mem0/vector_stores/mongodb.py","lineNumber":13,"sourceCode":"import logging\nfrom importlib.metadata import version\nfrom typing import Any, Dict, List, Optional\n\nfrom pydantic import BaseModel\n\ntry:\n    from pymongo import MongoClient\n    from pymongo.driver_info import DriverInfo\n    from pymongo.errors import PyMongoError\n    from pymongo.operations import SearchIndexModel\nexcept ImportError:\n    raise ImportError(\"The 'pymongo' library is required. Please install it using 'pip install pymongo'.\")\n\nfrom mem0.vector_stores.base import VectorStoreBase\n\nlogger = logging.getLogger(__name__)\n\n_DRIVER_METADATA = DriverInfo(name=\"Mem0\", version=version(\"mem0ai\"))\n\nclass OutputData(BaseModel):\n    id: Optional[str]\n    score: Optional[float]\n    payload: Optional[dict]\n\n\nclass MongoDB(VectorStoreBase):\n    VECTOR_TYPE = \"vector\"\n    SIMILARITY_METRIC = \"cosine\"\n\n    def __init__(self, db_name: str, collection_name: str, embedding_model_dims: int, mongo_uri: str):","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/vector_stores/mongodb.py#L1-L31","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["pip install pymongo (4.x or later, since driver_info is imported)","If a pymongo 3.x pin exists from another dependency, upgrade it or relax the pin so 4.x resolves","For repeatable envs, add pymongo to the project's requirements/pyproject dependency group for vector stores"],"exampleFix":"# before\npip install mem0ai\n# config: provider \"mongodb\" -> ImportError\n\n# after\npip install \"pymongo>=4.9\"","handlingStrategy":"validation","validationCode":"try:\n    import pymongo  # noqa\n    from pymongo.driver_info import DriverInfo  # noqa\nexcept ImportError:\n    raise SystemExit(\"pymongo>=4 required for the mongodb vector store\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Install backend extras at image build time, not lazily","Add an import smoke test for the configured provider to CI","Pin pymongo>=4.9 to guarantee driver_info exists"],"tags":["mongodb","import-error","dependencies","vector-store"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}