linshenkx/prompt-optimizer · error · VariableValueGenerationValidationError

Prompt content must not be empty.

Error message

Prompt content must not be empty.

What it means

validateRequest (called at the start of generate) rejects requests whose promptContent is null, undefined, or whitespace-only. The prompt content is the input the model generates variable values from, so an empty prompt is a caller bug, not a model failure.

Source

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

      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.');
    }

    // 验证每个变量
    for (let i = 0; i < request.variables.length; i++) {
      const variable = request.variables[i];
      if (!variable.name?.trim()) {
        throw new VariableValueGenerationValidationError(`Variable at index ${i} has empty name.`);
      }
    }
  }

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Check promptContent?.trim() before calling generate and surface a user-facing validation message
  2. Fix the data source that produced an empty prompt (file read, form field, state)
  3. Ensure you are passing the full request object with the correct property names

Example fix

// before
await gen.generate({ promptContent: savedPrompt, ...req }); // savedPrompt may be ''

// after
const promptContent = savedPrompt?.trim();
if (!promptContent) throw new Error('Prompt is required before generating variable values');
await gen.generate({ ...req, promptContent });
Defensive patterns

Strategy: validation

Validate before calling

if (!req.promptContent?.trim()) throw new Error('Prompt content required');

Type guard

const hasPrompt = (r: VariableValueGenerationRequest) => typeof r.promptContent === 'string' && r.promptContent.trim().length > 0;

Try / catch

catch (e) { if (e instanceof VariableValueGenerationValidationError && /Prompt content/.test(e.message)) return badRequest('prompt required'); throw e; }

Prevention

When it happens

Trigger: Calling generate({ promptContent: '', generationModelKey: 'gpt', variables: [...] }) or passing promptContent: undefined / ' '.

Common situations: Upstream UI letting users submit an empty prompt; reading promptContent from a file/env var that is missing; refactoring that renamed the field; passing the wrong object into generate().

Related errors


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