{"record":{"id":"8adb567771f188c9","repo":"linshenkx/prompt-optimizer","slug":"variables-index-is-missing-a-valid-value-fie","errorCode":null,"errorMessage":"variables[${index}] is missing a valid \"value\" field.","messagePattern":"variables\\[(.+?)\\] is missing a valid \"value\" field\\.","errorType":"validation","errorClass":"VariableExtractionParseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-extraction/service.ts","lineNumber":219,"sourceCode":"\n    // 验证 summary 字段\n    if (typeof data.summary !== 'string') {\n      throw new VariableExtractionParseError('Extraction result must have a \"summary\" string.');\n    }\n\n    // 标准化每个变量\n    const variables: ExtractedVariable[] = data.variables.map((variable: any, index: number) => {\n      // 验证必需字段\n      if (!variable || typeof variable !== 'object') {\n        throw new VariableExtractionParseError(`variables[${index}] is not a valid object.`);\n      }\n\n      if (typeof variable.name !== 'string' || !variable.name.trim()) {\n        throw new VariableExtractionParseError(`variables[${index}] is missing a valid \"name\" field.`);\n      }\n\n      if (typeof variable.value !== 'string') {\n        throw new VariableExtractionParseError(`variables[${index}] is missing a valid \"value\" field.`);\n      }\n\n      if (!variable.position || typeof variable.position !== 'object') {\n        throw new VariableExtractionParseError(`variables[${index}] is missing a valid \"position\" object.`);\n      }\n\n      if (typeof variable.position.originalText !== 'string') {\n        throw new VariableExtractionParseError(\n          `variables[${index}].position is missing a valid \"originalText\" field.`\n        );\n      }\n\n      if (typeof variable.position.occurrence !== 'number') {\n        throw new VariableExtractionParseError(\n          `variables[${index}].position is missing a valid \"occurrence\" number.`\n        );\n      }\n","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-extraction/service.ts#L201-L237","documentation":"The variables array entry at ${index} has a 'value' field that is missing or not a string. The unified schema requires every extracted variable to carry a string value (even an empty string), so normalization aborts at this entry.","triggerScenarios":"The model returns a variable object where value is undefined, null, a number, boolean, or object. Common when the model omits value for unresolvable variables or returns typed values (e.g. numbers, booleans) instead of strings. Reached via extract().","commonSituations":"Model returns {\"name\":\"count\",\"value\":3} (number not string); model omits value when the variable has no textual match; model uses 'text' or 'content' as the key; template example shows optional value.","solutions":["Add a coercion shim: String(v.value ?? '') when value is a primitive.","Map alternate keys: v.value ?? v.text ?? v.content.","Fix the prompt/template to require \"value\" as a string (use \"\" when unmatched) with a few-shot example.","Drop entries without any value-like field if they are hallucinated."],"exampleFix":"// before\nreturn this.normalizeExtractionResponse(parsed);\n\n// after\nparsed.variables = parsed.variables.map(v => ({ ...v, value: typeof v.value === 'string' ? v.value : String(v.value ?? v.text ?? '') }));\nreturn this.normalizeExtractionResponse(parsed);","handlingStrategy":"validation","validationCode":null,"typeGuard":"const hasStringValue = (v: unknown): boolean =>\n  !!v && typeof v === 'object' && typeof (v as any).value === 'string';","tryCatchPattern":"try {\n  await extractionService.extract({ modelKey, content });\n} catch (e) {\n  if (e instanceof VariableExtractionParseError && /\"value\" field/.test(e.message)) {\n    // coerce primitives with String() / map text/content keys, then retry\n  }\n  throw e;\n}","preventionTips":["Coerce non-string values (String(v.value ?? '')) before passing to the service.","Map alternate keys (text, content) to value in pre-processing.","Instruct the model to always emit value as a string, using \"\" when unmatched."],"tags":["llm","json","schema","validation","variables"],"backgroundTag":"llm-response-schema-mismatch","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}