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
- Check the TemplateManager contains a template with the exact id 'variable-extraction' and that its content is a non-empty string.
- Restore or re-add the missing template file/entry to whatever source the TemplateManager loads from.
- If you renamed the template, either rename it back or register an alias under 'variable-extraction'.
- 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
- Assert required templates exist during application startup, not lazily at first request.
- Include template files in the deployment bundle and verify with a boot-time smoke check.
- Pin the exact template id 'variable-extraction' via a shared constant to avoid drift.
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
- VariableExtractionModelError(modelKey)
- Evaluation model key must not be empty.
- Evaluation mode configuration must not be empty.
- Sub mode must not be empty.
- Iteration requirement must not be empty.
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/20bac1550882e886.
Report an issue: GitHub.