{"record":{"id":"1a49fa28ea61398c","repo":"tirth8205/code-review-graph","slug":"openai-api-http-http-err-code-err-msg","errorCode":null,"errorMessage":"OpenAI API HTTP {http_err.code}: {err_msg}","messagePattern":"OpenAI API HTTP (.+?): (.+?)","errorType":"http","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"code_review_graph/embeddings.py","lineNumber":507,"sourceCode":"                    # etc.) which is far more actionable.\n                    try:\n                        err_body = http_err.read().decode(\"utf-8\", errors=\"replace\")\n                    except Exception:\n                        err_body = \"\"\n                    err_msg = err_body or str(http_err)\n                    try:\n                        parsed = _json.loads(err_body)\n                        if isinstance(parsed, dict) and \"error\" in parsed:\n                            err_obj = parsed[\"error\"]\n                            err_msg = (\n                                err_obj.get(\"message\", err_msg)\n                                if isinstance(err_obj, dict) else str(err_obj)\n                            )\n                    except Exception:  # nosec B110\n                        # Non-JSON error body is fine: we already seeded\n                        # err_msg with the raw body above, so fall through.\n                        pass\n                    raise RuntimeError(\n                        f\"OpenAI API HTTP {http_err.code}: {err_msg}\"\n                    ) from http_err\n\n                response = _json.loads(raw)\n\n                if \"error\" in response:\n                    err = response[\"error\"]\n                    msg = err.get(\"message\", \"unknown\") if isinstance(err, dict) else str(err)\n                    raise RuntimeError(f\"OpenAI API error: {msg}\")\n\n                data = response.get(\"data\", [])\n                if not data:\n                    raise RuntimeError(\"OpenAI API returned empty data\")\n                # OpenAI spec: data[i].index maps to input[i], but some\n                # compatible gateways re-order results or drop entries on\n                # partial failure, and others omit `index` entirely. Three\n                # disjoint cases:\n                #   1. All items have a valid int ``index``: must form a","sourceCodeStart":489,"sourceCodeEnd":525,"githubUrl":"https://github.com/tirth8205/code-review-graph/blob/b58668751ab0c7670c078cf7cbd4d1f5b8e54f81/code_review_graph/embeddings.py#L489-L525","documentation":"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: ...'.","triggerScenarios":"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).","commonSituations":"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.","solutions":["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","Verify connectivity and credentials with a one-line curl against the same base_url/model","If using a custom OpenAI-compatible gateway, confirm it implements the /embeddings endpoint and error JSON shape","Add exponential-backoff retry for 429/5xx"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"import os\nassert os.environ.get(\"OPENAI_API_KEY\"), \"OPENAI_API_KEY not set\"\n# smoke-test endpoint+model cheaply before the big batch\nprovider.embed_query(\"ping\")","typeGuard":null,"tryCatchPattern":"import time, urllib.error\nfor attempt in range(5):\n    try:\n        return provider.embed(texts)\n    except RuntimeError as e:\n        if \"OpenAI API HTTP 429\" in str(e) or \"HTTP 5\" in str(e):\n            time.sleep(2 ** attempt); continue\n        raise  # 4xx auth/config errors are not retryable","preventionTips":["Run a one-item embed_query() smoke test before bulk embedding","Never retry 401/403/404 — fix credentials/base_url/model instead","Cache embeddings to disk so transient API failures don't restart whole runs","Use exponential backoff with jitter on 429/5xx"],"tags":["python","openai","http-error","embeddings","api-error"],"backgroundTag":"http-error-response","analyzedSha":"b58668751ab0c7670c078cf7cbd4d1f5b8e54f81","analyzedAt":"2026-08-28T13:19:08.966Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}