chroma-core/chroma · error · Error

MinK k must be positive

Error message

MinK k must be positive

What it means

MinK's constructor requires k (the number of smallest values to return) to be a positive integer. k <= 0 — including 0 — is rejected with a plain Error at construction time, because 'return the smallest 0 values' is almost always a misconfiguration rather than an intent.

Source

Thrown at clients/new-js/packages/chromadb/src/execution/expression/groupBy.ts:68

    );
  }

  public static maxK(keys: (Key | string)[], k: number): MaxK {
    return new MaxK(
      keys.map((key) => (key instanceof Key ? key : new Key(key))),
      k,
    );
  }
}

export class MinK extends Aggregate {
  constructor(public readonly keys: Key[], public readonly k: number) {
    super();
    if (keys.length === 0) {
      throw new Error("MinK keys cannot be empty");
    }
    if (k <= 0) {
      throw new Error("MinK k must be positive");
    }
  }

  public toJSON(): AggregateJSON {
    return {
      $min_k: {
        keys: this.keys.map((key) => key.name),
        k: this.k,
      },
    };
  }
}

export class MaxK extends Aggregate {
  constructor(public readonly keys: Key[], public readonly k: number) {
    super();
    if (keys.length === 0) {
      throw new Error("MaxK keys cannot be empty");

View on GitHub (pinned to aecdd12c8a)

Solutions

  1. Pass a positive integer for k (k >= 1)
  2. If k comes from config, coerce and clamp: Math.max(1, Number(process.env.TOP_K) || 5)
  3. Treat 'unset' as a sensible default (e.g. 5) rather than 0

Example fix

// before
const agg = Aggregate.minK(["score"], Number(cfg.topK)); // cfg.topK = 0 -> Error

// after
const topK = Math.max(1, Number(cfg.topK) || 5);
const agg = Aggregate.minK(["score"], topK);
Defensive patterns

Strategy: validation

Validate before calling

const k = Number(cfg.topK);
if (!Number.isInteger(k) || k <= 0) {
  throw new Error(`topK must be a positive integer, got ${cfg.topK}`);
}
const agg = Aggregate.minK(["score"], k);

Type guard

const isPositiveInt = (v: unknown): v is number =>
  typeof v === "number" && Number.isInteger(v) && v > 0;

Prevention

When it happens

Trigger: new MinK([K.SCORE], 0); Aggregate.minK(["score"], -1); or aggregate JSON { $min_k: { keys: ["score"], k: 0 } } parsed by Aggregate.from.

Common situations: k read from an environment variable or config file that defaults to 0 when unset (parseInt(undefined) → NaN also fails the <= 0 check after NaN comparisons; explicit 0 is the classic case). App semantics where 0 means 'no limit' — here it must be a positive count instead.

Related errors


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