{"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":"Error","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/6b01dc5a687b26b3e218f796de45ec51f8fa9e8c/v3/@claude-flow/cli/src/ruvector/vector-db.ts#L67-L103","documentation":"The cosineSimilarity(a, b) helper throws when a.length !== b.length before computing the dot product, because cosine similarity is undefined for vectors of different dimensionality. The message reports both lengths so the mismatch is obvious. This is a module-private function called by VectorDb similarity paths.","triggerScenarios":"Comparing a stored vector of one dimension against a query of another; mixing embeddings from two models (e.g., 384-dim MiniLM vs 1536-dim ada-002) in the same VectorDb; a zero-length vector compared against a non-zero one.","commonSituations":"Migrating embedders without reindexing; multi-tenant stores where tenants use different models; corrupt/truncated vectors loaded from disk.","solutions":["Re-embed the entire corpus when you change embedding dimension.","Enforce a single dimension per VectorDb instance (tag or namespace by model).","Validate vector lengths at insert time so mismatches never reach similarity."],"exampleFix":"// before: mixed-dim store\nconst score = cosineSimilarity(stored.minilmVec, query.adaVec); // 384 vs 1536 -> throws\n\n// after: namespace by model+dim\nconst db384 = new VectorDb(384);\ndb384.add(stored.minilmVec);\nconst score = cosineSimilarity(stored.minilmVec, query.minilmVec);","handlingStrategy":"validation","validationCode":"function cosineSafe(a, b) {\n  if (!a || !b || a.length !== b.length) {\n    throw new Error(`Vector dimension mismatch: ${a?.length} vs ${b?.length}`);\n  }\n  // ... proceed with dot/norm math\n}","typeGuard":"function sameDim(a: Float32Array, b: Float32Array): boolean {\n  return a != null && b != null && a.length === b.length && a.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Re-embed the whole corpus when changing embedder dimension.","Validate length at VectorDb insert time so bad vectors never reach similarity.","Namespace stores by model+dimension to avoid cross-model comparisons."],"tags":["validation","vector-db","embeddings","math"],"backgroundTag":null,"analyzedSha":"6b01dc5a687b26b3e218f796de45ec51f8fa9e8c","analyzedAt":"2026-08-12T13:20:50.148Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}