{"record":{"id":"7b76f0f77d5095a2","repo":"mastra-ai/mastra","slug":"chunks-and-embeddings-must-have-the-same-length","errorCode":null,"errorMessage":"Chunks and embeddings must have the same length","messagePattern":"Chunks and embeddings must have the same length","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/rag/src/graph-rag/index.ts","lineNumber":230,"sourceCode":"      normVec1 += a * a;\n      normVec2 += b * b;\n    }\n    const magnitudeProduct = Math.sqrt(normVec1 * normVec2);\n\n    if (magnitudeProduct === 0) {\n      return 0;\n    }\n\n    const similarity = dotProduct / magnitudeProduct;\n    return Math.max(-1, Math.min(1, similarity));\n  }\n\n  createGraph(chunks: GraphChunk[], embeddings: GraphEmbedding[]) {\n    if (!chunks?.length || !embeddings?.length) {\n      throw new Error('Chunks and embeddings arrays must not be empty');\n    }\n    if (chunks.length !== embeddings.length) {\n      throw new Error('Chunks and embeddings must have the same length');\n    }\n    // Create nodes from chunks\n    chunks.forEach((chunk, index) => {\n      const node: GraphNode = {\n        id: index.toString(),\n        content: chunk.text,\n        embedding: embeddings[index]?.vector,\n        metadata: { ...chunk.metadata },\n      };\n      this.addNode(node);\n      this.nodes.set(node.id, node);\n    });\n\n    // Create edges based on cosine similarity\n    for (let i = 0; i < chunks.length; i++) {\n      const firstEmbedding = embeddings[i]?.vector as number[];\n      for (let j = i + 1; j < chunks.length; j++) {\n        const secondEmbedding = embeddings[j]?.vector as number[];","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/rag/src/graph-rag/index.ts#L212-L248","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-embed the current chunk list so chunks.length === embeddings.length.","If chunks were filtered, apply the identical filter to embeddings before the call.","Assert equality and fail early with counts to identify which pipeline stage desynchronized.","Use a deterministic zip/validate step that pairs chunk text with its embedding id before createGraph."],"exampleFix":"// before\nawait graph.createGraph(chunks, embeddings);\n// after\nif (chunks.length !== embeddings.length) {\n  throw new Error(`Desync: ${chunks.length} chunks vs ${embeddings.length} embeddings — re-embed`);\n}\nawait graph.createGraph(chunks, embeddings);","handlingStrategy":"validation","validationCode":"if (chunks.length !== embeddings.length) {\n  throw new Error(`Chunk/embedding desync: ${chunks.length} vs ${embeddings.length}; re-embed the current chunks`);\n}","typeGuard":"null","tryCatchPattern":"try {\n  graph.createGraph(chunks, embeddings);\n} catch (e) {\n  if ((e as Error).message === 'Chunks and embeddings must have the same length') {\n    // re-run embedding over the current chunk list, then rebuild\n  } else throw e;\n}","preventionTips":["Re-embed whenever chunking logic or parameters change","Apply any chunk filters to embeddings identically","Pair each chunk with its embedding via a stable id before batching into createGraph","Never cache embeddings keyed by position across different chunk sets"],"tags":["rag","length-mismatch","embedding","validation"],"backgroundTag":"array-length-mismatch","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}