{"record":{"id":"3d9c75ceff529038","repo":"linshenkx/prompt-optimizer","slug":"values-index-is-missing-a-valid-value-field","errorCode":null,"errorMessage":"values[${index}] is missing a valid \"value\" field.","messagePattern":"values\\[(.+?)\\] is missing a valid \"value\" field\\.","errorType":"validation","errorClass":"VariableValueGenerationParseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":228,"sourceCode":"      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\n    // 🔧 对齐处理：过滤掉不在请求列表中的变量 + 建立Map用于快速查找\n    const valueMap = new Map<string, GeneratedVariableValue>();\n    for (const val of rawValues) {\n      if (requestedNames.has(val.name)) {","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L210-L246","documentation":"Each generated value item must contain a string value field — the actual generated content. normalizeGenerationResponse throws with the item's index when item.value is missing or not a string (numbers, objects, null all fail).","triggerScenarios":"values[i] = { name: 'var1', reason: 'r' } with no value, or value: 42 / value: null / value: { text: 'x' }.","commonSituations":"Model returns numbers unquoted for numeric variables (age: 30); value nested as { text } or { content } by schema-drifting models; null placeholders for variables the model skipped; truncation cutting the value field.","solutions":["Coerce: if value is a number/boolean, String(value); if nested, unwrap item.value.text ?? item.value.content","Instruct in the template that value must ALWAYS be a JSON string even for numbers","Drop null-valued items and re-request those specific variables","Check for truncation (raise max_tokens) when values tail off"],"exampleFix":"// before\nconst out = await gen.generate(req);\n\n// after\nconst parsed = JSON.parse(text);\nfor (const item of parsed.values ?? []) {\n  if (item.value && typeof item.value === 'object') item.value = item.value.text ?? item.value.content ?? '';\n  else if (item.value !== undefined && typeof item.value !== 'string') item.value = String(item.value);\n}","handlingStrategy":"validation","validationCode":"for (const item of parsed.values ?? []) { if (item.value && typeof item.value === 'object') item.value = item.value.text ?? item.value.content ?? ''; else if (item.value != null && typeof item.value !== 'string') item.value = String(item.value); }","typeGuard":"const hasValue = (item: any) => typeof item?.value === 'string';","tryCatchPattern":"catch (e) { if (e instanceof VariableValueGenerationParseError && /value. field/.test(e.message)) { /* coerce numbers / unwrap nested value and retry */ } throw e; }","preventionTips":["Instruct the model that value is ALWAYS a string, even for numbers","Raise max_tokens to avoid mid-item truncation"],"tags":["llm-output","schema-validation","json","type-coercion","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"}