tirth8205/code-review-graph · error · RuntimeError

OpenAI API returned mixed indexed/unindexed data — refusing

Error message

OpenAI API returned mixed indexed/unindexed data — refusing to misalign vectors.

What it means

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.

Source

Thrown at code_review_graph/embeddings.py:557

                    if len(set(indices)) != len(indices) or set(indices) != expected:
                        raise RuntimeError(
                            "OpenAI API returned malformed indices "
                            f"(got {indices}, expected permutation of "
                            f"0..{len(texts) - 1}) — refusing to misalign vectors."
                        )
                    data = sorted(data, key=lambda item: int(item["index"]))
                elif not any_has_index:
                    if len(data) != len(texts):
                        raise RuntimeError(
                            f"OpenAI API returned {len(data)} embeddings for "
                            f"{len(texts)} inputs with no index field — "
                            "refusing to misalign vectors."
                        )
                else:
                    # Mixed: some items have index, others don't (or carry
                    # non-int index). Server order would silently misplace
                    # the indexed items, so we refuse.
                    raise RuntimeError(
                        "OpenAI API returned mixed indexed/unindexed data — "
                        "refusing to misalign vectors."
                    )

                vectors = [item["embedding"] for item in data]
                if vectors and self._dimension is None:
                    self._dimension = len(vectors[0])
                return vectors

            except Exception as e:
                # Retryable = HTTP 429/5xx, network/timeout/TLS issues.
                # Non-retryable = HTTP 4xx (other), malformed responses,
                # misaligned data length — those are caller-side bugs that
                # will keep failing on retry.
                is_retryable = False
                if isinstance(e, urllib.error.HTTPError):
                    is_retryable = e.code == 429 or 500 <= e.code < 600
                elif isinstance(e, (

View on GitHub (pinned to b58668751a)

Solutions

  1. Capture the raw response from the gateway and inspect which items lack index — that identifies the offending code path
  2. 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
  3. Route around the non-conforming gateway (use official OpenAI or another compliant endpoint) until fixed
Defensive patterns

Strategy: fallback

Validate before calling

# Canary against a new gateway: detect mixed-format responses cheaply
try:
    v = provider.embed(["a", "b"])
    assert len(v) == 2
except RuntimeError:
    raise RuntimeError("Gateway returns non-conforming embeddings responses; use another endpoint")

Try / catch

try:
    vecs = primary_gateway.embed(texts)
except RuntimeError as e:
    if "mixed indexed/unindexed" in str(e):
        vecs = official_openai.embed(texts)  # fall back to a spec-compliant endpoint
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Homegrown OpenAI-compatible embedding servers, gateways stitching responses from multiple upstream shards, or proxies that rewrite some items and pass others through unchanged.

Related errors


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