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
- 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.
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
- 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
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
- Node must have an embedding
- HTML chunking requires either headers or sections to be spec
- JSON chunking requires maxSize to be specified
- Sentence chunking requires maxSize to be specified
- Keywords must be greater than 0
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/7b76f0f77d5095a2.
Report an issue: GitHub.