linshenkx/prompt-optimizer · error · VariableExtractionParseError
variables[${index}] is not a valid object.
Error message
variables[${index}] is not a valid object. What it means
While validating each entry of the variables array, entry at position ${index} is null, a primitive, or otherwise not an object. Each extracted variable must be an object with name/value/position fields, so normalization aborts on the first malformed entry and reports its index.
Source
Thrown at packages/core/src/services/variable-extraction/service.ts:211
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') {
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.`
);View on GitHub (pinned to 3e677b1d9f)
Solutions
- Log the raw variables array to see the actual shape at the failing index.
- Fix the prompt/template to require each entry be an object like {name, value, position}.
- Add a pre-normalization filter/mapper converting shorthand entries: strings -> {name: s, value: '', position: {start: 0, end: 0}}.
- Switch to a model with stronger instruction-following.
Example fix
// before
return this.normalizeExtractionResponse(parsed);
// after
parsed.variables = parsed.variables.map(v => typeof v === 'string' ? { name: v, value: '', position: { start: 0, end: 0 } } : v).filter(Boolean);
return this.normalizeExtractionResponse(parsed); Defensive patterns
Strategy: validation
Type guard
const allVariablesAreObjects = (vars: unknown[]): boolean => vars.every(v => !!v && typeof v === 'object' && !Array.isArray(v));
Try / catch
try {
await extractionService.extract({ modelKey, content });
} catch (e) {
if (e instanceof VariableExtractionParseError && /is not a valid object/.test(e.message)) {
// filter/map string entries to objects, then retry
}
throw e;
} Prevention
- Filter falsy entries and map shorthand strings to full objects before validation.
- Show the exact per-variable object shape in the prompt's few-shot example.
- Drop obviously malformed entries (null/primitives) rather than letting the whole extraction fail.
When it happens
Trigger: The LLM's variables array contains a null, a string (e.g. just the variable name), or a number at the reported index. Reached via extract() when the model emits a heterogeneous or degenerate array.
Common situations: Model emits variables as a plain string array ("variables": ["user.name", "date"]) instead of objects; model inserts null placeholders; mixed-quality output from a weak model; prompt example showed a shorthand format.
Related errors
- variables[${index}] is missing a valid "name" field.
- variables[${index}] is missing a valid "value" field.
- Extraction result must have a "variables" array.
- Extraction result must have a "summary" string.
- Invalid import data format
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/b70b9915fc9369b6.
Report an issue: GitHub.