{"record":{"id":"1d4c48ac261ee619","repo":"chroma-core/chroma","slug":"expected-nresults-to-be-a-number-but-got-type","errorCode":null,"errorMessage":"Expected 'nResults' to be a number, but got ${typeof nResults}","messagePattern":"Expected 'nResults' to be a number, but got (.+?)","errorType":"validation","errorClass":"ChromaValueError","httpStatus":null,"severity":"error","filePath":"clients/new-js/packages/chromadb/src/utils.ts","lineNumber":754,"sourceCode":"          \", \",\n        )}, but got ${item}`,\n      );\n    }\n\n    if (exclude?.includes(item)) {\n      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 {","sourceCodeStart":736,"sourceCodeEnd":772,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/clients/new-js/packages/chromadb/src/utils.ts#L736-L772","documentation":"query()'s nResults is validated with typeof === 'number' (utils.ts:752-757) inside prepareQuery. A string '10', NaN, or any non-number throws ChromaValueError reporting the actual typeof in the message. No string-to-number coercion is performed, unlike lenient HTTP-layer APIs.","triggerScenarios":"collection.query({ queryTexts: [...], nResults: '10' }) — typical when topK comes from process.env, a URL query parameter, or JSON config; nResults: NaN produced by Number(undefined).","commonSituations":"Reading topK from environment variables or request strings and passing it unconverted; default-parameter logic that yields undefined cast to any.","solutions":["Convert at the boundary: nResults: Number(raw)","Guard non-finite values: Number.isFinite(n) ? n : 10","Type the query options with the package's exported types so strings are rejected at compile time"],"exampleFix":"// before\nawait col.query({ queryTexts, nResults: process.env.TOP_K }); // string\n\n// after\nawait col.query({ queryTexts, nResults: Number(process.env.TOP_K) });","handlingStrategy":"validation","validationCode":"const nResults = Number(rawTopK);\nif (!Number.isFinite(nResults)) throw new TypeError(`nResults must be a finite number, got ${rawTopK}`);\nawait col.query({ queryTexts, nResults });","typeGuard":"const isValidNResults = (v: unknown): v is number =>\n  typeof v === 'number' && Number.isFinite(v) && v > 0;","tryCatchPattern":null,"preventionTips":["Convert env/URL-sourced values with Number() at the boundary, never pass them raw","Reject NaN explicitly with Number.isFinite before the call","Type query options with the package's exported types"],"tags":["javascript","typescript","validation","nresults","type-mismatch"],"backgroundTag":"invalid-query-parameters","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}