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
- 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.
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
- 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)
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
- OpenAISDKAgent resumeData must include previousResponseId, c
- configDirName must be a non-empty directory name
- COMPARE_INVALID_INPUT
- RUN_EXPERIMENT_FAILED_NO_DATA_PROVIDED
- Knowledge scope cannot be empty
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/9c116379fa6f8d0a.
Report an issue: GitHub.