mastra-ai/mastra · error

Chunks and embeddings must have the same length

Error message

Chunks and embeddings must have the same length

What it means

createGraph requires a 1:1 correspondence between chunks and embeddings because node i gets content from chunks[i] and its vector from embeddings[i]. If the array lengths differ it throws, since pairing would be ambiguous and would mis-assign embeddings to content.

Source

Thrown at packages/rag/src/graph-rag/index.ts:230

      normVec1 += a * a;
      normVec2 += b * b;
    }
    const magnitudeProduct = Math.sqrt(normVec1 * normVec2);

    if (magnitudeProduct === 0) {
      return 0;
    }

    const similarity = dotProduct / magnitudeProduct;
    return Math.max(-1, Math.min(1, similarity));
  }

  createGraph(chunks: GraphChunk[], embeddings: GraphEmbedding[]) {
    if (!chunks?.length || !embeddings?.length) {
      throw new Error('Chunks and embeddings arrays must not be empty');
    }
    if (chunks.length !== embeddings.length) {
      throw new Error('Chunks and embeddings must have the same length');
    }
    // Create nodes from chunks
    chunks.forEach((chunk, index) => {
      const node: GraphNode = {
        id: index.toString(),
        content: chunk.text,
        embedding: embeddings[index]?.vector,
        metadata: { ...chunk.metadata },
      };
      this.addNode(node);
      this.nodes.set(node.id, node);
    });

    // Create edges based on cosine similarity
    for (let i = 0; i < chunks.length; i++) {
      const firstEmbedding = embeddings[i]?.vector as number[];
      for (let j = i + 1; j < chunks.length; j++) {
        const secondEmbedding = embeddings[j]?.vector as number[];

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Re-embed the current chunk list so chunks.length === embeddings.length.
  2. If chunks were filtered, apply the identical filter to embeddings before the call.
  3. Assert equality and fail early with counts to identify which pipeline stage desynchronized.
  4. Use a deterministic zip/validate step that pairs chunk text with its embedding id before createGraph.

Example fix

// before
await graph.createGraph(chunks, embeddings);
// after
if (chunks.length !== embeddings.length) {
  throw new Error(`Desync: ${chunks.length} chunks vs ${embeddings.length} embeddings — re-embed`);
}
await graph.createGraph(chunks, embeddings);
Defensive patterns

Strategy: validation

Validate before calling

if (chunks.length !== embeddings.length) {
  throw new Error(`Chunk/embedding desync: ${chunks.length} vs ${embeddings.length}; re-embed the current chunks`);
}

Type guard

null

Try / catch

try {
  graph.createGraph(chunks, embeddings);
} catch (e) {
  if ((e as Error).message === 'Chunks and embeddings must have the same length') {
    // re-run embedding over the current chunk list, then rebuild
  } else throw e;
}

Prevention

When it happens

Trigger: createGraph(chunks, embeddings) where chunks.length !== embeddings.length — e.g. the embedder dropped or duplicated entries, batches failed partially, or chunks were re-split after embedding.

Common situations: Embedding API returning fewer results than inputs on partial failure; chunking parameters changed after embeddings were generated; filtering chunks but not embeddings (or vice versa); caching embeddings from an older chunk set.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/7b76f0f77d5095a2. Report an issue: GitHub.