chroma-core/chroma · error · Error

Number of weights must match number of ranks

Error message

Number of weights must match number of ranks

What it means

Thrown by Rrf (rank.ts:476) when the optional `weights` array length differs from the `ranks` array length. Weights are applied per-rank in the fusion formula, so the two lists must correspond one-to-one. This is a plain Error (not a TypeError) and is raised before weight values themselves are validated.

Source

Thrown at clients/new-js/packages/chromadb/src/execution/expression/rank.ts:476

  weights,
  normalize = false,
}: RrfOptions): RankExpression => {
  if (!Number.isInteger(k) || k <= 0) {
    throw new TypeError("Rrf k must be a positive integer");
  }
  if (!Array.isArray(ranks) || ranks.length === 0) {
    throw new TypeError("Rrf requires at least one rank expression");
  }

  const expressions = ranks.map((rank, index) =>
    requireRank(rank, `ranks[${index}]`),
  );

  let weightValues = weights
    ? weights.slice()
    : new Array(expressions.length).fill(1);
  if (weightValues.length !== expressions.length) {
    throw new Error("Number of weights must match number of ranks");
  }
  if (weightValues.some((value) => typeof value !== "number" || value < 0)) {
    throw new TypeError("Weights must be non-negative numbers");
  }

  if (normalize) {
    const total = weightValues.reduce((sum, value) => sum + value, 0);
    if (total <= 0) {
      throw new Error(
        "Weights must sum to a positive value when normalize=true",
      );
    }
    weightValues = weightValues.map((value) => value / total);
  }

  const terms = expressions.map((rank, index) => {
    const weight = weightValues[index];
    const numerator = Val(weight);

View on GitHub (pinned to aecdd12c8a)

Solutions

  1. Match lengths: supply one weight per rank, e.g. Rrf({ ranks: [a, b], weights: [0.7, 0.3] })
  2. For equal weighting, omit weights entirely — the default is all 1s
  3. Derive weights from the ranks list itself (e.g. ranks.map((_, i) => config[i] ?? 1)) so they cannot drift

Example fix

// before
const fused = Rrf({ ranks: [bm25, dense, mmr], weights: [0.5, 0.5] });

// after
const fused = Rrf({ ranks: [bm25, dense, mmr], weights: [0.4, 0.4, 0.2] });
// or omit weights for uniform weighting
Defensive patterns

Strategy: validation

Validate before calling

const weights = configuredWeights.length === ranks.length
  ? configuredWeights
  : undefined; // fall back to uniform
const fused = Rrf({ ranks, weights });

Try / catch

try {
  const fused = Rrf({ ranks, weights });
} catch (e) {
  if (e instanceof Error && e.message.includes('must match number of ranks')) {
    return Rrf({ ranks }); // uniform weights
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling Rrf({ ranks: [a, b, c], weights: [1, 1] }) — three rankers but two weights; adding a retriever without extending the weights array; weights built from a different source (config keyed by feature list) that drifted out of sync with the ranks list.

Common situations: Hard-coded weight arrays next to dynamically assembled retriever lists; feature-flag code that appends a ranker but not its weight; refactoring that reorders one array but not the other; copy-pasted weights from an example with a different retriever count.

Related errors


AI-assisted analysis of chroma-core/chroma@aecdd12c8a (2026-08-16). Data as JSON: /api/errors/c1033f3c7ccf09f5. Report an issue: GitHub.