mastra-ai/mastra · error

Chunks and embeddings arrays must not be empty

Error message

Chunks and embeddings arrays must not be empty

What it means

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.

Source

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

      const b = vec2[i]!;

      dotProduct += a * b;
      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++) {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Check that chunking produced content before calling createGraph; fix the source loading if chunks is empty.
  2. Verify the embedding step actually returns one vector per chunk and that the array is non-empty.
  3. Skip graph construction (or throw your own clearer error) when there is nothing to index.
  4. Log counts of chunks and embeddings just before the call to see which array is empty.

Example fix

// before
await graph.createGraph(chunks, embeddings);
// after
if (!chunks.length || !embeddings.length) {
  throw new Error(`Nothing to index: chunks=${chunks.length}, embeddings=${embeddings.length}`);
}
await graph.createGraph(chunks, embeddings);
Defensive patterns

Strategy: validation

Validate before calling

if (!chunks?.length || !embeddings?.length) {
  throw new Error(`Nothing to index (chunks=${chunks?.length ?? 0}, embeddings=${embeddings?.length ?? 0})`);
}

Type guard

null

Try / catch

try {
  graph.createGraph(chunks, embeddings);
} catch (e) {
  if ((e as Error).message === 'Chunks and embeddings arrays must not be empty') {
    // ingestion produced nothing: fix document loading/chunking, then rebuild
  } else throw e;
}

Prevention

When it happens

Trigger: createGraph([], []) or createGraph(chunks, []) — typically when document loading/chunking produced nothing, or the embedding call returned an empty array.

Common situations: 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.

Related errors


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