tirth8205/code-review-graph · error · ImportError

google-genai not installed. Run: pip install "code-review-gr

Error message

google-genai not installed. Run: pip install "code-review-graph[google-embeddings]"

What it means

The Google embedding provider's __init__ tries to construct a google-genai Client and catches ImportError, re-raising with a hint to install the [google-embeddings] extra. It means the optional google-genai SDK is absent from the environment, so no Google embeddings can be produced until installed and an API key supplied.

Source

Thrown at code_review_graph/embeddings.py:190

        model = self._get_model()
        if hasattr(model, "get_embedding_dimension"):
            return model.get_embedding_dimension()
        return model.get_sentence_embedding_dimension()

    @property
    def name(self) -> str:
        return f"local:{self._model_name}"


class GoogleEmbeddingProvider(EmbeddingProvider):
    def __init__(self, api_key: str, model: str = "gemini-embedding-001") -> None:
        try:
            from google import genai
            self._client = genai.Client(api_key=api_key)
            self.model = model
            self._dimension: int | None = None
        except ImportError:
            raise ImportError(
                "google-genai not installed. "
                "Run: pip install \"code-review-graph[google-embeddings]\""
            )

    def embed(self, texts: list[str]) -> list[list[float]]:
        batch_size = 100
        results = []
        for i in range(0, len(texts), batch_size):
            batch = texts[i:i + batch_size]
            response = self._call_with_retry(
                lambda b=batch: self._client.models.embed_content(
                    model=self.model,
                    contents=b,
                    config={"task_type": "RETRIEVAL_DOCUMENT"},
                )
            )
            results.extend([e.values for e in response.embeddings])
        if self._dimension is None and results:

View on GitHub (pinned to b58668751a)

Solutions

  1. pip install 'code-review-graph[google-embeddings]'
  2. Confirm the right SDK is present: python -c "from google import genai" (not google-generativeai)
  3. Ensure GOOGLE_API_KEY / the passed api_key is set so construction succeeds after install

Example fix

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

Strategy: validation

Validate before calling

import importlib.util

if importlib.util.find_spec("google.genai") is None:
    raise SystemExit("Run: pip install 'code-review-graph[google-embeddings]'")

Type guard

def google_provider_available() -> bool:
    return importlib.util.find_spec("google.genai") is not None

Try / catch

try:
    provider = GoogleProvider(api_key=...)
except ImportError as e:
    if "google-genai" in str(e):
        # fall back to another provider or install extra
        provider = LocalProvider()

Prevention

When it happens

Trigger: Instantiating the Google embeddings provider class (e.g. GoogleEmbeddingProvider(api_key=...)) in an environment where `from google import genai` fails.

Common situations: Base install without extras, or confusion between the legacy `google-generativeai` package and the newer `google-genai` SDK — having only the former installed still triggers this error.

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/72412a7c178657d3. Report an issue: GitHub.