{"record":{"id":"94d96c2e093569e2","repo":"run-llama/llama_index","slug":"cannot-import-sentence-transformers-or-torch-packa","errorCode":null,"errorMessage":"Cannot import sentence-transformers or torch package,","messagePattern":"Cannot import sentence-transformers or torch package,","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/postprocessor/sbert_rerank.py","lineNumber":40,"sourceCode":"    )\n    trust_remote_code: bool = Field(\n        default=False,\n        description=\"Whether to trust remote code.\",\n    )\n    _model: Any = PrivateAttr()\n\n    def __init__(\n        self,\n        top_n: int = 2,\n        model: str = \"cross-encoder/stsb-distilroberta-base\",\n        device: Optional[str] = None,\n        keep_retrieval_score: bool = False,\n        trust_remote_code: bool = True,\n    ):\n        try:\n            from sentence_transformers import CrossEncoder  # pants: no-infer-dep\n        except ImportError:\n            raise ImportError(\n                \"Cannot import sentence-transformers or torch package,\",\n                \"please `pip install torch sentence-transformers`\",\n            )\n        device = infer_torch_device() if device is None else device\n        super().__init__(\n            top_n=top_n,\n            model=model,\n            device=device,\n            keep_retrieval_score=keep_retrieval_score,\n        )\n        self._model = CrossEncoder(\n            model,\n            max_length=DEFAULT_SENTENCE_TRANSFORMER_MAX_LENGTH,\n            device=device,\n            trust_remote_code=trust_remote_code,\n        )\n\n    @classmethod","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/postprocessor/sbert_rerank.py#L22-L58","documentation":"Error \"Cannot import sentence-transformers or torch package,\" thrown in run-llama/llama_index.","triggerScenarios":"Thrown at llama-index-core/llama_index/core/postprocessor/sbert_rerank.py:40 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Install the packages: `pip install sentence-transformers torch`.","Use a different reranker that does not require sentence-transformers."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}