linshenkx/prompt-optimizer · error · VariableExtractionParseError

Extraction result must have a "summary" string.

Error message

Extraction result must have a "summary" string.

What it means

The parsed LLM response object has a 'summary' field that is missing or not a string. The unified extraction schema requires a top-level string summary, so normalization fails even if the variables array is fine.

Source

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

    }
  }

  /**
   * 标准化提取响应(统一结构)
   */
  private normalizeExtractionResponse(data: any): VariableExtractionResponse {
    if (!data || typeof data !== 'object') {
      throw new VariableExtractionParseError('Extraction result is not a valid object.');
    }

    // 验证 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') {

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Update the prompt/template to require "summary" as a string (e.g. "summary": "" when nothing to say).
  2. Add a defensive default before normalization: if (typeof parsed.summary !== 'string') parsed.summary = ''.
  3. Log the parsed response to confirm which shape the model emits.
  4. Provide a few-shot example in the template showing summary present.

Example fix

// before
return this.normalizeExtractionResponse(parsed);

// after
if (typeof parsed.summary !== 'string') parsed.summary = '';
return this.normalizeExtractionResponse(parsed);
Defensive patterns

Strategy: validation

Type guard

const hasStringSummary = (d: unknown): boolean =>
  isExtractionObject(d) && typeof (d as any).summary === 'string';

Try / catch

try {
  await extractionService.extract({ modelKey, content });
} catch (e) {
  if (e instanceof VariableExtractionParseError && /"summary" string/.test(e.message)) {
    // inject a default summary and retry, or fix the prompt
  }
  throw e;
}

Prevention

When it happens

Trigger: Model returns an object with a valid variables array but omits summary, returns summary as null/number/object, or nests it one level deeper. Reached via extract() during normalizeExtractionResponse.

Common situations: Model instructed only to extract variables so it drops the summary field; model returns "summary": null when nothing to summarize; template's output-format section no longer mentions summary after an edit.

Related errors


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