{"record":{"id":"101ec3b0fc787722","repo":"chroma-core/chroma","slug":"mink-keys-cannot-be-empty","errorCode":null,"errorMessage":"MinK keys cannot be empty","messagePattern":"MinK keys cannot be empty","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clients/new-js/packages/chromadb/src/execution/expression/groupBy.ts","lineNumber":65,"sourceCode":"    return new MinK(\n      keys.map((key) => (key instanceof Key ? key : new Key(key))),\n      k,\n    );\n  }\n\n  public static maxK(keys: (Key | string)[], k: number): MaxK {\n    return new MaxK(\n      keys.map((key) => (key instanceof Key ? key : new Key(key))),\n      k,\n    );\n  }\n}\n\nexport class MinK extends Aggregate {\n  constructor(public readonly keys: Key[], public readonly k: number) {\n    super();\n    if (keys.length === 0) {\n      throw new Error(\"MinK keys cannot be empty\");\n    }\n    if (k <= 0) {\n      throw new Error(\"MinK k must be positive\");\n    }\n  }\n\n  public toJSON(): AggregateJSON {\n    return {\n      $min_k: {\n        keys: this.keys.map((key) => key.name),\n        k: this.k,\n      },\n    };\n  }\n}\n\nexport class MaxK extends Aggregate {\n  constructor(public readonly keys: Key[], public readonly k: number) {","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/clients/new-js/packages/chromadb/src/execution/expression/groupBy.ts#L47-L83","documentation":"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.","triggerScenarios":"new MinK([], 5); Aggregate.minK([], 5); or GroupBy built from JSON { keys: [\"cat\"], aggregate: { $min_k: { keys: [], k: 5 } } }.","commonSituations":"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.","solutions":["Default to a meaningful key such as 'score' (K.SCORE) when the computed key list comes out empty","Validate keys.length >= 1 before constructing MinK and surface a clearer domain error","Log the computed key list at query-build time to find why it is empty"],"exampleFix":"// before\nconst agg = Aggregate.minK(fields.filter((f) => f.selected), 5); // may be [] -> Error\n\n// after\nconst selected = fields.filter((f) => f.selected);\nconst agg = Aggregate.minK(selected.length ? selected : [\"score\"], 5);","handlingStrategy":"validation","validationCode":"const keys = computeAggregateKeys(input);\nif (!Array.isArray(keys) || keys.length === 0) {\n  throw new Error(\"At least one aggregate key is required (e.g. 'score')\");\n}\nconst agg = Aggregate.minK(keys, 5);","typeGuard":"const hasNonEmptyKeys = (keys: unknown): keys is string[] =>\n  Array.isArray(keys) && keys.length > 0 && keys.every((k) => typeof k === \"string\");","tryCatchPattern":null,"preventionTips":["Default dynamically computed key lists to ['score'] when empty","Validate user-selected aggregate fields server-side before query build","Unit-test the query builder with empty selections to catch regressions early"],"tags":["chroma","group-by","mink","validation"],"backgroundTag":"empty-array-argument","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}