{"record":{"id":"9cc707bf4af80dcd","repo":"chroma-core/chroma","slug":"number-of-requested-results-has-to-positive","errorCode":null,"errorMessage":"Number of requested results has to positive","messagePattern":"Number of requested results has to positive","errorType":"validation","errorClass":"ChromaValueError","httpStatus":null,"severity":"error","filePath":"clients/new-js/packages/chromadb/src/utils.ts","lineNumber":760,"sourceCode":"      throw new ChromaValueError(`${item} is not allowed for this operation`);\n    }\n  });\n};\n\n/**\n * Validates the number of results parameter for queries.\n * @param nResults - Number of results to validate\n * @throws ChromaValueError if nResults is not a positive number\n */\nexport const validateNResults = (nResults: number) => {\n  if (typeof (nResults as any) !== \"number\") {\n    throw new ChromaValueError(\n      `Expected 'nResults' to be a number, but got ${typeof nResults}`,\n    );\n  }\n\n  if (nResults <= 0) {\n    throw new ChromaValueError(\"Number of requested results has to positive\");\n  }\n};\n\nexport const parseConnectionPath = (path: string) => {\n  try {\n    const url = new URL(path);\n\n    const ssl = url.protocol === \"https:\";\n    const host = url.hostname;\n    const port = url.port;\n\n    return {\n      ssl,\n      host,\n      port: Number(port),\n    };\n  } catch {\n    throw new ChromaValueError(`Invalid URL: ${path}`);","sourceCodeStart":742,"sourceCodeEnd":778,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/clients/new-js/packages/chromadb/src/utils.ts#L742-L778","documentation":"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().","triggerScenarios":"nResults: 0 — commonly computed as someArray.length when the array is empty; negative values parsed from user input such as ?limit=-5.","commonSituations":"Deriving topK from a variable-length list (e.g. number of query texts) that can be empty; clamping logic that floors to zero.","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()"],"exampleFix":"// before\nawait col.query({ queryTexts, nResults: ids.length }); // 0 when empty\n\n// after\nawait col.query({ queryTexts, nResults: Math.max(1, ids.length) });","handlingStrategy":"validation","validationCode":"const nResults = Math.max(1, Number(rawTopK) || 0);\nif (Number.isNaN(nResults) || nResults < 1) {\n  throw new RangeError('nResults must be >= 1');\n}","typeGuard":"const isPositiveNResults = (v: unknown): v is number =>\n  typeof v === 'number' && v > 0;","tryCatchPattern":null,"preventionTips":["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"],"tags":["javascript","validation","nresults","query-options"],"backgroundTag":"invalid-query-parameters","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}