linshenkx/prompt-optimizer · error · VariableValueGenerationExecutionError

error instanceof Error ? error.message : String(error)

Error message

error instanceof Error ? error.message : String(error)

What it means

This is the generic catch-all in VariableValueGenerationService.generate: any unexpected error that is not already a VariableValueGeneration subtype gets wrapped in VariableValueGenerationExecutionError with the original message preserved. It typically surfaces network failures, provider auth errors, or model invocation problems that happened while calling the generation model.

Source

Thrown at packages/core/src/services/variable-value-generation/service.ts:68

    // 4. 构建模板上下文
    const context = this.buildTemplateContext(request);

    // 5. 使用 TemplateProcessor 渲染模板
    const messages = TemplateProcessor.processTemplate(template, context);

    // 6. 调用 LLM 发送请求
    try {
      const result = await this.llmService.sendMessage(messages, request.generationModelKey);

      // 7. 解析 LLM 返回的 JSON 结果(传递请求的变量列表用于对齐校验)
      return this.parseGenerationResult(result, request.variables);
    } catch (error) {
      // 🔧 修复:保留原始错误类型,不要过度包装
      if (error instanceof VariableValueGenerationError) {
        throw error;
      }
      throw new VariableValueGenerationExecutionError(error instanceof Error ? error.message : String(error))
    }
  }

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

    if (!request.generationModelKey?.trim()) {
      throw new VariableValueGenerationValidationError('Generation model key must not be empty.');
    }

    if (!request.variables || request.variables.length === 0) {
      throw new VariableValueGenerationValidationError('Variables list must not be empty.');
    }

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Read the wrapped message — it preserves the original error (auth, network, rate-limit text)
  2. Verify the model key passed as generationModelKey is configured and reachable (test with a minimal direct model call)
  3. Check API keys and environment variables for the model provider
  4. If it is a transient network/rate-limit error, retry generate() with backoff

Example fix

// before
const res = await genService.generate(req);

// after
try {
  const res = await genService.generate(req);
} catch (e) {
  if (e instanceof VariableValueGenerationExecutionError) {
    console.error('underlying failure:', e.message); // inspect original cause
  }
  throw e;
}
Defensive patterns

Strategy: retry

Validate before calling

const model = await modelManager.getModel(req.generationModelKey);
if (!model) throw new Error('model unavailable before generate()');

Try / catch

try { await gen.generate(req); } catch (e) { if (e instanceof VariableValueGenerationExecutionError && !(e instanceof VariableValueGenerationParseError) && !(e instanceof VariableValueGenerationValidationError)) { /* inspect e.message for provider/network cause; retry with backoff if transient */ } throw e; }

Prevention

When it happens

Trigger: Calling generate() when the underlying model call throws — e.g. the modelManager invocation fails (connection refused, 401 API key, 429 rate limit, timeout), or template rendering raises an unexpected exception.

Common situations: Missing/expired API key for the configured model; model endpoint unreachable (proxy/firewall/DNS); rate limits from the provider; the model key exists but its provider is misconfigured; disk/permission errors reading templates.

Related errors


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