mem0ai/mem0 · error · ImportError

transformers package is required for HuggingFaceReranker. In

Error message

transformers package is required for HuggingFaceReranker. Install with: pip install transformers torch

What it means

HuggingFaceReranker raises ImportError when transformers (and by implication torch) are not installed; the import is guarded by TRANSFORMERS_AVAILABLE. This reranker runs cross-encoder models locally, so it carries heavy optional dependencies that core mem0 does not install by default. The class imports fine; only construction fails.

Source

Thrown at mem0/reranker/huggingface_reranker.py:31

    TRANSFORMERS_AVAILABLE = True
except ImportError:
    TRANSFORMERS_AVAILABLE = False

logger = logging.getLogger(__name__)


class HuggingFaceReranker(BaseReranker):
    """HuggingFace Transformers based reranker implementation."""

    def __init__(self, config: Union[BaseRerankerConfig, HuggingFaceRerankerConfig, Dict]):
        """
        Initialize HuggingFace reranker.

        Args:
            config: Configuration object with reranker parameters
        """
        if not TRANSFORMERS_AVAILABLE:
            raise ImportError("transformers package is required for HuggingFaceReranker. Install with: pip install transformers torch")

        # Convert to HuggingFaceRerankerConfig if needed
        if isinstance(config, dict):
            config = HuggingFaceRerankerConfig(**config)
        elif isinstance(config, BaseRerankerConfig) and not isinstance(config, HuggingFaceRerankerConfig):
            # Convert BaseRerankerConfig to HuggingFaceRerankerConfig with defaults
            config = HuggingFaceRerankerConfig(
                provider=getattr(config, 'provider', 'huggingface'),
                model=getattr(config, 'model', 'BAAI/bge-reranker-base'),
                api_key=getattr(config, 'api_key', None),
                top_k=getattr(config, 'top_k', None),
                device=None,  # Will auto-detect
                batch_size=32,  # Default
                max_length=512,  # Default
                normalize=True,  # Default
            )

        self.config = config

View on GitHub (pinned to 001c235229)

Solutions

  1. pip install transformers torch (or a mem0 extra that includes them, if provided)
  2. If you cannot afford the heavy deps, switch to a hosted reranker (cohere) or drop reranking
  3. Pin compatible versions: match torch to your CUDA/CPU runtime and let transformers follow
  4. For CPU-only, use a pip index for CPU torch to keep images small

Example fix

# before
config = {"reranker": {"provider": "huggingface", "config": {"model": "BAAI/bge-reranker-base"}}}
memory = Memory.from_config(config)  # ImportError

# after
# pip install transformers torch
config = {"reranker": {"provider": "huggingface", "config": {"model": "BAAI/bge-reranker-base", "device": "cpu"}}}
memory = Memory.from_config(config)
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util
missing = [p for p in ("transformers", "torch") if importlib.util.find_spec(p) is None]
if missing:
    raise RuntimeError(f"huggingface reranker needs: {missing}")

Try / catch

try:
    reranker = HuggingFaceReranker(cfg)
except ImportError:
    reranker = None  # or switch provider: CohereReranker etc.

Prevention

When it happens

Trigger: Configuring reranker={'provider':'huggingface', ...} without `pip install transformers torch`; slim Docker images that exclude ML runtimes; CPU-only servers where torch was deliberately omitted for size.

Common situations: Wanting free local reranking without an API key and hitting the missing heavy deps; CI pipelines that time out or bloat after adding the reranker config; conflicts between an existing transformers/torch version and mem0's requirement.

Related errors


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