linshenkx/prompt-optimizer · error · VariableValueGenerationValidationError

Variables list must not be empty.

Error message

Variables list must not be empty.

What it means

generate() needs at least one variable to generate values for; validateRequest throws when request.variables is undefined, null, or an empty array. Generating values with no variables is meaningless, so it is treated as a caller error before any model call is made.

Source

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

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

  /**
   * 验证模型是否存在
   */
  private async validateModel(modelKey: string): Promise<void> {
    const model = await this.modelManager.getModel(modelKey);
    if (!model) {
      throw new VariableValueGenerationModelError(modelKey);

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Ensure the variable-extraction step ran and produced at least one variable before invoking generate
  2. Guard with an explicit check and skip/warn instead of calling generate with an empty list
  3. Verify no upstream filter (dedupe, name validation) emptied the variables array

Example fix

// before
await gen.generate({ promptContent, generationModelKey, variables: extracted.variables });

// after
if (!extracted.variables?.length) {
  return { skipped: true, reason: 'no variables extracted' };
}
await gen.generate({ promptContent, generationModelKey, variables: extracted.variables });
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(req.variables) || req.variables.length === 0) { return skipGeneration('no variables'); }

Type guard

const hasVariables = (r: VariableValueGenerationRequest) => Array.isArray(r.variables) && r.variables.length > 0;

Try / catch

catch (e) { if (e instanceof VariableValueGenerationValidationError && /Variables list/.test(e.message)) return skip(); throw e; }

Prevention

When it happens

Trigger: generate({ promptContent: 'x', generationModelKey: 'm', variables: [] }) or omitting the variables field entirely.

Common situations: Calling value generation before the extraction step has produced variables; UI flow letting users skip variable selection; a filter that removed all variables (e.g. only invalid names survived); state reset between extraction and generation steps.

Related errors


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