chroma-core/chroma · error · Error
MaxK k must be positive
Error message
MaxK k must be positive
What it means
MaxK's constructor requires k (the number of largest values to return) to be a positive integer. Any k <= 0 — including 0 — is rejected with a plain Error at construction time, before the query is serialized or sent.
Source
Thrown at clients/new-js/packages/chromadb/src/execution/expression/groupBy.ts:89
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");
}
if (k <= 0) {
throw new Error("MaxK k must be positive");
}
}
public toJSON(): AggregateJSON {
return {
$max_k: {
keys: this.keys.map((key) => key.name),
k: this.k,
},
};
}
}
export interface GroupByJSON {
keys: string[];
aggregate: AggregateJSON;
}
View on GitHub (pinned to aecdd12c8a)
Solutions
- Pass a positive integer for k (k >= 1)
- Clamp external input: Math.max(1, Math.floor(Number(topN) || 10))
- Document that 'unlimited' is not expressible via k; pick an explicit bound instead
Example fix
// before const agg = Aggregate.maxK(["score"], Number(req.query.top)); // top=0 -> Error // after const top = Math.max(1, Math.floor(Number(req.query.top) || 10)); const agg = Aggregate.maxK(["score"], top);
Defensive patterns
Strategy: validation
Validate before calling
const top = Number(req.query.top);
if (!Number.isInteger(top) || top <= 0) {
throw new Error(`top must be a positive integer, got ${req.query.top}`);
}
const agg = Aggregate.maxK(["score"], top); Type guard
const isPositiveInt = (v: unknown): v is number => typeof v === "number" && Number.isInteger(v) && v > 0;
Prevention
- Clamp external top-N input: Math.max(1, Math.floor(Number(x) || 10))
- Reject ?top=0 at the request-validation layer with a clear message
- Remember there is no 'unlimited' k — pick an explicit bound
When it happens
Trigger: new MaxK([K.SCORE], 0); Aggregate.maxK(["score"], -3); or aggregate JSON { $max_k: { keys: ["score"], k: 0 } } parsed by Aggregate.from.
Common situations: k sourced from a request query parameter that defaults to 0 (e.g. ?top=0). Using 0 to mean 'all results' — in Chroma aggregates, k must be an explicit positive count. Fractional values from dividing counts (k: 10 / 3) also fail the integer positivity requirement downstream.
Related errors
- MinK k must be positive
- MaxK keys cannot be empty
- Aggregate input must be an Aggregate instance or object with
- MinK keys cannot be empty
- GroupBy keys cannot be empty
AI-assisted analysis of chroma-core/chroma@aecdd12c8a (2026-08-16).
Data as JSON: /api/errors/d1c347e06a1fae4d.
Report an issue: GitHub.