linshenkx/prompt-optimizer · error · VariableExtractionParseError

variables[ ] is missing a valid "name" field.

Error message

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

What it means

The variables array entry at ${index} is an object but its 'name' field is missing, not a string, or a blank/whitespace-only string. A non-empty name is required for every extracted variable, so normalization aborts at this entry.

Solutions

  1. Log the failing entry to see which key the model used instead of 'name'.
  2. Add a shim mapping common alternates: v.name ?? v.key ?? v.variable.
  3. Fix the prompt/template to require "name" as a non-empty string with a few-shot example.
  4. Filter out nameless entries before calling extraction if they are noise.

Example fix

// before
return this.normalizeExtractionResponse(parsed);

// after
parsed.variables = parsed.variables.map(v => ({ ...v, name: v.name ?? v.key ?? v.variable })).filter(v => typeof v.name === 'string' && v.name.trim());
return this.normalizeExtractionResponse(parsed);
Defensive patterns

Strategy: validation

Type guard

const hasValidName = (v: unknown): v is { name: string; [k: string]: unknown } =>
  !!v && typeof v === 'object' && typeof (v as any).name === 'string' && (v as any).name.trim().length > 0;

Try / catch

try {
  await extractionService.extract({ modelKey, content });
} catch (e) {
  if (e instanceof VariableExtractionParseError && /"name" field/.test(e.message)) {
    // remap alternate keys (key/variable) and filter empty names, then retry
  }
  throw e;
}

Prevention

When it happens

Trigger: The model returns a variable object where name is undefined, null, a number, or ''/' '. Reached via extract() during per-variable validation in normalizeExtractionResponse.

Common situations: Model uses a different key ('key', 'variable', 'id') instead of 'name'; model returns empty-string names for hallucinated slots; multilingual model returning name with only whitespace; schema drift after template edits.

Related errors


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

Appendix: source

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

    // 验证 variables 字段
    if (!Array.isArray(data.variables)) {
      throw new VariableExtractionParseError('Extraction result must have a "variables" array.');
    }

    // 验证 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(

View on GitHub (pinned to 3e677b1d9f)