{"record":{"id":"dbf491fcdcaef383","repo":"tirth8205/code-review-graph","slug":"openai-api-error-msg","errorCode":null,"errorMessage":"OpenAI API error: {msg}","messagePattern":"OpenAI API error: (.+?)","errorType":"http","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"code_review_graph/embeddings.py","lineNumber":516,"sourceCode":"                            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\n                #      permutation of 0..N-1, then sort and use.\n                #   2. NO item carries an ``index`` field: trust server\n                #      order, only verify count matches.\n                #   3. Anything in between (partial indices, str indices,\n                #      missing on some): refuse. Zipping server order in\n                #      that case would happily misalign the indexed items.\n                any_has_index = any(\"index\" in item for item in data)\n                all_int_index = all(\n                    isinstance(item.get(\"index\"), int) for item in data","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/tirth8205/code-review-graph/blob/b58668751ab0c7670c078cf7cbd4d1f5b8e54f81/code_review_graph/embeddings.py#L498-L534","documentation":"This RuntimeError fires when the HTTP request to the OpenAI-compatible embeddings endpoint succeeded (status 200) but the JSON response body contains a top-level \"error\" object — some gateways report errors in-band rather than via HTTP status. The provider extracts error.message (or stringifies the error) and raises.","triggerScenarios":"embed()/embed_query() receiving a 200 response whose body is {\"error\": {...}} — typical of OpenAI-compatible proxies, LiteLLM routers, orAzure-style endpoints that wrap upstream failures in-band.","commonSituations":"Self-hosted or proxied OpenAI-compatible gateways that return 200 with an error payload for quota, model-access, or upstream-provider failures; upstream OpenAI outages surfaced through a router.","solutions":["Inspect the embedded message to identify the underlying cause (quota, model access, upstream failure)","If using a proxy/router, check its logs and upstream provider configuration","Verify the model name is allowed for your key/gateway","Retry with backoff if the message indicates a transient upstream issue"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"# Smoke-test the gateway: some return 200 with in-band errors\nprovider.embed_query(\"ping\")  # fail fast before a long batch","typeGuard":null,"tryCatchPattern":"try:\n    vecs = provider.embed(texts)\nexcept RuntimeError as e:\n    if \"OpenAI API error:\" in str(e):\n        log_gateway_incident(str(e))  # in-band gateway failure\n    raise","preventionTips":["Know whether your endpoint is official OpenAI or a proxy — proxies often report errors in-band","Monitor provider logs for in-band error messages, not just HTTP status codes","Retry transient in-band errors (quota/upstream) with backoff, but fail fast on auth messages"],"tags":["python","openai","api-error","embeddings","in-band-error"],"backgroundTag":"api-error-response","analyzedSha":"b58668751ab0c7670c078cf7cbd4d1f5b8e54f81","analyzedAt":"2026-08-28T13:19:08.966Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}