{"record":{"id":"32e1a8fdb4fdae83","repo":"eyaltoledano/claude-task-master","slug":"ai-response-did-not-include-a-valid-subtasks-array","errorCode":null,"errorMessage":"AI response did not include a valid subtasks array.","messagePattern":"AI response did not include a valid subtasks array\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"scripts/modules/task-manager/expand-task.js","lineNumber":332,"sourceCode":"\t\t\tconst role = useResearch ? 'research' : 'main';\n\n\t\t\t// Call generateObjectService with the determined prompts and telemetry params\n\t\t\taiServiceResponse = await generateObjectService({\n\t\t\t\tprompt: promptContent,\n\t\t\t\tsystemPrompt: systemPrompt,\n\t\t\t\trole,\n\t\t\t\tsession,\n\t\t\t\tprojectRoot,\n\t\t\t\tschema: COMMAND_SCHEMAS['expand-task'],\n\t\t\t\tobjectName: 'subtasks',\n\t\t\t\tcommandName: 'expand-task',\n\t\t\t\toutputType: outputFormat\n\t\t\t});\n\n\t\t\t// With generateObject, we expect structured data – verify it before use\n\t\t\tconst mainResult = aiServiceResponse?.mainResult;\n\t\t\tif (!mainResult || !Array.isArray(mainResult.subtasks)) {\n\t\t\t\tthrow new Error('AI response did not include a valid subtasks array.');\n\t\t\t}\n\t\t\tgeneratedSubtasks = mainResult.subtasks.map((subtask) => ({\n\t\t\t\t...subtask,\n\t\t\t\tdependencies: subtask.dependencies ?? [],\n\t\t\t\tstatus: subtask.status ?? 'pending',\n\t\t\t\ttestStrategy: subtask.testStrategy ?? null\n\t\t\t}));\n\t\t\tlogger.info(`Received ${generatedSubtasks.length} subtasks from AI.`);\n\t\t} catch (error) {\n\t\t\tif (loadingIndicator) stopLoadingIndicator(loadingIndicator);\n\t\t\tlogger.error(\n\t\t\t\t`Error during AI call or parsing for task ${taskId}: ${error.message}`, // Added task ID context\n\t\t\t\t'error'\n\t\t\t);\n\t\t\tthrow error;\n\t\t} finally {\n\t\t\tif (loadingIndicator) stopLoadingIndicator(loadingIndicator);\n\t\t}","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/eyaltoledano/claude-task-master/blob/c0c98d367c55296bfe69e65680625b6db437af02/scripts/modules/task-manager/expand-task.js#L314-L350","documentation":"expandTask() asks the AI service to generate subtasks via generateObjectService and expects the response's mainResult to contain a subtasks array (structured generateObject output). If mainResult is missing or subtasks is not an array, it cannot append anything and throws before mutating the task.","triggerScenarios":"generateObjectService returns undefined/absent mainResult, or mainResult lacks a subtasks array — e.g. the model returned a single object instead of an array, schema validation silently degraded, or the service returned an error envelope.","commonSituations":"Weak/non-schema-following models returning { subtask: {...} } or { subtasks: {...} } (object not array); provider errors from bad API keys or rate limits surfaced as empty results; version drift between task-master and the AI services module changing the response shape.","solutions":["Retry the expand command; transient provider issues often produce empty results.","Verify your model configuration (task-master models) and use a model that reliably follows JSON schemas.","Check that the zod schema passed to generateObjectService requires a subtasks array so failures are retried upstream, and confirm ai-services-client version matches task-master's expectation of { mainResult: { subtasks: [...] } }."],"exampleFix":"// before (service layer)\nreturn { mainResult: parsed };   // parsed = { subtask: {...} }\n// after\nreturn { mainResult: { subtasks: Array.isArray(parsed.subtasks) ? parsed.subtasks : [parsed.subtask] } };","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function hasSubtasksArray(res) {\n  return !!res?.mainResult && Array.isArray(res.mainResult.subtasks);\n}","tryCatchPattern":"try {\n  await expandTask(taskId, context);\n} catch (err) {\n  if (err.message.includes('valid subtasks array')) {\n    console.error('AI returned no subtasks array — check model config and retry, or add subtasks manually.');\n  } else throw err;\n}","preventionTips":["Use a schema-compliant model for subtask generation (task-master models).","Ensure the generation schema strictly requires subtasks: z.array().","Retry once automatically; provider hiccups often cause empty results."],"tags":["ai-service","schema-validation","expand-task"],"backgroundTag":"ai-response-missing-field","analyzedSha":"c0c98d367c55296bfe69e65680625b6db437af02","analyzedAt":"2026-08-29T02:56:26.071Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}