{"record":{"id":"9c116379fa6f8d0a","repo":"mastra-ai/mastra","slug":"chunks-and-embeddings-arrays-must-not-be-empty","errorCode":null,"errorMessage":"Chunks and embeddings arrays must not be empty","messagePattern":"Chunks and embeddings arrays must not be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/rag/src/graph-rag/index.ts","lineNumber":227,"sourceCode":"      const b = vec2[i]!;\n\n      dotProduct += a * b;\n      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++) {","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/rag/src/graph-rag/index.ts#L209-L245","documentation":"createGraph(chunks, embeddings) refuses to build a graph from empty input: if either array is null, undefined, or length 0 it throws. An empty graph would silently return no results from every query, so the library fails fast instead.","triggerScenarios":"createGraph([], []) or createGraph(chunks, []) — typically when document loading/chunking produced nothing, or the embedding call returned an empty array.","commonSituations":"Ingesting an empty folder or documents yielding zero chunks; a filter excluding all content; embedder failing and returning []; calling createGraph before any documents are loaded.","solutions":["Check that chunking produced content before calling createGraph; fix the source loading if chunks is empty.","Verify the embedding step actually returns one vector per chunk and that the array is non-empty.","Skip graph construction (or throw your own clearer error) when there is nothing to index.","Log counts of chunks and embeddings just before the call to see which array is empty."],"exampleFix":"// before\nawait graph.createGraph(chunks, embeddings);\n// after\nif (!chunks.length || !embeddings.length) {\n  throw new Error(`Nothing to index: chunks=${chunks.length}, embeddings=${embeddings.length}`);\n}\nawait graph.createGraph(chunks, embeddings);","handlingStrategy":"validation","validationCode":"if (!chunks?.length || !embeddings?.length) {\n  throw new Error(`Nothing to index (chunks=${chunks?.length ?? 0}, embeddings=${embeddings?.length ?? 0})`);\n}","typeGuard":"null","tryCatchPattern":"try {\n  graph.createGraph(chunks, embeddings);\n} catch (e) {\n  if ((e as Error).message === 'Chunks and embeddings arrays must not be empty') {\n    // ingestion produced nothing: fix document loading/chunking, then rebuild\n  } else throw e;\n}","preventionTips":["Log chunk and embedding counts before indexing","Fail your pipeline early when chunking yields zero chunks","Verify embed output is a non-empty array with one vector per chunk","Guard against silently empty document sources (wrong path, over-aggressive filters)"],"tags":["rag","empty-input","validation","argument-error"],"backgroundTag":"empty-input-validation","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}