linshenkx/prompt-optimizer · error · VariableExtractionParseError
Extraction result must have a "variables" array.
Error message
Extraction result must have a "variables" array.
What it means
The parsed LLM response is an object, but it lacks a 'variables' array (the field is missing, or is not an Array). 'variables' is a required field of the unified extraction response, so normalization fails before any per-variable validation runs.
Source
Thrown at packages/core/src/services/variable-extraction/service.ts:199
} catch (fallbackError) {
throw new VariableExtractionParseError(
`Failed to parse LLM response: ${error instanceof Error ? error.message : String(error)}. Raw content length: ${content.length} characters.`
);
}
}
}
/**
* 标准化提取响应(统一结构)
*/
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.`);
}
View on GitHub (pinned to 3e677b1d9f)
Solutions
- Update the prompt/template to state that the output MUST include "variables": [] (empty array when nothing is extracted).
- Log the parsed object keys to see what field name the model used instead.
- Add a pre-normalization shim mapping the actual field (e.g. data.extracted or Object.values(data.variables)) to data.variables.
- Try a stronger model or few-shot example showing the exact schema.
Example fix
// before return this.normalizeExtractionResponse(parsed); // after if (!Array.isArray(parsed.variables) && Array.isArray(parsed.extracted)) parsed.variables = parsed.extracted; return this.normalizeExtractionResponse(parsed);
Defensive patterns
Strategy: type-guard
Type guard
const hasVariablesArray = (d: unknown): d is { variables: unknown[]; [k: string]: unknown } =>
isExtractionObject(d) && Array.isArray((d as any).variables); Try / catch
try {
await extractionService.extract({ modelKey, content });
} catch (e) {
if (e instanceof VariableExtractionParseError && /"variables" array/.test(e.message)) {
// remap the actual field name (e.g. extracted) and retry once
}
throw e;
} Prevention
- State explicitly in the prompt that the output must contain a top-level "variables" array (empty array if none).
- Normalize alternate field names (extracted, result, items) in your own pre-processing layer.
- Use structured output / JSON schema enforcement if the model provider supports it.
When it happens
Trigger: Model returns an object whose variables field is omitted, an object instead of an array (e.g. a keyed map of variables), or a string. Reached via extract() when the model's JSON deviates from the required {variables: [], summary: ""} shape.
Common situations: Model returns {"extracted": [...]} or {"result": {"variables": [...]}} (extra nesting); model returns variables as an object keyed by name; prompt template edited so the field name changed; model omits the field when it finds no variables instead of returning an empty array.
Related errors
- Extraction result must have a "summary" string.
- variables[${index}] is not a valid object.
- variables[${index}] is missing a valid "name" field.
- variables[${index}] is missing a valid "value" field.
- Invalid import data format
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/605f41479ad88ca2.
Report an issue: GitHub.