From 9c9b3d54e2944037b09c448c167e635583991a60 Mon Sep 17 00:00:00 2001 From: Soumil Rathi Date: Thu, 2 Apr 2026 11:04:38 -0700 Subject: [PATCH] feat(oss): make spaCy an optional dependency Move spacy from core dependencies to optional [nlp] extra. The system gracefully degrades without spaCy: - Lemmatization falls back to raw text - Entity extraction returns empty (no entity boost) - BM25 keyword search still works on raw text - Semantic search and additive scoring unaffected Install with NLP support: pip install mem0ai[nlp] Co-Authored-By: Claude Opus 4.6 (1M context) --- README.md | 7 +++++++ mem0/utils/spacy_models.py | 9 +++++++-- pyproject.toml | 4 +++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 77d957226..0d70bf230 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,13 @@ Install the sdk via pip: pip install mem0ai ``` +For enhanced hybrid search with BM25 keyword matching and entity extraction, install with NLP support: + +```bash +pip install mem0ai[nlp] +python -m spacy download en_core_web_sm +``` + Install sdk via npm: ```bash npm install mem0ai diff --git a/mem0/utils/spacy_models.py b/mem0/utils/spacy_models.py index f5dfb263b..588211070 100644 --- a/mem0/utils/spacy_models.py +++ b/mem0/utils/spacy_models.py @@ -19,8 +19,13 @@ _lock = threading.Lock() def _ensure_model_available(): - """Download en_core_web_sm if not already installed. Does not load the model.""" - import spacy + """Download en_core_web_sm if spaCy is installed but model is missing.""" + try: + import spacy + except ImportError: + raise ImportError( + "spaCy is not installed. Install it with: pip install mem0ai[nlp]" + ) if not spacy.util.is_package("en_core_web_sm"): logger.info("Downloading spaCy model en_core_web_sm...") diff --git a/pyproject.toml b/pyproject.toml index 25e28c43f..a7ec8b440 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,10 +21,12 @@ dependencies = [ "pytz>=2024.1", "sqlalchemy>=2.0.31", "protobuf>=5.29.0,<6.0.0", - "spacy>=3.7.0", ] [project.optional-dependencies] +nlp = [ + "spacy>=3.7.0", +] graph = [ "langchain-neo4j>=0.4.0", "langchain-aws>=0.2.23",