{"record":{"id":"780d73da35f1407b","repo":"mem0ai/mem0","slug":"threshold-must-be-a-valid-number","errorCode":null,"errorMessage":"threshold must be a valid number","messagePattern":"threshold must be a valid number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/memory/index.ts","lineNumber":187,"sourceCode":"      `Invalid ${name}: cannot be empty or whitespace-only. Provide a valid identifier.`,\n    );\n  }\n  if (/\\s/.test(trimmed)) {\n    throw new Error(\n      `Invalid ${name}: cannot contain whitespace. Provide a valid identifier without spaces.`,\n    );\n  }\n  return trimmed;\n}\n\n/**\n * Validates search parameters.\n * @throws Error if threshold or topK are invalid\n */\nfunction validateSearchParams(threshold?: number, topK?: number): void {\n  if (threshold !== undefined) {\n    if (typeof threshold !== \"number\" || isNaN(threshold)) {\n      throw new Error(\"threshold must be a valid number\");\n    }\n    if (threshold < 0 || threshold > 1) {\n      throw new Error(\n        `Invalid threshold: ${threshold}. Must be between 0 and 1 (inclusive).`,\n      );\n    }\n  }\n  if (topK !== undefined) {\n    if (typeof topK !== \"number\" || isNaN(topK) || !Number.isInteger(topK)) {\n      throw new Error(\"topK must be a valid integer\");\n    }\n    if (topK < 0) {\n      throw new Error(`Invalid topK: ${topK}. Must be a non-negative integer.`);\n    }\n  }\n}\n\nexport class Memory {","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/memory/index.ts#L169-L205","documentation":"Thrown by validateSearchParams when the threshold option of Memory.search() is defined but is not a number (wrong type) or is NaN. threshold gates the similarity cutoff for search results, so a non-numeric value — typically a string from env/config files — is rejected before any vector query runs.","triggerScenarios":"Calling memory.search('q', { threshold: '0.5' }) (string from JSON config or query param), threshold: NaN (result of a failed parseFloat), or threshold: null coerced oddly. Any value where typeof !== 'number' or Number.isNaN triggers it.","commonSituations":"Loading search options from JSON/YAML/env where everything arrives as a string ('0.5'), passing req.query.threshold straight from an HTTP handler, or computing threshold with parseFloat on unparsed input that yields NaN.","solutions":["Coerce before calling: threshold: Number(opts.threshold) and check Number.isFinite first.","Validate at the API boundary: reject or default non-numeric threshold in your HTTP/config layer.","If threshold is optional in your app, only include the key when a valid number exists (don't pass undefined-as-string).","Add a unit test for options parsing so string thresholds never reach Memory.search."],"exampleFix":"// before\nconst results = await memory.search(query, { threshold: req.query.threshold }); // '0.5' string -> throws\n\n// after\nconst rawThreshold = req.query.threshold;\nconst threshold = rawThreshold === undefined ? undefined : Number(rawThreshold);\nif (threshold !== undefined && !Number.isFinite(threshold)) {\n  throw new TypeError('threshold must be numeric');\n}\nconst results = await memory.search(query, { threshold });","handlingStrategy":"validation","validationCode":"function parseThreshold(raw: unknown): number | undefined {\n  if (raw === undefined || raw === null) return undefined;\n  const n = Number(raw);\n  if (!Number.isFinite(n)) throw new TypeError('threshold must be numeric');\n  return n;\n}","typeGuard":"function isValidThreshold(t: unknown): t is number {\n  return typeof t === 'number' && !Number.isNaN(t) && t >= 0 && t <= 1;\n}","tryCatchPattern":null,"preventionTips":["Number()-convert any threshold coming from env, JSON, or HTTP before passing it on.","Reject non-numeric threshold in your own API validation with a clear 400.","Only include the threshold key when a valid number exists."],"tags":["validation","search","threshold","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}