linshenkx/prompt-optimizer · error · VariableValueGenerationParseError

values[${index}] is missing a valid "name" field.

Error message

values[${index}] is missing a valid "name" field.

What it means

Each item in values must have a non-empty string name (after trim) so generated values can be matched back to the requested variables. This error names the array index of the item whose name is missing, empty, or whitespace-only.

Source

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

      throw new VariableValueGenerationParseError('Generation result must have a "values" array.');
    }

    if (typeof data.summary !== 'string') {
      throw new VariableValueGenerationParseError('Generation result must have a "summary" string.');
    }

    // 构建请求变量名集合(用于快速查找)
    // 🔧 对请求变量名也进行trim,避免首尾空格导致匹配失败
    const requestedNames = new Set(requestedVariables.map(v => v.name.trim()));

    // 标准化每个生成的值
    const rawValues: GeneratedVariableValue[] = data.values.map((item: any, index: number) => {
      if (!item || typeof item !== 'object') {
        throw new VariableValueGenerationParseError(`values[${index}] is not a valid object.`);
      }

      if (typeof item.name !== 'string' || !item.name.trim()) {
        throw new VariableValueGenerationParseError(`values[${index}] is missing a valid "name" field.`);
      }

      if (typeof item.value !== 'string') {
        throw new VariableValueGenerationParseError(`values[${index}] is missing a valid "value" field.`);
      }

      if (typeof item.reason !== 'string') {
        throw new VariableValueGenerationParseError(`values[${index}] is missing a valid "reason" field.`);
      }

      return {
        name: item.name.trim(),
        value: item.value,
        reason: item.reason,
        confidence: typeof item.confidence === 'number' ? item.confidence : undefined,
      };
    });

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. If you control the payload, remap alternate keys (item.variable/item.key -> item.name) and trim
  2. Strengthen the template: every value item MUST include the exact variable name string
  3. Match positionally as a fallback: assign requestedVariables[i].name when counts match
  4. Log the offending item to distinguish dropped vs renamed names

Example fix

// before
const out = await gen.generate(req);

// after
const parsed = JSON.parse(text);
parsed.values = parsed.values.map((item, i) => ({
  ...item,
  name: (item.name ?? item.variable ?? item.key ?? requested[i]?.name ?? '').toString().trim(),
}));
Defensive patterns

Strategy: validation

Validate before calling

parsed.values = parsed.values.map((item, i) => ({ ...item, name: (item.name ?? item.variable ?? item.key ?? requested[i]?.name ?? '').toString().trim() }));

Type guard

const hasName = (item: any) => typeof item?.name === 'string' && item.name.trim().length > 0;

Try / catch

catch (e) { if (e instanceof VariableValueGenerationParseError && /name. field/.test(e.message)) { /* remap alternate keys or positional names and retry */ } throw e; }

Prevention

When it happens

Trigger: values[i] = { value: 'x', reason: 'r' } with no name, or name: '' / ' ' / 42.

Common situations: Model drops name when returning values positionally (assuming order implies identity); whitespace-padded names from the model; name emitted under a different key ('variable', 'key'); template example omitting name.

Related errors


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