linshenkx/prompt-optimizer · error · VariableValueGenerationParseError

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

Error message

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

What it means

Each generated value item must contain a string value field — the actual generated content. normalizeGenerationResponse throws with the item's index when item.value is missing or not a string (numbers, objects, null all fail).

Source

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

      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,
      };
    });

    // 🔧 对齐处理:过滤掉不在请求列表中的变量 + 建立Map用于快速查找
    const valueMap = new Map<string, GeneratedVariableValue>();
    for (const val of rawValues) {
      if (requestedNames.has(val.name)) {

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Coerce: if value is a number/boolean, String(value); if nested, unwrap item.value.text ?? item.value.content
  2. Instruct in the template that value must ALWAYS be a JSON string even for numbers
  3. Drop null-valued items and re-request those specific variables
  4. Check for truncation (raise max_tokens) when values tail off

Example fix

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

// after
const parsed = JSON.parse(text);
for (const item of parsed.values ?? []) {
  if (item.value && typeof item.value === 'object') item.value = item.value.text ?? item.value.content ?? '';
  else if (item.value !== undefined && typeof item.value !== 'string') item.value = String(item.value);
}
Defensive patterns

Strategy: validation

Validate before calling

for (const item of parsed.values ?? []) { if (item.value && typeof item.value === 'object') item.value = item.value.text ?? item.value.content ?? ''; else if (item.value != null && typeof item.value !== 'string') item.value = String(item.value); }

Type guard

const hasValue = (item: any) => typeof item?.value === 'string';

Try / catch

catch (e) { if (e instanceof VariableValueGenerationParseError && /value. field/.test(e.message)) { /* coerce numbers / unwrap nested value and retry */ } throw e; }

Prevention

When it happens

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

Common situations: Model returns numbers unquoted for numeric variables (age: 30); value nested as { text } or { content } by schema-drifting models; null placeholders for variables the model skipped; truncation cutting the value field.

Related errors


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