{"record":{"id":"c43c7ccf64a60a9d","repo":"tirth8205/code-review-graph","slug":"openai-api-returned-mixed-indexed-unindexed-data","errorCode":null,"errorMessage":"OpenAI API returned mixed indexed/unindexed data — refusing to misalign vectors.","messagePattern":"OpenAI API returned mixed indexed/unindexed data — refusing to misalign vectors\\.","errorType":"http","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"code_review_graph/embeddings.py","lineNumber":557,"sourceCode":"                    if len(set(indices)) != len(indices) or set(indices) != expected:\n                        raise RuntimeError(\n                            \"OpenAI API returned malformed indices \"\n                            f\"(got {indices}, expected permutation of \"\n                            f\"0..{len(texts) - 1}) — refusing to misalign vectors.\"\n                        )\n                    data = sorted(data, key=lambda item: int(item[\"index\"]))\n                elif not any_has_index:\n                    if len(data) != len(texts):\n                        raise RuntimeError(\n                            f\"OpenAI API returned {len(data)} embeddings for \"\n                            f\"{len(texts)} inputs with no index field — \"\n                            \"refusing to misalign vectors.\"\n                        )\n                else:\n                    # Mixed: some items have index, others don't (or carry\n                    # non-int index). Server order would silently misplace\n                    # the indexed items, so we refuse.\n                    raise RuntimeError(\n                        \"OpenAI API returned mixed indexed/unindexed data — \"\n                        \"refusing to misalign vectors.\"\n                    )\n\n                vectors = [item[\"embedding\"] for item in data]\n                if vectors and self._dimension is None:\n                    self._dimension = len(vectors[0])\n                return vectors\n\n            except Exception as e:\n                # Retryable = HTTP 429/5xx, network/timeout/TLS issues.\n                # Non-retryable = HTTP 4xx (other), malformed responses,\n                # misaligned data length — those are caller-side bugs that\n                # will keep failing on retry.\n                is_retryable = False\n                if isinstance(e, urllib.error.HTTPError):\n                    is_retryable = e.code == 429 or 500 <= e.code < 600\n                elif isinstance(e, (","sourceCodeStart":539,"sourceCodeEnd":575,"githubUrl":"https://github.com/tirth8205/code-review-graph/blob/b58668751ab0c7670c078cf7cbd4d1f5b8e54f81/code_review_graph/embeddings.py#L539-L575","documentation":"If some returned embedding items have an integer `index` and others do not (or carry non-int index), there is no consistent rule to map vectors back to inputs: server order would misplace the indexed items. The provider therefore refuses with this RuntimeError instead of silently misaligning embeddings.","triggerScenarios":"embed() against a gateway that mixes response formats — some items include data[i].index, others omit it or use a string index — e.g. a partially upgraded gateway or one that only indexes items it reordered.","commonSituations":"Homegrown OpenAI-compatible embedding servers, gateways stitching responses from multiple upstream shards, or proxies that rewrite some items and pass others through unchanged.","solutions":["Capture the raw response from the gateway and inspect which items lack index — that identifies the offending code path","Upgrade/fix the gateway to emit a uniform format: either all items indexed as a 0..N-1 permutation, or all unindexed with exact count","Route around the non-conforming gateway (use official OpenAI or another compliant endpoint) until fixed"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"# Canary against a new gateway: detect mixed-format responses cheaply\ntry:\n    v = provider.embed([\"a\", \"b\"])\n    assert len(v) == 2\nexcept RuntimeError:\n    raise RuntimeError(\"Gateway returns non-conforming embeddings responses; use another endpoint\")","typeGuard":null,"tryCatchPattern":"try:\n    vecs = primary_gateway.embed(texts)\nexcept RuntimeError as e:\n    if \"mixed indexed/unindexed\" in str(e):\n        vecs = official_openai.embed(texts)  # fall back to a spec-compliant endpoint\n    else:\n        raise","preventionTips":["Canary-test gateways with a 2-item embed before trusting them in production","Prefer official OpenAI/Voyage endpoints for correctness-critical embeddings","Keep a compliant fallback provider configured when using custom gateways"],"tags":["python","openai","embeddings","mixed-format","misalignment-guard"],"backgroundTag":"response-schema-validation","analyzedSha":"b58668751ab0c7670c078cf7cbd4d1f5b8e54f81","analyzedAt":"2026-08-28T13:19:08.966Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}