{"record":{"id":"9d2b4ce5439ab5df","repo":"ruvnet/ruflo","slug":"vector-dimension-mismatch-a-length-vs-b-leng","errorCode":null,"errorMessage":"Vector dimension mismatch: ${a.length} vs ${b.length}","messagePattern":"Vector dimension mismatch: (.+?) vs (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/ruvector/vector-db.ts","lineNumber":85,"sourceCode":"  remove(id: string): boolean {\n    return this.vectors.delete(id);\n  }\n\n  size(): number {\n    return this.vectors.size;\n  }\n\n  clear(): void {\n    this.vectors.clear();\n  }\n}\n\n/**\n * Compute cosine similarity between two vectors\n */\nfunction cosineSimilarity(a: Float32Array, b: Float32Array): number {\n  if (a.length !== b.length) {\n    throw new Error(`Vector dimension mismatch: ${a.length} vs ${b.length}`);\n  }\n\n  let dotProduct = 0;\n  let normA = 0;\n  let normB = 0;\n\n  for (let i = 0; i < a.length; i++) {\n    dotProduct += a[i] * b[i];\n    normA += a[i] * a[i];\n    normB += b[i] * b[i];\n  }\n\n  const denom = Math.sqrt(normA) * Math.sqrt(normB);\n  return denom === 0 ? 0 : dotProduct / denom;\n}\n\n/**\n * Whether the hash-embedding one-time warning has been emitted","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/ruvector/vector-db.ts#L67-L103","documentation":"Thrown by the cosineSimilarity helper in vector-db.ts when the two vectors it was asked to compare have different lengths. In practice this means a search/query vector whose length differs from the vectors stored in the VectorDB (constructed with a fixed dimensions argument). Because cosine similarity iterates a[i]*b[i] for the full length, mismatched vectors cannot be compared, so it fails fast.","triggerScenarios":"Constructing VectorDB with dimensions=N and inserting vectors of length N, then calling search/similarity with a query vector of length M != N; or inserting vectors of inconsistent lengths in the first place so two stored vectors get compared.","commonSituations":"Switching embedding models after the DB was populated (old stored 384-dim vectors, new 768-dim queries), loading a persisted DB built under an old configuration, feeding raw token arrays instead of model embeddings.","solutions":["Embed queries with the same model (and thus the same dimension) used when inserting vectors","After changing embedding model/dimension, rebuild the vector store: clear() and re-insert re-embedded vectors","Validate query.length against the dimension passed to the VectorDB constructor before calling search"],"exampleFix":"// before\nconst db = new VectorDB(384);\n// ...later, model changed to 768-dim:\ndb.search(embed768(query)); // throws 'Vector dimension mismatch: 768 vs 384'\n\n// after\nif (embed768(query).length !== 384) {\n  throw new Error('embedding model drift — rebuild the store or migrate vectors');\n}\ndb.search(Float32Array.from(embed384(query)));","handlingStrategy":"validation","validationCode":"const query = Float32Array.from(rawQuery);\nif (query.length !== DB_DIMENSIONS) { // the number passed to new VectorDB(dimensions)\n  throw new Error(`query is ${query.length}-dim but store is ${DB_DIMENSIONS}-dim; re-embed or rebuild store`);\n}\ndb.search(query);","typeGuard":"const isCompatibleVector = (v: Float32Array, dims: number): boolean => v instanceof Float32Array && v.length === dims;","tryCatchPattern":"try {\n  db.search(query);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Vector dimension mismatch')) {\n    // dimension drift: rebuild the store with re-embedded vectors, then retry once\n  } else throw e;\n}","preventionTips":["Store the embedding model name and dimension as metadata next to the vector store and verify both at load time","Centralize embedding creation in one function so inserts and queries can never diverge","Treat any model change as a migration: clear() and re-embed everything before serving queries"],"tags":["vector-db","cosine-similarity","embeddings","dimension-mismatch"],"backgroundTag":"vector-dimension-mismatch","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}