{"record":{"id":"9ab05a22cf886f3e","repo":"mem0ai/mem0","slug":"topk-must-be-a-valid-integer","errorCode":null,"errorMessage":"topK must be a valid integer","messagePattern":"topK must be a valid integer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/memory/index.ts","lineNumber":197,"sourceCode":"\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 {\n  private config: MemoryConfig;\n  private customInstructions: string | undefined;\n  private embedder: Embedder;\n  private vectorStore!: VectorStore;\n  private llm: LLM;\n  private reranker: Reranker | null = null;\n  private db: HistoryManager;\n  private collectionName: string | undefined;\n  private apiVersion: string;\n  telemetryId: string;","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/memory/index.ts#L179-L215","documentation":"Thrown by validateSearchParams when topK is defined but is not an integer number — it checks typeof number, non-NaN, and Number.isInteger. Floating-point or string topK values (e.g. '10' or 5.5) are rejected because a fractional result count is meaningless for the vector query.","triggerScenarios":"Calling memory.search('q', { topK: '10' }) (string from env/JSON), topK: 5.5, or topK: NaN from bad arithmetic such as parseInt on undefined producing NaN.","commonSituations":"Reading topK from environment variables or request queries without Number() conversion; dividing a desired count (e.g. limit/2 when limit is odd) producing a float; config files parsed as strings.","solutions":["Convert and floor before calling: topK: Math.floor(Number(opts.topK)).","Validate inputs at your API boundary and reject non-integer topK with your own 400 error.","Guard arithmetic: ensure any computed topK passes Number.isInteger before use.","Only include topK in the options when a valid integer is available."],"exampleFix":"// before\nawait memory.search('q', { topK: process.env.TOP_K }); // string '10' -> throws\n\n// after\nconst topK = Number.parseInt(process.env.TOP_K ?? '10', 10);\nawait memory.search('q', { topK }); // integer 10","handlingStrategy":"validation","validationCode":"function parseTopK(raw: unknown): number | undefined {\n  if (raw === undefined || raw === null || raw === '') return undefined;\n  const n = Number(raw);\n  if (!Number.isInteger(n)) throw new TypeError('topK must be an integer');\n  return n;\n}","typeGuard":"function isValidTopK(k: unknown): k is number {\n  return typeof k === 'number' && Number.isInteger(k) && k >= 0;\n}","tryCatchPattern":null,"preventionTips":["parseInt/Number-convert topK from env/query strings before calling search.","Floor any computed topK (e.g. from divisions) at the call site.","Omit topK when you don't have a valid integer rather than passing junk."],"tags":["validation","search","topk","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}