mem0ai/mem0 · error · ImportError
sentence-transformers package is required for SentenceTransf
Error message
sentence-transformers package is required for SentenceTransformerReranker. Install with: pip install sentence-transformers
What it means
SentenceTransformerReranker raises ImportError when sentence-transformers is missing (guarded by SENTENCE_TRANSFORMERS_AVAILABLE). Like the HuggingFace reranker it runs models locally and needs its own optional package, distinct from plain transformers. Construction fails at Memory.from_config() time with the exact pip command in the message.
Source
Thrown at mem0/reranker/sentence_transformer_reranker.py:32
SENTENCE_TRANSFORMERS_AVAILABLE = True
except ImportError:
SENTENCE_TRANSFORMERS_AVAILABLE = False
logger = logging.getLogger(__name__)
class SentenceTransformerReranker(BaseReranker):
"""Sentence Transformer based reranker implementation."""
def __init__(self, config: Union[BaseRerankerConfig, SentenceTransformerRerankerConfig, Dict]):
"""
Initialize Sentence Transformer reranker.
Args:
config: Configuration object with reranker parameters
"""
if not SENTENCE_TRANSFORMERS_AVAILABLE:
raise ImportError("sentence-transformers package is required for SentenceTransformerReranker. Install with: pip install sentence-transformers")
# Convert to SentenceTransformerRerankerConfig if needed
if isinstance(config, dict):
config = SentenceTransformerRerankerConfig(**config)
elif isinstance(config, BaseRerankerConfig) and not isinstance(config, SentenceTransformerRerankerConfig):
# Convert BaseRerankerConfig to SentenceTransformerRerankerConfig with defaults
config = SentenceTransformerRerankerConfig(
provider=getattr(config, 'provider', 'sentence_transformer'),
model=getattr(config, 'model', 'cross-encoder/ms-marco-MiniLM-L-6-v2'),
api_key=getattr(config, 'api_key', None),
top_k=getattr(config, 'top_k', None),
device=None, # Will auto-detect
batch_size=32, # Default
show_progress_bar=False, # Default
)
self.config = config
self.model = CrossEncoder(self.config.model, device=self.config.device)View on GitHub (pinned to 001c235229)
Solutions
- pip install sentence-transformers (it will pull torch if missing)
- Or use a lighter/hosted reranker provider if the dependency is too heavy
- Verify in the runtime interpreter: python -c "import sentence_transformers"
Example fix
# before
config = {"reranker": {"provider": "sentence_transformer", "config": {"model": "cross-encoder/ms-marco-MiniLM-L-6-v2"}}}
# ImportError
# after
# pip install sentence-transformers
config = {"reranker": {"provider": "sentence_transformer", "config": {"model": "cross-encoder/ms-marco-MiniLM-L-6-v2", "device": "cpu"}}} Defensive patterns
Strategy: validation
Validate before calling
import importlib.util
if importlib.util.find_spec("sentence_transformers") is None:
raise RuntimeError("install sentence-transformers before enabling this reranker") Try / catch
try:
reranker = SentenceTransformerReranker(cfg)
except ImportError:
reranker = None # or fall back to another provider Prevention
- sentence-transformers is separate from transformers; install both as needed
- Lock model + package versions together for reproducible reranking
- Warm-load the cross-encoder at startup, not on first query
When it happens
Trigger: Configuring reranker={'provider':'sentence_transformer', ...} without `pip install sentence-transformers`; environments that installed transformers+torch but not the sentence-transformers package (they are separate); minimal deployment images.
Common situations: Assuming transformers covers sentence-transformers (it does not — it is an extra package); moving from API-based rerankers to local cross-encoders to cut costs; dependency pruning scripts removing 'unused' packages.
Related errors
- cohere package is required for CohereReranker. Install with:
- transformers package is required for HuggingFaceReranker. In
- zeroentropy package is required for ZeroEntropyReranker. Ins
- The 'chromadb' library is required. Please install it using
- langchain-core is required to pass a custom LLM to procedura
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/f56026b0d5ee9be7.
Report an issue: GitHub.