{"record":{"id":"16f534f659b61851","repo":"linshenkx/prompt-optimizer","slug":"values-index-is-missing-a-valid-name-field","errorCode":null,"errorMessage":"values[${index}] is missing a valid \"name\" field.","messagePattern":"values\\[(.+?)\\] is missing a valid \"name\" field\\.","errorType":"validation","errorClass":"VariableValueGenerationParseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":224,"sourceCode":"      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.`);\n      }\n\n      if (typeof item.value !== 'string') {\n        throw new VariableValueGenerationParseError(`values[${index}] is missing a valid \"value\" field.`);\n      }\n\n      if (typeof item.reason !== 'string') {\n        throw new VariableValueGenerationParseError(`values[${index}] is missing a valid \"reason\" field.`);\n      }\n\n      return {\n        name: item.name.trim(),\n        value: item.value,\n        reason: item.reason,\n        confidence: typeof item.confidence === 'number' ? item.confidence : undefined,\n      };\n    });\n","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L206-L242","documentation":"Each item in values must have a non-empty string name (after trim) so generated values can be matched back to the requested variables. This error names the array index of the item whose name is missing, empty, or whitespace-only.","triggerScenarios":"values[i] = { value: 'x', reason: 'r' } with no name, or name: '' / '   ' / 42.","commonSituations":"Model drops name when returning values positionally (assuming order implies identity); whitespace-padded names from the model; name emitted under a different key ('variable', 'key'); template example omitting name.","solutions":["If you control the payload, remap alternate keys (item.variable/item.key -> item.name) and trim","Strengthen the template: every value item MUST include the exact variable name string","Match positionally as a fallback: assign requestedVariables[i].name when counts match","Log the offending item to distinguish dropped vs renamed names"],"exampleFix":"// before\nconst out = await gen.generate(req);\n\n// after\nconst parsed = JSON.parse(text);\nparsed.values = parsed.values.map((item, i) => ({\n  ...item,\n  name: (item.name ?? item.variable ?? item.key ?? requested[i]?.name ?? '').toString().trim(),\n}));","handlingStrategy":"validation","validationCode":"parsed.values = parsed.values.map((item, i) => ({ ...item, name: (item.name ?? item.variable ?? item.key ?? requested[i]?.name ?? '').toString().trim() }));","typeGuard":"const hasName = (item: any) => typeof item?.name === 'string' && item.name.trim().length > 0;","tryCatchPattern":"catch (e) { if (e instanceof VariableValueGenerationParseError && /name. field/.test(e.message)) { /* remap alternate keys or positional names and retry */ } throw e; }","preventionTips":["Echo the exact variable names in the prompt and require them verbatim in each item","Fall back to positional matching when counts align"],"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"}