linshenkx/prompt-optimizer · error · IterationError

Iteration failed: ${errorMessage}

Error message

Iteration failed: ${errorMessage}

What it means

Thrown when the TemplateManager fails while fetching the iteration template (templateId or DEFAULT_TEMPLATES.ITERATE). The underlying error message is embedded into an IterationError prefixed with 'Iteration failed:'. This is a wrapper around template-store lookup/retrieval failures, not a model problem.

Source

Thrown at packages/core/src/services/prompt/service.ts:308

      this.validateInput(lastOptimizedPrompt, modelKey);
      this.validateInput(iterateInput, modelKey);

      // 获取模型配置
      const modelConfig = await this.modelManager.getModel(modelKey);
      if (!modelConfig) {
        throw new ServiceDependencyError("ModelManager", "Model not found");
      }

      // 获取迭代提示词
      let template;
      try {
        template = await this.templateManager.getTemplate(
          templateId || DEFAULT_TEMPLATES.ITERATE,
        );
      } catch (error) {
        const errorMessage =
          error instanceof Error ? error.message : String(error);
        throw new IterationError(
          originalPrompt,
          iterateInput,
          `Iteration failed: ${errorMessage}`,
        );
      }

      if (!template?.content) {
        throw new IterationError(
          originalPrompt,
          iterateInput,
          "Iteration failed: Template not found or invalid",
        );
      }

      // 🔧 迭代功能必须使用高级模板(message array 格式)以支持变量替换
      if (typeof template.content === "string") {
        throw new IterationError(
          originalPrompt,

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Inspect the embedded errorMessage to identify the real template-store failure
  2. Verify the template exists: call templateManager.getTemplate(templateId || DEFAULT_TEMPLATES.ITERATE) directly
  3. Restore/re-seed the default ITERATE template or fix the templateId passed by the caller
  4. If the store is corrupted, repair or reset the template store and retry

Example fix

// before
await promptService.iteratePrompt(lastPrompt, input, modelKey, "my-iter-tpl");

// after
const tplId = "my-iter-tpl";
const tpl = await templateManager.getTemplate(tplId).catch(() => null);
if (!tpl) {
  // fall back to the built-in iteration template
  await promptService.iteratePrompt(lastPrompt, input, modelKey);
} else {
  await promptService.iteratePrompt(lastPrompt, input, modelKey, tplId);
}
Defensive patterns

Strategy: try-catch

Validate before calling

const tpl = await templateManager.getTemplate(templateId || DEFAULT_TEMPLATES.ITERATE).catch(() => null);
if (!tpl) { /* fall back to default or abort early */ }

Type guard

null

Try / catch

try {
  await promptService.iteratePrompt(last, input, modelKey, templateId);
} catch (e) {
  if (e instanceof IterationError && e.message.startsWith("Iteration failed:")) {
    // inspect suffix; retry with default template if template-related
  } else throw e;
}

Prevention

When it happens

Trigger: iteratePrompt with a templateId that does not exist in the TemplateManager, a corrupted/unreadable template store, or the default iterate template being deleted or not seeded. Any exception thrown inside templateManager.getTemplate is caught here and re-wrapped.

Common situations: Passing a custom templateId from stale UI state after the template was deleted; first run where seed templates were not installed; template store file permissions or schema migration issues after an upgrade.

Related errors


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