linshenkx/prompt-optimizer · warning · TestError

User prompt is required

Error message

User prompt is required

What it means

testPrompt validates its inputs before running: for user-prompt testing the systemPrompt may be empty, but userPrompt must be a non-empty, non-whitespace string. A blank userPrompt aborts immediately with TestError 'User prompt is required'.

Source

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

        iterateInput,
        `Iteration failed: ${errorMessage}`,
      );
    }
  }

  /**
   * 测试提示词 - 支持可选系统提示词
   */
  async testPrompt(
    systemPrompt: string,
    userPrompt: string,
    modelKey: string,
    inputImages?: ImageInputRef[],
  ): Promise<string> {
    try {
      // 对于用户提示词优化,systemPrompt 可以为空
      if (!userPrompt?.trim()) {
        throw new TestError(systemPrompt, userPrompt, "User prompt is required");
      }
      if (!modelKey?.trim()) {
        throw new TestError(systemPrompt, userPrompt, "Model key is required");
      }

      const modelConfig = await this.modelManager.getModel(modelKey);
      if (!modelConfig) {
        throw new TestError(systemPrompt, userPrompt, "Model not found");
      }

      if (this.hasTestInputImages(inputImages)) {
        const result = await this.requireImageUnderstandingService().understand({
          modelConfig,
          systemPrompt: systemPrompt?.trim() ? systemPrompt : undefined,
          userPrompt,
          images: inputImages,
        });

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Ensure the caller validates that userPrompt contains non-whitespace text before invoking testPrompt
  2. Trim input in the UI/form layer and disable the test action while the prompt is blank
  3. Default or reconstruct the userPrompt if it legitimately can be empty in your flow
  4. Add a unit assertion for non-empty userPrompt in calling code paths

Example fix

// before
await promptService.testPrompt(systemPrompt, userPrompt, modelKey); // userPrompt may be ""

// after
const trimmed = userPrompt?.trim();
if (!trimmed) throw new Error("Type a user prompt before testing.");
await promptService.testPrompt(systemPrompt, trimmed, modelKey);
Defensive patterns

Strategy: validation

Validate before calling

if (!userPrompt?.trim()) {
  throw new Error("userPrompt must be non-empty before testing");
}
await promptService.testPrompt(systemPrompt, userPrompt.trim(), modelKey);

Type guard

const isNonEmptyPrompt = (p?: string | null): p is string =>
  typeof p === "string" && p.trim().length > 0;

Try / catch

null

Prevention

When it happens

Trigger: Calling testPrompt(systemPrompt, "", modelKey), testPrompt(systemPrompt, " ", modelKey), or passing undefined/null userPrompt (e.g. an unbound form field in the UI before the user typed anything).

Common situations: Testing a submit handler before validation fires; state where the prompt textarea is still empty; trimming user input upstream and passing the trimmed-empty value downstream.

Related errors


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