{"record":{"id":"2e70d2033af6f7eb","repo":"linshenkx/prompt-optimizer","slug":"variable-at-index-i-has-empty-name","errorCode":null,"errorMessage":"Variable at index ${i} has empty name.","messagePattern":"Variable at index (.+?) has empty name\\.","errorType":"validation","errorClass":"VariableValueGenerationValidationError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":92,"sourceCode":"   */\n  private validateRequest(request: VariableValueGenerationRequest): void {\n    if (!request.promptContent?.trim()) {\n      throw new VariableValueGenerationValidationError('Prompt content must not be empty.');\n    }\n\n    if (!request.generationModelKey?.trim()) {\n      throw new VariableValueGenerationValidationError('Generation model key must not be empty.');\n    }\n\n    if (!request.variables || request.variables.length === 0) {\n      throw new VariableValueGenerationValidationError('Variables list must not be empty.');\n    }\n\n    // 验证每个变量\n    for (let i = 0; i < request.variables.length; i++) {\n      const variable = request.variables[i];\n      if (!variable.name?.trim()) {\n        throw new VariableValueGenerationValidationError(`Variable at index ${i} has empty name.`);\n      }\n    }\n  }\n\n  /**\n   * 验证模型是否存在\n   */\n  private async validateModel(modelKey: string): Promise<void> {\n    const model = await this.modelManager.getModel(modelKey);\n    if (!model) {\n      throw new VariableValueGenerationModelError(modelKey);\n    }\n  }\n\n  /**\n   * 获取变量值生成模板\n   */\n  private async getGenerationTemplate(): Promise<Template> {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L74-L110","documentation":"validateRequest iterates the variables array and throws VariableValueGenerationValidationError with the offending index when any variable's name is missing or whitespace-only. The name is the join key between the request and the model's generated values, so blank names break the response mapping.","triggerScenarios":"Calling generate() with variables containing an entry like { name: '', value: 'x' } or { name: '   ' } or with the name field absent entirely; the message names the exact index.","commonSituations":"LLM extraction returned a variable with an empty name (extraction normalization trims but doesn't reject empty results upstream); manual variable lists built from user input without trimming; CSV/spreadsheet imports producing blank rows.","solutions":["Filter out blank-named variables before calling generate","Trim and validate names when building the variables array (and reject empties at extraction time)","Log the index from the message to find the offending entry quickly"],"exampleFix":"// before\nawait gen.generate({ ...req, variables });\n\n// after\nconst cleaned = variables\n  .map(v => ({ ...v, name: v.name?.trim() ?? '' }))\n  .filter(v => v.name.length > 0);\nif (!cleaned.length) throw new Error('No named variables to generate');\nawait gen.generate({ ...req, variables: cleaned });","handlingStrategy":"validation","validationCode":"const bad = req.variables.findIndex(v => !v.name?.trim());\nif (bad >= 0) throw new Error(`variable[${bad}] has empty name`);","typeGuard":"const namesValid = (vars: VariableToGenerate[]) => vars.every(v => typeof v.name === 'string' && v.name.trim().length > 0);","tryCatchPattern":"catch (e) { if (e instanceof VariableValueGenerationValidationError && /empty name/i.test(e.message)) { const i = Number(e.message.match(/index (\\d+)/)?.[1]); /* drop variables[i] and retry */ } throw e; }","preventionTips":["Trim and reject blank names at data-entry time","Parse the index out of the message to auto-repair the offending entry"],"tags":["request-validation","data-cleaning","variable-value-generation"],"backgroundTag":"invalid-request-parameters","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}