tirth8205/code-review-graph · error · ImportError

sentence-transformers not installed. Run: pip install code-r

Error message

sentence-transformers not installed. Run: pip install code-review-graph[embeddings]

What it means

This ImportError is raised by code-review-graph's local embedding provider when the optional `sentence-transformers` dependency is missing. The lazy import inside _get_model fails, and the library re-raises with an actionable install hint instead of the raw ImportError. A companion comment notes that failed construction deliberately leaves the provider and cache empty so a later retry can succeed after installing.

Source

Thrown at code_review_graph/embeddings.py:151

            if self._model is not None:
                return self._model
            cached = _MODEL_CACHE.get(self._model_name)
            if cached is not None:
                self._model = cached
                return self._model

            try:
                from sentence_transformers import SentenceTransformer
                # Check environment variable, default to False to prevent RCE
                _rce_val = os.environ.get("CRG_ALLOW_REMOTE_CODE", "0")
                allow_remote_code = _rce_val.lower() in ("1", "true", "yes")

                model = SentenceTransformer(
                    self._model_name,
                    trust_remote_code=allow_remote_code,
                )
            except ImportError:
                raise ImportError(
                    "sentence-transformers not installed. "
                    "Run: pip install code-review-graph[embeddings]"
                )

            # Publish only a fully constructed model. Failed attempts leave
            # both the provider and shared cache empty so a waiter can retry.
            _MODEL_CACHE[self._model_name] = model
            self._model = model
        return self._model

    def embed(self, texts: list[str]) -> list[list[float]]:
        model = self._get_model()
        vectors = model.encode(texts, show_progress_bar=False)
        return [v.tolist() for v in vectors]

    def embed_query(self, text: str) -> list[float]:
        return self.embed([text])[0]

View on GitHub (pinned to b58668751a)

Solutions

  1. pip install 'code-review-graph[embeddings]'
  2. Verify with: python -c "import sentence_transformers"
  3. Pin a CPU torch wheel first if on a constrained runner: pip install torch --index-url https://download.pytorch.org/whl/cpu, then reinstall the extra

Example fix

# before
pip install code-review-graph
# after
pip install 'code-review-graph[embeddings]'
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util, sys

if importlib.util.find_spec("sentence_transformers") is None:
    sys.exit("Install first: pip install 'code-review-graph[embeddings]'")

Type guard

def has_local_embeddings() -> bool:
    return importlib.util.find_spec("sentence_transformers") is not None

Try / catch

try:
    provider.embed(texts)
except ImportError as e:
    if "sentence-transformers" in str(e):
        print("pip install 'code-review-graph[embeddings]'")
    raise

Prevention

When it happens

Trigger: Calling prewarm_local_embeddings(), embed(), or dimension() on the local SentenceTransformer provider without the [embeddings] extra installed. Any test constructing the local provider (e.g. test_model_cache_remains_scoped_by_model_name) will also hit it in an environment lacking the package.

Common situations: Installing the base package without extras (`pip install code-review-graph` instead of `code-review-graph[embeddings]`), CI environments that only install core deps, or a venv recreated without the extras flag.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


AI-assisted analysis of tirth8205/code-review-graph@b58668751a (2026-08-28). Data as JSON: /api/errors/437c7c6a84f6a423. Report an issue: GitHub.