{"record":{"id":"3bf0916183e33431","repo":"mastra-ai/mastra","slug":"keywords-must-be-greater-than-0","errorCode":null,"errorMessage":"Keywords must be greater than 0","messagePattern":"Keywords must be greater than 0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/rag/src/document/extractors/keywords.ts","lineNumber":34,"sourceCode":"};\n\n/**\n * Extract keywords from a list of nodes.\n */\nexport class KeywordExtractor extends BaseExtractor {\n  llm: MastraLanguageModel | MastraLegacyLanguageModel;\n  keywords: number = 5;\n  promptTemplate: KeywordExtractPrompt;\n\n  /**\n   * Constructor for the KeywordExtractor class.\n   * @param {MastraLanguageModel} llm MastraLanguageModel instance.\n   * @param {number} keywords Number of keywords to extract.\n   * @param {string} [promptTemplate] Optional custom prompt template (must include {context})\n   * @throws {Error} If keywords is less than 1.\n   */\n  constructor(options?: KeywordExtractArgs) {\n    if (options?.keywords && options.keywords < 1) throw new Error('Keywords must be greater than 0');\n\n    super();\n\n    this.llm = options?.llm ?? baseLLM;\n    this.keywords = options?.keywords ?? 5;\n    this.promptTemplate = options?.promptTemplate\n      ? new PromptTemplate({\n          templateVars: ['context', 'maxKeywords'],\n          template: options.promptTemplate,\n        })\n      : defaultKeywordExtractPrompt;\n  }\n\n  /**\n   *\n   * @param node Node to extract keywords from.\n   * @returns Keywords extracted from the node.\n   */","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/rag/src/document/extractors/keywords.ts#L16-L52","documentation":"The KeywordExtractor constructor validates the keywords option: it must be at least 1 because extracting zero or negative keywords is meaningless and would break prompt construction / result slicing. The guard only fires when keywords is provided AND < 1; undefined falls back to the default of 5.","triggerScenarios":"new KeywordExtractor({ keywords: 0 }) or keywords: -3 — typically the value is computed dynamically (e.g. from user input or a config value that is 0 by default) rather than hard-coded.","commonSituations":"Reading 'keyword count' from a UI or settings file where the default is 0, parsing a numeric option from a string that yields 0/NaN-adjacent values, or misconfigured template variables producing 0.","solutions":["Pass a positive integer: new KeywordExtractor({ keywords: 5 }).","Omit the keywords option entirely to use the built-in default of 5.","Clamp/validate the value at the source: Math.max(1, config.keywords) or reject invalid config at load time.","If the count comes from user input, enforce a minimum of 1 in your form/config validation before constructing the extractor."],"exampleFix":"// before\nnew KeywordExtractor({ keywords: config.keywordCount }); // may be 0\n\n// after\nnew KeywordExtractor({ keywords: Math.max(1, config.keywordCount || 5) });","handlingStrategy":"validation","validationCode":"const keywords = options?.keywords;\nif (keywords !== undefined && (!Number.isInteger(keywords) || keywords < 1)) {\n  throw new Error('keywords must be a positive integer');\n}","typeGuard":"function isValidKeywordCount(n: unknown): n is number {\n  return typeof n === 'number' && Number.isInteger(n) && n >= 1;\n}","tryCatchPattern":"try {\n  const extractor = new KeywordExtractor({ keywords });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Keywords must be greater than 0')) {\n    extractor = new KeywordExtractor(); // default of 5\n  } else throw e;\n}","preventionTips":["Omit the keywords option to use the default (5) rather than passing 0.","Clamp dynamic values with Math.max(1, n).","Validate user/config-supplied counts at the boundary before constructing extractors."],"tags":["rag","extractors","validation","configuration","keywords"],"backgroundTag":"invalid-parameter-range","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}