{"record":{"id":"bd329d5cacbdf388","repo":"n8n-io/n8n","slug":"failed-to-parse-workflow-code-after-3-consecutive","errorCode":null,"errorMessage":"Failed to parse workflow code after 3 consecutive attempts.","messagePattern":"Failed to parse workflow code after 3 consecutive attempts\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/ai-workflow-builder.ee/src/code-builder/code-builder-agent.ts","lineNumber":539,"sourceCode":"\t\t} catch (error) {\n\t\t\tawait parentRunManager?.handleChainError(error);\n\t\t\tthrow error;\n\t\t}\n\n\t\treturn {\n\t\t\tworkflow: state.workflow,\n\t\t\tparseDuration: state.parseDuration,\n\t\t\tsourceCode: state.sourceCode,\n\t\t\titeration: state.iteration,\n\t\t};\n\t}\n\n\t/**\n\t * Check if we've hit the consecutive parse error limit\n\t */\n\tprivate checkConsecutiveParseErrors(count: number): void {\n\t\tif (count >= 3) {\n\t\t\tthrow new Error('Failed to parse workflow code after 3 consecutive attempts.');\n\t\t}\n\t}\n\n\t/**\n\t * Process a single iteration of the agentic loop\n\t */\n\tprivate async *processIteration(params: {\n\t\tllmResult: { hasToolCalls: boolean; response: AIMessage };\n\t\tmessages: BaseMessage[];\n\t\tcurrentWorkflow?: WorkflowJSON;\n\t\ttextEditorEnabled: boolean;\n\t\ttextEditorHandler?: TextEditorHandler;\n\t\ttextEditorToolHandler?: TextEditorToolHandler;\n\t\tstate: AgenticLoopState;\n\t}): AsyncGenerator<StreamOutput, { shouldBreak: boolean }, unknown> {\n\t\tconst {\n\t\t\tllmResult,\n\t\t\tmessages,","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/ai-workflow-builder.ee/src/code-builder/code-builder-agent.ts#L521-L557","documentation":"The code-builder agent loop keeps a counter of consecutive failures to parse the LLM's generated workflow code. When that counter reaches the hard cap of 3, checkConsecutiveParseErrors aborts the whole run instead of burning more LLM iterations on unparseable output. It signals persistent model output quality problems (invalid syntax, malformed JSON, truncated code), not a transient hiccup.","triggerScenarios":"Thrown at packages/@n8n/ai-workflow-builder.ee/src/code-builder/code-builder-agent.ts:539 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the last generated sourceCode captured in agent state to see exactly why parsing failed","Retry the run: transient model degradation can produce a burst of bad outputs","Switch to a stronger/more capable build model if failures recur","Check that the parser configuration (grammar, wrap/sanitize steps) matches the code format the prompt requests","Lower workflow complexity of the request — very large workflows can truncate model output mid-code"],"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"}