{"record":{"id":"088835352438186e","repo":"n8n-io/n8n","slug":"topk-must-be-an-integer-1-got-k","errorCode":null,"errorMessage":"topK must be an integer >= 1, got ${k}","messagePattern":"topK must be an integer >= 1, got (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/agents/src/sdk/vector-store.ts","lineNumber":190,"sourceCode":"\t\t\tthrow new Error(\n\t\t\t\t`VectorStore \"${this.name}\" requires an embedding model — set it via .embeddingModel()`,\n\t\t\t);\n\t\t}\n\t\treturn { backend: this.backend, embeddingModel: this.embeddingModelValue };\n\t}\n\n\t/** Normalizes and validates a filter; returns `undefined` for an empty one so it's never a no-op `WHERE`. */\n\tprivate resolveFilter(input?: VectorFilterInput): VectorFilter | undefined {\n\t\tif (input === undefined) return undefined;\n\t\tconst normalized = normalizeFilterInput(input);\n\t\tassertValidFilter(normalized);\n\t\treturn normalized.conditions.length > 0 ? normalized : undefined;\n\t}\n}\n\nfunction assertValidTopK(k: number): void {\n\tif (!Number.isInteger(k) || k < 1) {\n\t\tthrow new Error(`topK must be an integer >= 1, got ${k}`);\n\t}\n}\n","sourceCodeStart":172,"sourceCodeEnd":193,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/agents/src/sdk/vector-store.ts#L172-L193","documentation":"Thrown by assertValidTopK() whenever a topK value is not a positive integer. Validation runs both eagerly in the .topK(k) builder and again on the per-call opts.topK passed to search(). The check is Number.isInteger(k) && k >= 1, so fractional numbers, zero, negatives, NaN, and Infinity all fail. This protects the underlying LIMIT/topK passed to each backend.","triggerScenarios":"Calling `store.topK(0)`, `store.topK(2.5)`, `store.topK(-1)`, or `store.topK(NaN)`; calling `store.search('q', { topK: Math.round(score) })` where score rounds to 0; reading topK from user/config input parsed as a float; spreading a default of 0 that was meant as 'unset'.","commonSituations":"Config/UI slider that allows 0 as 'no limit'; JSON config with topK: 0 mistaken for 'use default'; arithmetic that produces a fractional result (e.g. count/2); coerced string input `Number('abc')` yielding NaN.","solutions":["Pass a positive integer literal or a value you have clamped: `Math.max(1, Math.floor(value))`.","If 0/'unset' must be supported in your config layer, translate it to the default (4) or omit the topK option entirely so the builder default applies.","Validate external topK input with a guard before calling .topK() or search()."],"exampleFix":"// before\nstore.topK(0);\nawait store.search('q', { topK: 2.5 });\n\n// after\nstore.topK(5);\nawait store.search('q', { topK: Math.max(1, Math.floor(config.limit)) });","handlingStrategy":"validation","validationCode":"function safeTopK(value: unknown): number {\n  const n = typeof value === 'number' ? value : Number(value);\n  if (!Number.isInteger(n) || n < 1) {\n    throw new Error(`Invalid topK ${String(value)}: must be an integer >= 1`);\n  }\n  return n;\n}\n\n// Usage:\nstore.topK(safeTopK(config.limit));\nawait store.search('q', { topK: safeTopK(req.body.limit) });","typeGuard":"function isValidTopK(value: unknown): value is number {\n  return typeof value === 'number' && Number.isInteger(value) && value >= 1;\n}\n\nif (isValidTopK(input)) {\n  store.topK(input);\n}","tryCatchPattern":"if (opts?.topK !== undefined && (!Number.isInteger(opts.topK) || opts.topK < 1)) {\n  return res.status(400).json({ error: 'topK must be an integer >= 1' });\n}\nawait store.search(query, opts);","preventionTips":["Coerce all external topK input through a single validator before passing it to .topK()/search().","Treat config value 0/'unset' as 'use the default (4)' by omitting the option rather than passing 0.","Type your API surface as number and add a runtime guard — TypeScript alone won't stop NaN or floats at runtime."],"tags":["vector-store","validation","sdk"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}