linshenkx/prompt-optimizer · error · VariableExtractionModelError

VariableExtractionModelError(modelKey)

Error message

VariableExtractionModelError(modelKey)

What it means

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.

Source

Thrown at packages/core/src/services/variable-extraction/service.ts:113

   * 验证请求参数
   */
  private validateRequest(request: VariableExtractionRequest): void {
    if (!request.promptContent?.trim()) {
      throw new VariableExtractionValidationError('Prompt content must not be empty.');
    }

    if (!request.extractionModelKey?.trim()) {
      throw new VariableExtractionValidationError('Extraction model key must not be empty.');
    }
  }

  /**
   * 验证模型存在性
   */
  private async validateModel(modelKey: string): Promise<void> {
    const model = await this.modelManager.getModel(modelKey);
    if (!model) {
      throw new VariableExtractionModelError(modelKey);
    }
  }

  /**
   * 获取提示词模板 (统一模板)
   */
  private async getExtractionTemplate(): Promise<Template> {
    const templateId = 'variable-extraction';

    try {
      const template = await this.templateManager.getTemplate(templateId);
      if (!template?.content) {
        throw new VariableExtractionExecutionError(`Template "${templateId}" not found or empty.`);
      }
      return template;
    } catch (error) {
      if (error instanceof VariableExtractionError) {
        throw error

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Verify the modelKey string passed to extract() exactly matches a key registered in the ModelManager (check case/spaces).
  2. Log modelManager.getModel(key) yourself before calling extract() to confirm it returns undefined.
  3. Ensure model registration/initialization code runs before the first extract() call (check startup ordering or lazy-init race).
  4. If the model was renamed, update the variable-extraction config to the new key or re-register under the old key.

Example fix

// before
const result = await extractionService.extract({ modelKey: 'gpt4-turbo', /* ... */ });

// after
const model = await modelManager.getModel('gpt4-turbo');
if (!model) throw new Error(`Model 'gpt4-turbo' not registered; available: ${await modelManager.listModels()}`);
const result = await extractionService.extract({ modelKey: 'gpt4-turbo', /* ... */ });
Defensive patterns

Strategy: validation

Validate before calling

const model = await modelManager.getModel(modelKey);
if (!model) {
  throw new Error(`modelKey '${modelKey}' is not registered`);
}
const result = await extractionService.extract({ modelKey, content });

Type guard

const isRegisteredModelKey = (models: string[], key: string): boolean => models.includes(key);

Try / catch

try {
  await extractionService.extract({ modelKey, content });
} catch (e) {
  if (e instanceof VariableExtractionModelError) {
    // config problem: fix model registration, do not retry blindly
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/13bf469268019d69. Report an issue: GitHub.