{"record":{"id":"28cb6c6c25cffd51","repo":"chroma-core/chroma","slug":"maxk-keys-cannot-be-empty","errorCode":null,"errorMessage":"MaxK keys cannot be empty","messagePattern":"MaxK keys cannot be empty","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clients/new-js/packages/chromadb/src/execution/expression/groupBy.ts","lineNumber":86,"sourceCode":"      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) {\n    super();\n    if (keys.length === 0) {\n      throw new Error(\"MaxK keys cannot be empty\");\n    }\n    if (k <= 0) {\n      throw new Error(\"MaxK k must be positive\");\n    }\n  }\n\n  public toJSON(): AggregateJSON {\n    return {\n      $max_k: {\n        keys: this.keys.map((key) => key.name),\n        k: this.k,\n      },\n    };\n  }\n}\n\nexport interface GroupByJSON {\n  keys: string[];","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/clients/new-js/packages/chromadb/src/execution/expression/groupBy.ts#L68-L104","documentation":"MaxK is the 'k largest values' aggregate used in GroupBy queries. Its constructor validates that at least one key (the field to aggregate over) is provided; an empty keys array fails fast with a plain Error before the query is serialized.","triggerScenarios":"new MaxK([], 10); Aggregate.maxK([], 10); or GroupBy built from JSON { keys: [\"cat\"], aggregate: { $max_k: { keys: [], k: 10 } } }.","commonSituations":"Dynamically deriving aggregate keys from a metadata schema or user-selected columns and getting an empty list. Reusing a MinK code path for MaxK but passing the grouping keys (already consumed) instead of the aggregate keys.","solutions":["Default to a meaningful key such as 'score' (K.SCORE) when the computed key list is empty","Validate keys.length >= 1 before constructing MaxK","Log the keys array at query-build time to see why it is empty"],"exampleFix":"// before\nconst agg = Aggregate.maxK(selectedFields.filter(Boolean), 10); // may be [] -> Error\n\n// after\nconst keys = selectedFields.filter(Boolean);\nconst agg = Aggregate.maxK(keys.length ? keys : [\"score\"], 10);","handlingStrategy":"validation","validationCode":"const keys = selectedFields.filter(Boolean);\nif (keys.length === 0) {\n  throw new Error(\"At least one aggregate key is required (e.g. 'score')\");\n}\nconst agg = Aggregate.maxK(keys, 10);","typeGuard":"const hasNonEmptyKeys = (keys: unknown): keys is string[] =>\n  Array.isArray(keys) && keys.length > 0;","tryCatchPattern":null,"preventionTips":["Default empty key lists to ['score']","Don't reuse the group-by keys array for the aggregate keys — they are different fields","Log computed key lists during development to spot empties"],"tags":["chroma","group-by","maxk","validation"],"backgroundTag":"empty-array-argument","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}