{"record":{"id":"a56adf24ef983dda","repo":"linshenkx/prompt-optimizer","slug":"variables-index-is-missing-a-valid-name-fiel","errorCode":null,"errorMessage":"variables[${index}] is missing a valid \"name\" field.","messagePattern":"variables\\[(.+?)\\] is missing a valid \"name\" field\\.","errorType":"validation","errorClass":"VariableExtractionParseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-extraction/service.ts","lineNumber":215,"sourceCode":"    // 验证 variables 字段\n    if (!Array.isArray(data.variables)) {\n      throw new VariableExtractionParseError('Extraction result must have a \"variables\" array.');\n    }\n\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(","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-extraction/service.ts#L197-L233","documentation":"The variables array entry at ${index} is an object but its 'name' field is missing, not a string, or a blank/whitespace-only string. A non-empty name is required for every extracted variable, so normalization aborts at this entry.","triggerScenarios":"The model returns a variable object where name is undefined, null, a number, or ''/'   '. Reached via extract() during per-variable validation in normalizeExtractionResponse.","commonSituations":"Model uses a different key ('key', 'variable', 'id') instead of 'name'; model returns empty-string names for hallucinated slots; multilingual model returning name with only whitespace; schema drift after template edits.","solutions":["Log the failing entry to see which key the model used instead of 'name'.","Add a shim mapping common alternates: v.name ?? v.key ?? v.variable.","Fix the prompt/template to require \"name\" as a non-empty string with a few-shot example.","Filter out nameless entries before calling extraction if they are noise."],"exampleFix":"// before\nreturn this.normalizeExtractionResponse(parsed);\n\n// after\nparsed.variables = parsed.variables.map(v => ({ ...v, name: v.name ?? v.key ?? v.variable })).filter(v => typeof v.name === 'string' && v.name.trim());\nreturn this.normalizeExtractionResponse(parsed);","handlingStrategy":"validation","validationCode":null,"typeGuard":"const hasValidName = (v: unknown): v is { name: string; [k: string]: unknown } =>\n  !!v && typeof v === 'object' && typeof (v as any).name === 'string' && (v as any).name.trim().length > 0;","tryCatchPattern":"try {\n  await extractionService.extract({ modelKey, content });\n} catch (e) {\n  if (e instanceof VariableExtractionParseError && /\"name\" field/.test(e.message)) {\n    // remap alternate keys (key/variable) and filter empty names, then retry\n  }\n  throw e;\n}","preventionTips":["Map alternate keys (key, variable, id) to name in your pre-processing layer.","Filter out entries with blank names instead of failing the whole batch.","Require non-empty name strings in the prompt example."],"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"}