tirth8205/code-review-graph · error · RuntimeError

OpenAI API HTTP {http_err.code}: {err_msg}

Error message

OpenAI API HTTP {http_err.code}: {err_msg}

What it means

The OpenAI-compatible embeddings provider raises this RuntimeError when the HTTPS request itself returns an HTTPError (4xx/5xx). It extracts a JSON error body when possible (seeding err_msg from the raw body otherwise) and chains the original http_err, producing a message like 'OpenAI API HTTP 401: ...'.

Source

Thrown at code_review_graph/embeddings.py:507

                    # etc.) which is far more actionable.
                    try:
                        err_body = http_err.read().decode("utf-8", errors="replace")
                    except Exception:
                        err_body = ""
                    err_msg = err_body or str(http_err)
                    try:
                        parsed = _json.loads(err_body)
                        if isinstance(parsed, dict) and "error" in parsed:
                            err_obj = parsed["error"]
                            err_msg = (
                                err_obj.get("message", err_msg)
                                if isinstance(err_obj, dict) else str(err_obj)
                            )
                    except Exception:  # nosec B110
                        # Non-JSON error body is fine: we already seeded
                        # err_msg with the raw body above, so fall through.
                        pass
                    raise RuntimeError(
                        f"OpenAI API HTTP {http_err.code}: {err_msg}"
                    ) from http_err

                response = _json.loads(raw)

                if "error" in response:
                    err = response["error"]
                    msg = err.get("message", "unknown") if isinstance(err, dict) else str(err)
                    raise RuntimeError(f"OpenAI API error: {msg}")

                data = response.get("data", [])
                if not data:
                    raise RuntimeError("OpenAI API returned empty data")
                # OpenAI spec: data[i].index maps to input[i], but some
                # compatible gateways re-order results or drop entries on
                # partial failure, and others omit `index` entirely. Three
                # disjoint cases:
                #   1. All items have a valid int ``index``: must form a

View on GitHub (pinned to b58668751a)

Solutions

  1. Read the HTTP code: 401/403 → fix API key; 404 → fix base_url or model name; 429 → slow down/backoff; 5xx → retry or check gateway health
  2. Verify connectivity and credentials with a one-line curl against the same base_url/model
  3. If using a custom OpenAI-compatible gateway, confirm it implements the /embeddings endpoint and error JSON shape
  4. Add exponential-backoff retry for 429/5xx
Defensive patterns

Strategy: retry

Validate before calling

import os
assert os.environ.get("OPENAI_API_KEY"), "OPENAI_API_KEY not set"
# smoke-test endpoint+model cheaply before the big batch
provider.embed_query("ping")

Try / catch

import time, urllib.error
for attempt in range(5):
    try:
        return provider.embed(texts)
    except RuntimeError as e:
        if "OpenAI API HTTP 429" in str(e) or "HTTP 5" in str(e):
            time.sleep(2 ** attempt); continue
        raise  # 4xx auth/config errors are not retryable

Prevention

When it happens

Trigger: embed()/embed_query() hitting a 401 (bad key), 404 (wrong base_url/model), 429 (rate limit), or 5xx from the OpenAI-compatible endpoint (including self-hosted gateways).

Common situations: Expired OPENAI_API_KEY, pointing base_url at a proxy/vLLM/LiteLLM gateway that returns non-200s, model name unsupported by the gateway, or rate limiting during bulk embedding runs.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


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