linshenkx/prompt-optimizer · error · VariableValueGenerationParseError
values[${index}] is not a valid object.
Error message
values[${index}] is not a valid object. What it means
Each entry of the values array must be an object. normalizeGenerationResponse maps over data.values and throws VariableValueGenerationParseError with the failing index when an item is null, a primitive (string/number/boolean), or an array.
Source
Thrown at packages/core/src/services/variable-value-generation/service.ts:220
throw new VariableValueGenerationParseError('Generation result is not a valid object.');
}
if (!Array.isArray(data.values)) {
throw new VariableValueGenerationParseError('Generation result must have a "values" array.');
}
if (typeof data.summary !== 'string') {
throw new VariableValueGenerationParseError('Generation result must have a "summary" string.');
}
// 构建请求变量名集合(用于快速查找)
// 🔧 对请求变量名也进行trim,避免首尾空格导致匹配失败
const requestedNames = new Set(requestedVariables.map(v => v.name.trim()));
// 标准化每个生成的值
const rawValues: GeneratedVariableValue[] = data.values.map((item: any, index: number) => {
if (!item || typeof item !== 'object') {
throw new VariableValueGenerationParseError(`values[${index}] is not a valid object.`);
}
if (typeof item.name !== 'string' || !item.name.trim()) {
throw new VariableValueGenerationParseError(`values[${index}] is missing a valid "name" field.`);
}
if (typeof item.value !== 'string') {
throw new VariableValueGenerationParseError(`values[${index}] is missing a valid "value" field.`);
}
if (typeof item.reason !== 'string') {
throw new VariableValueGenerationParseError(`values[${index}] is missing a valid "reason" field.`);
}
return {
name: item.name.trim(),
value: item.value,
reason: item.reason,View on GitHub (pinned to 3e677b1d9f)
Solutions
- Filter/normalize non-object entries before parsing: drop nulls, or parse 'name=value' strings into objects
- Make the template's few-shot examples consistently object-shaped
- If nulls appear for specific variables, decide policy: drop them or re-request those variables individually
Example fix
// before const out = await gen.generate(req); // after const parsed = JSON.parse(text); parsed.values = (parsed.values ?? []).filter(v => v && typeof v === 'object' && !Array.isArray(v));
Defensive patterns
Strategy: type-guard
Validate before calling
parsed.values = (parsed.values ?? []).filter(v => v && typeof v === 'object' && !Array.isArray(v));
Type guard
const isValueItem = (v: any): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
Try / catch
catch (e) { if (e instanceof VariableValueGenerationParseError && /not a valid object/.test(e.message)) { /* filter non-object items and retry */ } throw e; } Prevention
- Keep template examples uniform — every values item must be an object
- Drop model-emitted null placeholders before normalization
When it happens
Trigger: values: [ "var1=hello", { name: 'var2', ... } ] — a mix of strings and objects — or values: [null, {...}] when the model emitted nulls for variables it could not generate.
Common situations: Model outputs compact 'name=value' strings for simple cases but objects for complex ones; model emits null placeholders for un-generatable variables; template examples inconsistent about item shape.
Related errors
- Generation result is not a valid object.
- Generation result must have a "values" array.
- Generation result must have a "summary" string.
- values[${index}] is missing a valid "name" field.
- values[${index}] is missing a valid "value" field.
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/f2b4e7dcd259039a.
Report an issue: GitHub.