{"record":{"id":"6ef1a16b106a938f","repo":"linshenkx/prompt-optimizer","slug":"generation-result-must-have-a-values-array","errorCode":null,"errorMessage":"Generation result must have a \"values\" array.","messagePattern":"Generation result must have a \"values\" array\\.","errorType":"validation","errorClass":"VariableValueGenerationParseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":206,"sourceCode":"        );\n      }\n    }\n  }\n\n  /**\n   * 标准化并验证生成响应\n   * 🔧 修复：添加变量对齐校验，确保返回的变量与请求一致\n   */\n  private normalizeGenerationResponse(\n    data: any,\n    requestedVariables: VariableToGenerate[]\n  ): VariableValueGenerationResponse {\n    if (!data || typeof data !== 'object') {\n      throw new VariableValueGenerationParseError('Generation result is not a valid object.');\n    }\n\n    if (!Array.isArray(data.values)) {\n      throw new VariableValueGenerationParseError('Generation result must have a \"values\" array.');\n    }\n\n    if (typeof data.summary !== 'string') {\n      throw new VariableValueGenerationParseError('Generation result must have a \"summary\" string.');\n    }\n\n    // 构建请求变量名集合（用于快速查找）\n    // 🔧 对请求变量名也进行trim，避免首尾空格导致匹配失败\n    const requestedNames = new Set(requestedVariables.map(v => v.name.trim()));\n\n    // 标准化每个生成的值\n    const rawValues: GeneratedVariableValue[] = data.values.map((item: any, index: number) => {\n      if (!item || typeof item !== 'object') {\n        throw new VariableValueGenerationParseError(`values[${index}] is not a valid object.`);\n      }\n\n      if (typeof item.name !== 'string' || !item.name.trim()) {\n        throw new VariableValueGenerationParseError(`values[${index}] is missing a valid \"name\" field.`);","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L188-L224","documentation":"The parsed generation response must contain a 'values' array (alongside the summary). normalizeGenerationResponse throws VariableValueGenerationParseError when data.values is missing or is not an array — e.g. the model returned an object keyed by variable name instead of a list.","triggerScenarios":"Model returns { summary: \"...\", values: { \"var1\": \"v\", \"var2\": \"v\" } } (object map instead of array) or omits values entirely.","commonSituations":"Template customized to a map-style output; model choosing a key/value dict because variable names are known; field renamed ('results', 'generated', 'items') by the model or an edited template; nested envelope like { result: { values: [...] } }.","solutions":["Convert object maps to arrays before parsing: values: Object.entries(map).map(([name, v]) => ({ name, ...v }))","Align the template's documented schema with { values: [{name, value, reason}], summary }","Unwrap nested envelopes ({ result: ... }) if the model added a wrapper","Log the raw keys of the response to spot renames"],"exampleFix":"// before\nconst out = await gen.generate(req);\n\n// after\nlet parsed = JSON.parse(text);\nif (parsed.result) parsed = parsed.result; // unwrap\nif (parsed.values && !Array.isArray(parsed.values) && typeof parsed.values === 'object') {\n  parsed.values = Object.entries(parsed.values).map(([name, v]) =>\n    typeof v === 'string' ? { name, value: v, reason: '' } : { name, ...v });\n}","handlingStrategy":"validation","validationCode":"if (parsed.values && !Array.isArray(parsed.values) && typeof parsed.values === 'object') { parsed.values = Object.entries(parsed.values).map(([name, v]) => ({ name, ...(typeof v === 'object' ? v : { value: v }) })); }","typeGuard":"const hasValuesArray = (d: any) => Array.isArray(d?.values);","tryCatchPattern":"catch (e) { if (e instanceof VariableValueGenerationParseError && /values. array/.test(e.message)) { /* convert map->array and re-normalize */ } throw e; }","preventionTips":["Prompt must say values is a JSON ARRAY of objects, not a map keyed by name","Unwrap any nested { result: ... } envelope the model adds"],"tags":["llm-output","schema-validation","json","variable-value-generation"],"backgroundTag":"llm-response-schema-invalid","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}