chroma-core/chroma · error · ChromaValueError
Number of requested results has to positive
Error message
Number of requested results has to positive
What it means
After the type check, validateNResults requires nResults > 0 (utils.ts:759-761); zero and negative values throw 'Number of requested results has to positive' (the typo is verbatim in the source). The check runs only when nResults is provided to query().
Source
Thrown at clients/new-js/packages/chromadb/src/utils.ts:760
throw new ChromaValueError(`${item} is not allowed for this operation`);
}
});
};
/**
* Validates the number of results parameter for queries.
* @param nResults - Number of results to validate
* @throws ChromaValueError if nResults is not a positive number
*/
export const validateNResults = (nResults: number) => {
if (typeof (nResults as any) !== "number") {
throw new ChromaValueError(
`Expected 'nResults' to be a number, but got ${typeof nResults}`,
);
}
if (nResults <= 0) {
throw new ChromaValueError("Number of requested results has to positive");
}
};
export const parseConnectionPath = (path: string) => {
try {
const url = new URL(path);
const ssl = url.protocol === "https:";
const host = url.hostname;
const port = url.port;
return {
ssl,
host,
port: Number(port),
};
} catch {
throw new ChromaValueError(`Invalid URL: ${path}`);View on GitHub (pinned to aecdd12c8a)
Solutions
- Clamp the value: nResults: Math.max(1, n)
- Skip issuing the query when the computed count is 0
- Validate user-supplied limit >= 1 before calling query()
Example fix
// before
await col.query({ queryTexts, nResults: ids.length }); // 0 when empty
// after
await col.query({ queryTexts, nResults: Math.max(1, ids.length) }); Defensive patterns
Strategy: validation
Validate before calling
const nResults = Math.max(1, Number(rawTopK) || 0);
if (Number.isNaN(nResults) || nResults < 1) {
throw new RangeError('nResults must be >= 1');
} Type guard
const isPositiveNResults = (v: unknown): v is number => typeof v === 'number' && v > 0;
Prevention
- Never derive nResults from a length that can be 0 without clamping
- Validate user-supplied limit/timeout params (>= 1) before building the query
- Skip the query entirely when there is nothing to ask for
When it happens
Trigger: nResults: 0 — commonly computed as someArray.length when the array is empty; negative values parsed from user input such as ?limit=-5.
Common situations: Deriving topK from a variable-length list (e.g. number of query texts) that can be empty; clamping logic that floors to zero.
Related errors
- Expected 'include' to be a non-empty array
- Expected 'nResults' to be a number, but got ${typeof nResult
- Expected metadata to be non-empty
- Expected metadata list value for key '${key}' to be non-empt
- Expected metadata list value for key '${key}' to contain onl
AI-assisted analysis of chroma-core/chroma@aecdd12c8a (2026-08-16).
Data as JSON: /api/errors/9cc707bf4af80dcd.
Report an issue: GitHub.