{"record":{"id":"2c0e0ab6862e603c","repo":"n8n-io/n8n","slug":"failed-to-generate-valid-workflow-after-max-vali","errorCode":null,"errorMessage":"Failed to generate valid workflow after ${MAX_VALIDATE_ATTEMPTS} validate attempts.","messagePattern":"Failed to generate valid workflow after (.+?) validate attempts\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/ai-workflow-builder.ee/src/code-builder/code-builder-agent.ts","lineNumber":657,"sourceCode":"\t\t}\n\t\tif (dispatchResult.parseDuration !== undefined) {\n\t\t\tstate.parseDuration = dispatchResult.parseDuration;\n\t\t}\n\t\tif (dispatchResult.workflowReady) {\n\t\t\tstate.sourceCode = dispatchResult.sourceCode ?? null;\n\t\t\tstate.textEditorValidateAttempts = 0;\n\t\t\treturn { shouldBreak: true };\n\t\t}\n\n\t\t// Increment validate attempts counter when validate_workflow was called but failed\n\t\tconst hadValidateCall = toolCalls.some((tc) => tc.name === 'validate_workflow');\n\t\tif (hadValidateCall && !dispatchResult.workflowReady) {\n\t\t\tstate.textEditorValidateAttempts++;\n\t\t}\n\n\t\t// Check for too many validate failures\n\t\tif (textEditorEnabled && state.textEditorValidateAttempts >= MAX_VALIDATE_ATTEMPTS) {\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to generate valid workflow after ${MAX_VALIDATE_ATTEMPTS} validate attempts.`,\n\t\t\t);\n\t\t}\n\n\t\treturn { shouldBreak: false };\n\t}\n\n\t/**\n\t * Handle text editor auto-finalize when no tool calls\n\t */\n\tprivate async *handleTextEditorAutoFinalize(params: {\n\t\ttextEditorHandler: TextEditorHandler;\n\t\tcurrentWorkflow?: WorkflowJSON;\n\t\tmessages: BaseMessage[];\n\t\tstate: AgenticLoopState;\n\t}): AsyncGenerator<StreamOutput, { shouldBreak: boolean }, unknown> {\n\t\tconst { textEditorHandler, currentWorkflow, messages, state } = params;\n","sourceCodeStart":639,"sourceCodeEnd":675,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/ai-workflow-builder.ee/src/code-builder/code-builder-agent.ts#L639-L675","documentation":"Inside the agentic loop's tool-call handler, the model kept invoking validate_workflow without the workflow ever becoming ready. Each failed validation increments textEditorValidateAttempts; once it reaches MAX_VALIDATE_ATTEMPTS the loop is aborted. This means the model repeatedly produced code that fails validation and exhausted its repair budget.","triggerScenarios":"Thrown at packages/@n8n/ai-workflow-builder.ee/src/code-builder/code-builder-agent.ts:657 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the validation feedback returned by validate_workflow on the final attempts to find the recurring defect","Retry with a fresh session — sometimes the model escapes a bad self-repair loop on restart","Increase MAX_VALIDATE_ATTEMPTS if workflows are consistently complex enough to need more repair rounds","Use a stronger build model or provide a more detailed plan so the first code generation is closer to valid"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","contentChangedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}