linshenkx/prompt-optimizer · error · VariableExtractionExecutionError

Template "${templateId}" not found or empty.

Error message

Template "${templateId}" not found or empty.

What it means

The variable-extraction service requires a prompt template with id 'variable-extraction' to be loaded from the TemplateManager. This error means the template lookup returned an object with no content (or nothing at all), so extraction cannot proceed because there is no prompt to send to the LLM.

Source

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

   * 验证模型存在性
   */
  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
      }
      // Preserve structured template errors if possible (code/params).
      if (typeof (error as any)?.code === 'string') {
        throw toErrorWithCode(error)
      }
      throw new VariableExtractionExecutionError(
        `Failed to get template "${templateId}": ${error instanceof Error ? error.message : String(error)}`,
      )
    }
  }

  /**
   * 构建模板上下文

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Check the TemplateManager contains a template with the exact id 'variable-extraction' and that its content is a non-empty string.
  2. Restore or re-add the missing template file/entry to whatever source the TemplateManager loads from.
  3. If you renamed the template, either rename it back or register an alias under 'variable-extraction'.
  4. Log await templateManager.getTemplate('variable-extraction') before extract() to inspect what is actually returned.

Example fix

// before
const result = await extractionService.extract({ modelKey, content });

// after
const tpl = await templateManager.getTemplate('variable-extraction');
if (!tpl?.content) throw new Error('variable-extraction template missing — check template registration');
const result = await extractionService.extract({ modelKey, content });
Defensive patterns

Strategy: validation

Validate before calling

const tpl = await templateManager.getTemplate('variable-extraction');
if (!tpl?.content) {
  throw new Error('variable-extraction template not loaded — check template registration');
}
const result = await extractionService.extract({ modelKey, content });

Type guard

const hasTemplateContent = (t: unknown): t is { content: string } =>
  !!t && typeof t === 'object' && typeof (t as any).content === 'string' && (t as any).content.length > 0;

Try / catch

try {
  await extractionService.extract({ modelKey, content });
} catch (e) {
  if (e instanceof VariableExtractionExecutionError && /not found or empty/.test(e.message)) {
    // template not registered: fix template setup
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling extract() when the TemplateManager has no template registered under the id 'variable-extraction', or the registered template exists but its content field is null/empty string. Typically happens when template loading/registration is skipped or the template bundle is incomplete.

Common situations: Template files missing from the deployment bundle (packaging excluded the templates dir); templates loaded from a directory where variable-extraction.md was renamed or deleted; template registered under a different id (e.g. 'variable_extraction' with underscore); content loaded but empty due to a failed file read that was swallowed.

Related errors


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