linshenkx/prompt-optimizer · error · VariableValueGenerationValidationError

Variable at index ${i} has empty name.

Error message

Variable at index ${i} has empty name.

What it means

validateRequest iterates the variables array and throws VariableValueGenerationValidationError with the offending index when any variable's name is missing or whitespace-only. The name is the join key between the request and the model's generated values, so blank names break the response mapping.

Source

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

   */
  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);
    }
  }

  /**
   * 获取变量值生成模板
   */
  private async getGenerationTemplate(): Promise<Template> {

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Filter out blank-named variables before calling generate
  2. Trim and validate names when building the variables array (and reject empties at extraction time)
  3. Log the index from the message to find the offending entry quickly

Example fix

// before
await gen.generate({ ...req, variables });

// after
const cleaned = variables
  .map(v => ({ ...v, name: v.name?.trim() ?? '' }))
  .filter(v => v.name.length > 0);
if (!cleaned.length) throw new Error('No named variables to generate');
await gen.generate({ ...req, variables: cleaned });
Defensive patterns

Strategy: validation

Validate before calling

const bad = req.variables.findIndex(v => !v.name?.trim());
if (bad >= 0) throw new Error(`variable[${bad}] has empty name`);

Type guard

const namesValid = (vars: VariableToGenerate[]) => vars.every(v => typeof v.name === 'string' && v.name.trim().length > 0);

Try / catch

catch (e) { if (e instanceof VariableValueGenerationValidationError && /empty name/i.test(e.message)) { const i = Number(e.message.match(/index (\d+)/)?.[1]); /* drop variables[i] and retry */ } throw e; }

Prevention

When it happens

Trigger: Calling generate() with variables containing an entry like { name: '', value: 'x' } or { name: ' ' } or with the name field absent entirely; the message names the exact index.

Common situations: LLM extraction returned a variable with an empty name (extraction normalization trims but doesn't reject empty results upstream); manual variable lists built from user input without trimming; CSV/spreadsheet imports producing blank rows.

Related errors


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