{"record":{"id":"678c84d4a8bc51cc","repo":"mem0ai/mem0","slug":"transformers-package-is-required-for-huggingfacere","errorCode":null,"errorMessage":"transformers package is required for HuggingFaceReranker. Install with: pip install transformers torch","messagePattern":"transformers package is required for HuggingFaceReranker\\. Install with: pip install transformers torch","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"mem0/reranker/huggingface_reranker.py","lineNumber":31,"sourceCode":"    TRANSFORMERS_AVAILABLE = True\nexcept ImportError:\n    TRANSFORMERS_AVAILABLE = False\n\nlogger = logging.getLogger(__name__)\n\n\nclass HuggingFaceReranker(BaseReranker):\n    \"\"\"HuggingFace Transformers based reranker implementation.\"\"\"\n\n    def __init__(self, config: Union[BaseRerankerConfig, HuggingFaceRerankerConfig, Dict]):\n        \"\"\"\n        Initialize HuggingFace reranker.\n\n        Args:\n            config: Configuration object with reranker parameters\n        \"\"\"\n        if not TRANSFORMERS_AVAILABLE:\n            raise ImportError(\"transformers package is required for HuggingFaceReranker. Install with: pip install transformers torch\")\n\n        # Convert to HuggingFaceRerankerConfig if needed\n        if isinstance(config, dict):\n            config = HuggingFaceRerankerConfig(**config)\n        elif isinstance(config, BaseRerankerConfig) and not isinstance(config, HuggingFaceRerankerConfig):\n            # Convert BaseRerankerConfig to HuggingFaceRerankerConfig with defaults\n            config = HuggingFaceRerankerConfig(\n                provider=getattr(config, 'provider', 'huggingface'),\n                model=getattr(config, 'model', 'BAAI/bge-reranker-base'),\n                api_key=getattr(config, 'api_key', None),\n                top_k=getattr(config, 'top_k', None),\n                device=None,  # Will auto-detect\n                batch_size=32,  # Default\n                max_length=512,  # Default\n                normalize=True,  # Default\n            )\n\n        self.config = config","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/reranker/huggingface_reranker.py#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["pip install transformers torch (or a mem0 extra that includes them, if provided)","If you cannot afford the heavy deps, switch to a hosted reranker (cohere) or drop reranking","Pin compatible versions: match torch to your CUDA/CPU runtime and let transformers follow","For CPU-only, use a pip index for CPU torch to keep images small"],"exampleFix":"# before\nconfig = {\"reranker\": {\"provider\": \"huggingface\", \"config\": {\"model\": \"BAAI/bge-reranker-base\"}}}\nmemory = Memory.from_config(config)  # ImportError\n\n# after\n# pip install transformers torch\nconfig = {\"reranker\": {\"provider\": \"huggingface\", \"config\": {\"model\": \"BAAI/bge-reranker-base\", \"device\": \"cpu\"}}}\nmemory = Memory.from_config(config)","handlingStrategy":"validation","validationCode":"import importlib.util\nmissing = [p for p in (\"transformers\", \"torch\") if importlib.util.find_spec(p) is None]\nif missing:\n    raise RuntimeError(f\"huggingface reranker needs: {missing}\")","typeGuard":null,"tryCatchPattern":"try:\n    reranker = HuggingFaceReranker(cfg)\nexcept ImportError:\n    reranker = None  # or switch provider: CohereReranker etc.","preventionTips":["Install heavy ML deps in dedicated images; verify with a smoke test that loads the model","Pre-download reranker weights at build time to avoid first-request stalls","Have a lighter fallback reranker when running on constrained infra"],"tags":["reranker","huggingface","transformers","dependency","import-error"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}