tirth8205/code-review-graph · error · RuntimeError

OpenAI API returned {len(data)} embeddings for {len(texts)}

Error message

OpenAI API returned {len(data)} embeddings for {len(texts)} inputs with no index field — refusing to misalign vectors.

What it means

When NONE of the returned embedding items carry an `index` field, the only safe assumption is positional correspondence, which requires len(data) == len(texts). If the counts differ, the provider raises this RuntimeError rather than guess how to zip server order onto the inputs, since a mismatch would silently assign vectors to the wrong texts.

Source

Thrown at code_review_graph/embeddings.py:548

                #      missing on some): refuse. Zipping server order in
                #      that case would happily misalign the indexed items.
                any_has_index = any("index" in item for item in data)
                all_int_index = all(
                    isinstance(item.get("index"), int) for item in data
                )
                if all_int_index:
                    expected = set(range(len(texts)))
                    indices = [int(item["index"]) for item in data]
                    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

View on GitHub (pinned to b58668751a)

Solutions

  1. Pre-validate inputs: remove empty/oversized strings before calling embed
  2. Compare len(data) vs len(texts) from the message to confirm a server-side drop, then reduce batch size and retry
  3. Upgrade or fix the gateway to either return index fields or exact-count positional data

Example fix

# before
texts = [t for t in chunks]
vectors = provider.embed(texts)
# after
texts = [t for t in chunks if t.strip()]
vectors = provider.embed(texts)
Defensive patterns

Strategy: validation

Validate before calling

# Normalize inputs: gateways drop empty/oversized items, causing count mismatch
texts = [t.strip() for t in texts if t and t.strip()]
assert texts, "no non-empty texts to embed"

Try / catch

try:
    vecs = provider.embed(batch)
except RuntimeError as e:
    if "no index field" in str(e):
        vecs = provider.embed(batch)  # retry once; persistent mismatch = gateway bug
    else:
        raise

Prevention

When it happens

Trigger: embed() against a gateway that omits `index` in embeddings responses and returns fewer or more embeddings than input texts (e.g. dropped empty-string inputs or concatenated batches).

Common situations: Gateways that filter out empty or oversized inputs server-side, or that merge/split batches — producing count mismatches with no index metadata to recover the mapping.

Related errors


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