run-llama/llama_index · error · ImportError

Cannot import sentence-transformers or torch package,

Error message

Cannot import sentence-transformers or torch package,

What it means

Error "Cannot import sentence-transformers or torch package," thrown in run-llama/llama_index.

Source

Thrown at llama-index-core/llama_index/core/postprocessor/sbert_rerank.py:40

    )
    trust_remote_code: bool = Field(
        default=False,
        description="Whether to trust remote code.",
    )
    _model: Any = PrivateAttr()

    def __init__(
        self,
        top_n: int = 2,
        model: str = "cross-encoder/stsb-distilroberta-base",
        device: Optional[str] = None,
        keep_retrieval_score: bool = False,
        trust_remote_code: bool = True,
    ):
        try:
            from sentence_transformers import CrossEncoder  # pants: no-infer-dep
        except ImportError:
            raise ImportError(
                "Cannot import sentence-transformers or torch package,",
                "please `pip install torch sentence-transformers`",
            )
        device = infer_torch_device() if device is None else device
        super().__init__(
            top_n=top_n,
            model=model,
            device=device,
            keep_retrieval_score=keep_retrieval_score,
        )
        self._model = CrossEncoder(
            model,
            max_length=DEFAULT_SENTENCE_TRANSFORMER_MAX_LENGTH,
            device=device,
            trust_remote_code=trust_remote_code,
        )

    @classmethod

View on GitHub (pinned to afd0fef371)

Solutions

  1. Install the packages: `pip install sentence-transformers torch`.
  2. Use a different reranker that does not require sentence-transformers.

When it happens

Trigger: Thrown at llama-index-core/llama_index/core/postprocessor/sbert_rerank.py:40 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/94d96c2e093569e2. Report an issue: GitHub.