ruvnet/ruflo · error · Error

Vector dimension mismatch: ${a.length} vs ${b.length}

Error message

Vector dimension mismatch: ${a.length} vs ${b.length}

What it means

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.

Source

Thrown at v3/@claude-flow/cli/src/ruvector/vector-db.ts:85

  remove(id: string): boolean {
    return this.vectors.delete(id);
  }

  size(): number {
    return this.vectors.size;
  }

  clear(): void {
    this.vectors.clear();
  }
}

/**
 * Compute cosine similarity between two vectors
 */
function cosineSimilarity(a: Float32Array, b: Float32Array): number {
  if (a.length !== b.length) {
    throw new Error(`Vector dimension mismatch: ${a.length} vs ${b.length}`);
  }

  let dotProduct = 0;
  let normA = 0;
  let normB = 0;

  for (let i = 0; i < a.length; i++) {
    dotProduct += a[i] * b[i];
    normA += a[i] * a[i];
    normB += b[i] * b[i];
  }

  const denom = Math.sqrt(normA) * Math.sqrt(normB);
  return denom === 0 ? 0 : dotProduct / denom;
}

/**
 * Whether the hash-embedding one-time warning has been emitted

View on GitHub (pinned to 6b01dc5a68)

Solutions

  1. Re-embed the entire corpus when you change embedding dimension.
  2. Enforce a single dimension per VectorDb instance (tag or namespace by model).
  3. Validate vector lengths at insert time so mismatches never reach similarity.

Example fix

// before: mixed-dim store
const score = cosineSimilarity(stored.minilmVec, query.adaVec); // 384 vs 1536 -> throws

// after: namespace by model+dim
const db384 = new VectorDb(384);
db384.add(stored.minilmVec);
const score = cosineSimilarity(stored.minilmVec, query.minilmVec);
Defensive patterns

Strategy: validation

Validate before calling

function cosineSafe(a, b) {
  if (!a || !b || a.length !== b.length) {
    throw new Error(`Vector dimension mismatch: ${a?.length} vs ${b?.length}`);
  }
  // ... proceed with dot/norm math
}

Type guard

function sameDim(a: Float32Array, b: Float32Array): boolean {
  return a != null && b != null && a.length === b.length && a.length > 0;
}

Prevention

When it happens

Trigger: 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.

Common situations: Migrating embedders without reindexing; multi-tenant stores where tenants use different models; corrupt/truncated vectors loaded from disk.

Related errors


AI-assisted analysis of ruvnet/ruflo@6b01dc5a68 (2026-08-12). Data as JSON: /api/errors/9d2b4ce5439ab5df. Report an issue: GitHub.