linshenkx/prompt-optimizer · error · VariableValueGenerationParseError
Generation result is not a valid object.
Error message
Generation result is not a valid object.
What it means
After successfully JSON-parsing the model output, normalizeGenerationResponse requires the top level to be an object. If the parsed value is an array, string, number, or null, VariableValueGenerationParseError is thrown. The model returned valid JSON but the wrong shape.
Source
Thrown at packages/core/src/services/variable-value-generation/service.ts:202
return this.normalizeGenerationResponse(parsed, requestedVariables);
} catch (fallbackError) {
throw new VariableValueGenerationParseError(
`Failed to parse LLM response: ${error instanceof Error ? error.message : String(error)}`
);
}
}
}
/**
* 标准化并验证生成响应
* 🔧 修复:添加变量对齐校验,确保返回的变量与请求一致
*/
private normalizeGenerationResponse(
data: any,
requestedVariables: VariableToGenerate[]
): VariableValueGenerationResponse {
if (!data || typeof data !== 'object') {
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.`);View on GitHub (pinned to 3e677b1d9f)
Solutions
- If you control the output, wrap bare arrays: { values: arr, summary: '' } before passing onward (or re-run generation)
- Fix the template to show the exact envelope with a one-shot example
- Log the parsed type (Array.isArray, typeof) to confirm the mismatch
- Prefer models with structured-output/JSON-schema enforcement
Example fix
// before
const out = await gen.generate(req);
// after
// if you post-process raw model text yourself:
let parsed = JSON.parse(text);
if (Array.isArray(parsed)) parsed = { values: parsed, summary: '' };
if (typeof parsed !== 'object' || parsed === null) throw new Error('unexpected LLM shape'); Defensive patterns
Strategy: type-guard
Validate before calling
const parsed = JSON.parse(text);
if (Array.isArray(parsed)) parsed = { values: parsed, summary: '' }; Type guard
function isEnvelope(d: any): d is { values: unknown[]; summary: string } { return !!d && typeof d === 'object' && !Array.isArray(d); } Try / catch
catch (e) { if (e instanceof VariableValueGenerationParseError && /not a valid object/.test(e.message)) { /* wrap arrays and retry normalize */ } throw e; } Prevention
- Show the exact envelope shape in the template's example
- Check Array.isArray on parsed LLM JSON before assuming object shape
When it happens
Trigger: Model returns a bare array of values ([{name, value, reason}, ...]) or a bare string instead of the expected { values: [...], summary: "..." } envelope.
Common situations: Models that 'simplify' the requested schema and return an array; template instruction ambiguous about the envelope; few-shot examples in a customized template showing a bare array; model returning just the JSON string of values.
Related errors
- Generation result must have a "values" array.
- Generation result must have a "summary" string.
- values[${index}] is not a valid object.
- 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/95ba93534d1a7bcf.
Report an issue: GitHub.