chroma-core/chroma · error · Error

MinK keys cannot be empty

Error message

MinK keys cannot be empty

What it means

MinK is the 'k smallest values' aggregate used in GroupBy queries. Its constructor validates that at least one key (a field to aggregate over, e.g. 'score') is provided; an empty keys array makes the aggregate meaningless, so it fails fast with a plain Error before the query is serialized.

Source

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

    return new MinK(
      keys.map((key) => (key instanceof Key ? key : new Key(key))),
      k,
    );
  }

  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) {

View on GitHub (pinned to aecdd12c8a)

Solutions

  1. Default to a meaningful key such as 'score' (K.SCORE) when the computed key list comes out empty
  2. Validate keys.length >= 1 before constructing MinK and surface a clearer domain error
  3. Log the computed key list at query-build time to find why it is empty

Example fix

// before
const agg = Aggregate.minK(fields.filter((f) => f.selected), 5); // may be [] -> Error

// after
const selected = fields.filter((f) => f.selected);
const agg = Aggregate.minK(selected.length ? selected : ["score"], 5);
Defensive patterns

Strategy: validation

Validate before calling

const keys = computeAggregateKeys(input);
if (!Array.isArray(keys) || keys.length === 0) {
  throw new Error("At least one aggregate key is required (e.g. 'score')");
}
const agg = Aggregate.minK(keys, 5);

Type guard

const hasNonEmptyKeys = (keys: unknown): keys is string[] =>
  Array.isArray(keys) && keys.length > 0 && keys.every((k) => typeof k === "string");

Prevention

When it happens

Trigger: new MinK([], 5); Aggregate.minK([], 5); or GroupBy built from JSON { keys: ["cat"], aggregate: { $min_k: { keys: [], k: 5 } } }.

Common situations: Dynamically computing the aggregate key list from user input, a config file, or a field-mapping table that unexpectedly returns []. Typos or case mismatches in field names causing an upstream filter to drop every entry.

Related errors


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