tirth8205/code-review-graph · error · ValueError

Missing required environment variable(s) for the OpenAI embe

Error message

Missing required environment variable(s) for the OpenAI embedding provider: {', '.join(missing)}.

What it means

The openai provider requires CRG_OPENAI_API_KEY, CRG_OPENAI_BASE_URL, and CRG_OPENAI_MODEL to all be set. get_provider() collects which are missing and raises listing them by name.

Source

Thrown at code_review_graph/embeddings.py:934

        provider is None
        and os.environ.get("CRG_OPENAI_API_KEY")
        and os.environ.get("CRG_OPENAI_BASE_URL")
    ):
        name = "openai"

    if name == "openai":
        api_key = os.environ.get("CRG_OPENAI_API_KEY")
        base_url = os.environ.get("CRG_OPENAI_BASE_URL")
        resolved_model = model or os.environ.get("CRG_OPENAI_MODEL")
        if not api_key or not base_url or not resolved_model:
            missing = [
                name for name, val in [
                    ("CRG_OPENAI_API_KEY", api_key),
                    ("CRG_OPENAI_BASE_URL", base_url),
                    ("CRG_OPENAI_MODEL", resolved_model),
                ] if not val
            ]
            raise ValueError(
                "Missing required environment variable(s) for the OpenAI "
                f"embedding provider: {', '.join(missing)}."
            )
        dim_env = os.environ.get("CRG_OPENAI_DIMENSION")
        dimension = int(dim_env) if dim_env else None
        batch_env = os.environ.get("CRG_OPENAI_BATCH_SIZE")
        batch_size = int(batch_env) if batch_env else None
        if not _is_localhost_url(base_url):
            _warn_cloud_egress("openai")
        return OpenAIEmbeddingProvider(
            api_key=api_key,
            base_url=base_url,
            model=resolved_model,
            dimension=dimension,
            batch_size=batch_size,
        )

    if name == "minimax":

View on GitHub (pinned to b58668751a)

Solutions

  1. Export all three: CRG_OPENAI_API_KEY, CRG_OPENAI_BASE_URL, CRG_OPENAI_MODEL
  2. The message names exactly which are missing — set those first
  3. Verify with: env | grep CRG_OPENAI
  4. Use a different provider (e.g. local) if you didn't intend OpenAI mode

Example fix

# before
export CRG_OPENAI_API_KEY=sk-...
# after
export CRG_OPENAI_API_KEY=sk-...
export CRG_OPENAI_BASE_URL=https://api.openai.com/v1
export CRG_OPENAI_MODEL=text-embedding-3-small
Defensive patterns

Strategy: validation

Validate before calling

missing = [v for v in ("CRG_OPENAI_API_KEY", "CRG_OPENAI_BASE_URL", "CRG_OPENAI_MODEL") if not os.environ.get(v)]
if missing:
    raise RuntimeError(f"missing env: {missing}")

Try / catch

try:
    provider = get_provider("openai")
except ValueError as e:
    if "Missing required environment variable" in str(e):
        provider = get_provider("local")  # fallback
    else:
        raise

Prevention

When it happens

Trigger: Selecting provider='openai' (explicitly or via OpenAI-compatible env detection) while one or more of the three CRG_OPENAI_* variables is unset or empty.

Common situations: Setting only the API key and assuming defaults exist, CI shells that drop exported vars, or .env files not loaded into the process environment.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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