supermemoryai/supermemory · error · Error
Vectors must have the same length
Error message
Vectors must have the same length
What it means
cosineSimilarity throws when the two vectors passed in have different lengths. Dot product (and cosine similarity) is only defined for vectors of the same dimension, so this is a precondition check on the mathematical operation.
Source
Thrown at packages/lib/similarity.ts:13
// Utility functions for calculating semantic similarity between documents and memories
/**
* Calculate cosine similarity between two normalized vectors (unit vectors)
* Since all embeddings in this system are normalized using normalizeEmbeddingFast,
* cosine similarity equals dot product for unit vectors.
*/
export const cosineSimilarity = (
vectorA: number[],
vectorB: number[],
): number => {
if (vectorA.length !== vectorB.length) {
throw new Error("Vectors must have the same length")
}
let dotProduct = 0
for (let i = 0; i < vectorA.length; i++) {
const vectorAi = vectorA[i]
const vectorBi = vectorB[i]
if (
typeof vectorAi !== "number" ||
typeof vectorBi !== "number" ||
isNaN(vectorAi) ||
isNaN(vectorBi)
) {
throw new Error("Vectors must contain only numbers")
}
dotProduct += vectorAi * vectorBi
}
View on GitHub (pinned to d436792e77)
Solutions
- Ensure both vectors come from the same embedding model and version
- Add a length check before calling and log/skip mismatched pairs
- Re-embed stored data after changing embedding models
- Validate vector dimension at ingestion time
Example fix
// before
const score = cosineSimilarity(storedVec, queryVec)
// after
if (storedVec.length !== queryVec.length) {
console.warn(`dim mismatch: ${storedVec.length} vs ${queryVec.length}`)
continue
}
const score = cosineSimilarity(storedVec, queryVec) Defensive patterns
Strategy: validation
Validate before calling
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) { throw new TypeError(`Vector length mismatch: ${a?.length} vs ${b?.length}`) } Type guard
const isVectorOf = (len: number) => (v: unknown): v is number[] => Array.isArray(v) && v.length === len && v.every((x) => typeof x === 'number' && Number.isFinite(x))
Try / catch
try { score = cosineSimilarity(a, b) } catch { continue /* skip incomparable vectors */ } Prevention
- Pin one embedding model/version for both storage and query
- Store model+dimension metadata alongside each vector
- Re-embed everything after any embedding model change
When it happens
Trigger: Calling cosineSimilarity with embeddings from different models (e.g. a 1536-dim OpenAI vector vs a 768-dim vector), or comparing a vector against an empty/undefined array coerced to a different length.
Common situations: Switching embedding providers without re-embedding stored vectors; mixing stored embeddings with freshly generated ones from a different model version; passing a truncated or malformed stored vector.
Related errors
- Vectors must contain only numbers
- withSupermemory: options must be an object with required con
- containerTag is required — provide a non-empty string to ide
- customId is required — provide a non-empty string to group m
- Supermemory tools config accepts either projectId or contain
AI-assisted analysis of supermemoryai/supermemory@d436792e77 (2026-08-28).
Data as JSON: /api/errors/395ee0921db64d50.
Report an issue: GitHub.