linshenkx/prompt-optimizer · error · VariableValueGenerationParseError

Generation result must have a "values" array.

Error message

Generation result must have a "values" array.

What it means

The parsed generation response must contain a 'values' array (alongside the summary). normalizeGenerationResponse throws VariableValueGenerationParseError when data.values is missing or is not an array — e.g. the model returned an object keyed by variable name instead of a list.

Source

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

        );
      }
    }
  }

  /**
   * 标准化并验证生成响应
   * 🔧 修复:添加变量对齐校验,确保返回的变量与请求一致
   */
  private normalizeGenerationResponse(
    data: any,
    requestedVariables: VariableToGenerate[]
  ): VariableValueGenerationResponse {
    if (!data || typeof data !== 'object') {
      throw new VariableValueGenerationParseError('Generation result is not a valid object.');
    }

    if (!Array.isArray(data.values)) {
      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.`);

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Convert object maps to arrays before parsing: values: Object.entries(map).map(([name, v]) => ({ name, ...v }))
  2. Align the template's documented schema with { values: [{name, value, reason}], summary }
  3. Unwrap nested envelopes ({ result: ... }) if the model added a wrapper
  4. Log the raw keys of the response to spot renames

Example fix

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

// after
let parsed = JSON.parse(text);
if (parsed.result) parsed = parsed.result; // unwrap
if (parsed.values && !Array.isArray(parsed.values) && typeof parsed.values === 'object') {
  parsed.values = Object.entries(parsed.values).map(([name, v]) =>
    typeof v === 'string' ? { name, value: v, reason: '' } : { name, ...v });
}
Defensive patterns

Strategy: validation

Validate before calling

if (parsed.values && !Array.isArray(parsed.values) && typeof parsed.values === 'object') { parsed.values = Object.entries(parsed.values).map(([name, v]) => ({ name, ...(typeof v === 'object' ? v : { value: v }) })); }

Type guard

const hasValuesArray = (d: any) => Array.isArray(d?.values);

Try / catch

catch (e) { if (e instanceof VariableValueGenerationParseError && /values. array/.test(e.message)) { /* convert map->array and re-normalize */ } throw e; }

Prevention

When it happens

Trigger: Model returns { summary: "...", values: { "var1": "v", "var2": "v" } } (object map instead of array) or omits values entirely.

Common situations: Template customized to a map-style output; model choosing a key/value dict because variable names are known; field renamed ('results', 'generated', 'items') by the model or an edited template; nested envelope like { result: { values: [...] } }.

Related errors


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