linshenkx/prompt-optimizer · error · VariableExtractionParseError

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

Error message

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

What it means

The variables array entry at ${index} has a 'value' field that is missing or not a string. The unified schema requires every extracted variable to carry a string value (even an empty string), so normalization aborts at this entry.

Source

Thrown at packages/core/src/services/variable-extraction/service.ts:219

    // 验证 summary 字段
    if (typeof data.summary !== 'string') {
      throw new VariableExtractionParseError('Extraction result must have a "summary" string.');
    }

    // 标准化每个变量
    const variables: ExtractedVariable[] = data.variables.map((variable: any, index: number) => {
      // 验证必需字段
      if (!variable || typeof variable !== 'object') {
        throw new VariableExtractionParseError(`variables[${index}] is not a valid object.`);
      }

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

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

      if (!variable.position || typeof variable.position !== 'object') {
        throw new VariableExtractionParseError(`variables[${index}] is missing a valid "position" object.`);
      }

      if (typeof variable.position.originalText !== 'string') {
        throw new VariableExtractionParseError(
          `variables[${index}].position is missing a valid "originalText" field.`
        );
      }

      if (typeof variable.position.occurrence !== 'number') {
        throw new VariableExtractionParseError(
          `variables[${index}].position is missing a valid "occurrence" number.`
        );
      }

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Add a coercion shim: String(v.value ?? '') when value is a primitive.
  2. Map alternate keys: v.value ?? v.text ?? v.content.
  3. Fix the prompt/template to require "value" as a string (use "" when unmatched) with a few-shot example.
  4. Drop entries without any value-like field if they are hallucinated.

Example fix

// before
return this.normalizeExtractionResponse(parsed);

// after
parsed.variables = parsed.variables.map(v => ({ ...v, value: typeof v.value === 'string' ? v.value : String(v.value ?? v.text ?? '') }));
return this.normalizeExtractionResponse(parsed);
Defensive patterns

Strategy: validation

Type guard

const hasStringValue = (v: unknown): boolean =>
  !!v && typeof v === 'object' && typeof (v as any).value === 'string';

Try / catch

try {
  await extractionService.extract({ modelKey, content });
} catch (e) {
  if (e instanceof VariableExtractionParseError && /"value" field/.test(e.message)) {
    // coerce primitives with String() / map text/content keys, then retry
  }
  throw e;
}

Prevention

When it happens

Trigger: The model returns a variable object where value is undefined, null, a number, boolean, or object. Common when the model omits value for unresolvable variables or returns typed values (e.g. numbers, booleans) instead of strings. Reached via extract().

Common situations: Model returns {"name":"count","value":3} (number not string); model omits value when the variable has no textual match; model uses 'text' or 'content' as the key; template example shows optional value.

Related errors


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