{"record":{"id":"b70b9915fc9369b6","repo":"linshenkx/prompt-optimizer","slug":"variables-index-is-not-a-valid-object","errorCode":null,"errorMessage":"variables[${index}] is not a valid object.","messagePattern":"variables\\[(.+?)\\] is not a valid object\\.","errorType":"validation","errorClass":"VariableExtractionParseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-extraction/service.ts","lineNumber":211,"sourceCode":"    if (!data || typeof data !== 'object') {\n      throw new VariableExtractionParseError('Extraction result is not a valid object.');\n    }\n\n    // 验证 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        );","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-extraction/service.ts#L193-L229","documentation":"While validating each entry of the variables array, entry at position ${index} is null, a primitive, or otherwise not an object. Each extracted variable must be an object with name/value/position fields, so normalization aborts on the first malformed entry and reports its index.","triggerScenarios":"The LLM's variables array contains a null, a string (e.g. just the variable name), or a number at the reported index. Reached via extract() when the model emits a heterogeneous or degenerate array.","commonSituations":"Model emits variables as a plain string array (\"variables\": [\"user.name\", \"date\"]) instead of objects; model inserts null placeholders; mixed-quality output from a weak model; prompt example showed a shorthand format.","solutions":["Log the raw variables array to see the actual shape at the failing index.","Fix the prompt/template to require each entry be an object like {name, value, position}.","Add a pre-normalization filter/mapper converting shorthand entries: strings -> {name: s, value: '', position: {start: 0, end: 0}}.","Switch to a model with stronger instruction-following."],"exampleFix":"// before\nreturn this.normalizeExtractionResponse(parsed);\n\n// after\nparsed.variables = parsed.variables.map(v => typeof v === 'string' ? { name: v, value: '', position: { start: 0, end: 0 } } : v).filter(Boolean);\nreturn this.normalizeExtractionResponse(parsed);","handlingStrategy":"validation","validationCode":null,"typeGuard":"const allVariablesAreObjects = (vars: unknown[]): boolean =>\n  vars.every(v => !!v && typeof v === 'object' && !Array.isArray(v));","tryCatchPattern":"try {\n  await extractionService.extract({ modelKey, content });\n} catch (e) {\n  if (e instanceof VariableExtractionParseError && /is not a valid object/.test(e.message)) {\n    // filter/map string entries to objects, then retry\n  }\n  throw e;\n}","preventionTips":["Filter falsy entries and map shorthand strings to full objects before validation.","Show the exact per-variable object shape in the prompt's few-shot example.","Drop obviously malformed entries (null/primitives) rather than letting the whole extraction fail."],"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"}