{"record":{"id":"13bf469268019d69","repo":"linshenkx/prompt-optimizer","slug":"variableextractionmodelerror-modelkey","errorCode":null,"errorMessage":"VariableExtractionModelError(modelKey)","messagePattern":"VariableExtractionModelError\\(modelKey\\)","errorType":"exception","errorClass":"VariableExtractionModelError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-extraction/service.ts","lineNumber":113,"sourceCode":"   * 验证请求参数\n   */\n  private validateRequest(request: VariableExtractionRequest): void {\n    if (!request.promptContent?.trim()) {\n      throw new VariableExtractionValidationError('Prompt content must not be empty.');\n    }\n\n    if (!request.extractionModelKey?.trim()) {\n      throw new VariableExtractionValidationError('Extraction model key must not be empty.');\n    }\n  }\n\n  /**\n   * 验证模型存在性\n   */\n  private async validateModel(modelKey: string): Promise<void> {\n    const model = await this.modelManager.getModel(modelKey);\n    if (!model) {\n      throw new VariableExtractionModelError(modelKey);\n    }\n  }\n\n  /**\n   * 获取提示词模板 (统一模板)\n   */\n  private async getExtractionTemplate(): Promise<Template> {\n    const templateId = 'variable-extraction';\n\n    try {\n      const template = await this.templateManager.getTemplate(templateId);\n      if (!template?.content) {\n        throw new VariableExtractionExecutionError(`Template \"${templateId}\" not found or empty.`);\n      }\n      return template;\n    } catch (error) {\n      if (error instanceof VariableExtractionError) {\n        throw error","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-extraction/service.ts#L95-L131","documentation":"Thrown by the variable-extraction service when the model key passed to extract() does not resolve to a registered model in the ModelManager. It means the LLM backend that should power variable extraction was never registered, was removed, or the key is misspelled. The check happens up front in validateModel() before any template or LLM call is made.","triggerScenarios":"Calling extract() (or any API that internally calls it) with a modelKey that is not registered via modelManager.registerModel / getModel returns undefined. This includes typos in the key, using a provider alias that was never configured, or calling extract() before models are initialized.","commonSituations":"Config file references a model that the app never registers at boot; renaming a model key but not updating extraction config; running in a test environment where only a mock model manager exists; ordering issue where extraction service is constructed before model registration completes.","solutions":["Verify the modelKey string passed to extract() exactly matches a key registered in the ModelManager (check case/spaces).","Log modelManager.getModel(key) yourself before calling extract() to confirm it returns undefined.","Ensure model registration/initialization code runs before the first extract() call (check startup ordering or lazy-init race).","If the model was renamed, update the variable-extraction config to the new key or re-register under the old key."],"exampleFix":"// before\nconst result = await extractionService.extract({ modelKey: 'gpt4-turbo', /* ... */ });\n\n// after\nconst model = await modelManager.getModel('gpt4-turbo');\nif (!model) throw new Error(`Model 'gpt4-turbo' not registered; available: ${await modelManager.listModels()}`);\nconst result = await extractionService.extract({ modelKey: 'gpt4-turbo', /* ... */ });","handlingStrategy":"validation","validationCode":"const model = await modelManager.getModel(modelKey);\nif (!model) {\n  throw new Error(`modelKey '${modelKey}' is not registered`);\n}\nconst result = await extractionService.extract({ modelKey, content });","typeGuard":"const isRegisteredModelKey = (models: string[], key: string): boolean => models.includes(key);","tryCatchPattern":"try {\n  await extractionService.extract({ modelKey, content });\n} catch (e) {\n  if (e instanceof VariableExtractionModelError) {\n    // config problem: fix model registration, do not retry blindly\n  }\n  throw e;\n}","preventionTips":["Validate modelKey against the registered model list before calling extract().","Register all models in a single startup step that completes before extraction requests are served.","Fail fast at boot with a health check that asserts the configured extraction model exists."],"tags":["model-registry","configuration","variable-extraction"],"backgroundTag":"model-key-not-found","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}