{"record":{"id":"d3597d8d7e596c0c","repo":"n8n-io/n8n","slug":"failed-to-parse-output","errorCode":null,"errorMessage":"Failed to parse output","messagePattern":"Failed to parse output","errorType":"exception","errorClass":"GuardrailError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/nodes-langchain/nodes/Guardrails/helpers/model.ts","lineNumber":124,"sourceCode":"\t\t\t}\n\t\t\tif (content[0].type === 'text') {\n\t\t\t\treturn content[0].text as string;\n\t\t\t}\n\t\t\tthrow new Error('Invalid content type');\n\t\t};\n\n\t\tconst text = extractText(result.content);\n\t\tconst { confidenceScore, flagged } = await outputParser.parse(text);\n\n\t\t// Validate output consistency\n\t\tif (typeof confidenceScore !== 'number' || typeof flagged !== 'boolean') {\n\t\t\tthrow new GuardrailError(name, 'Invalid output format', 'Expected number and boolean fields');\n\t\t}\n\n\t\treturn { confidenceScore, flagged };\n\t} catch (error) {\n\t\tif (isLangChainParserError(error)) {\n\t\t\tthrow new GuardrailError(name, 'Failed to parse output', MODEL_OUTPUT_PARSER_ERROR_MESSAGE);\n\t\t}\n\t\tthrow new GuardrailError(\n\t\t\tname,\n\t\t\t`Guardrail validation failed: ${error instanceof Error ? error.message : 'Unknown error'}`,\n\t\t\terror?.description,\n\t\t);\n\t}\n}\n\nexport async function runLLMValidation(\n\tname: string,\n\tinputText: string,\n\t{ model, prompt, threshold, systemMessage }: LLMConfig,\n): Promise<GuardrailResult> {\n\ttry {\n\t\tconst result = await runLLM(name, model, prompt, inputText, systemMessage);\n\t\tconst triggered = result.flagged && result.confidenceScore >= threshold;\n\t\treturn {","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/nodes-langchain/nodes/Guardrails/helpers/model.ts#L106-L142","documentation":"Thrown by the Guardrails model helper inside its try/catch when the caught error is identified as a LangChain OutputParserException (isLangChainParserError returns true). It indicates the LLM output could not be parsed at all, as opposed to being parsed but malformed (which raises 'Invalid output format'). The description is the constant MODEL_OUTPUT_PARSER_ERROR_MESSAGE.","triggerScenarios":"outputParser.parse(text) throws an OutputParserException because the model output does not match the expected format at all (missing JSON, unparseable structure). The catch detects this specific error type and throws GuardrailError 'Failed to parse output'.","commonSituations":"Model returns natural-language prose instead of structured output; model wraps JSON in markdown fences the parser cannot strip; langchain OutputParser version change tightening parsing; prompt template not including the format instructions.","solutions":["Ensure the prompt includes and reinforces the required output format instructions.","Upgrade or pin the langchain OutputParser so its parsing leniency matches what the model emits.","Switch to a model with stronger instruction-following for structured output.","If the model persistently wraps output in code fences, pre-strip fences before calling parse."],"exampleFix":"// before: raw model output passed straight to parser\ntext = extractText(result.content);\n// after: strip markdown fences before parsing\ntext = extractText(result.content).replace(/^```(?:json)?|```$/g, '').trim();","handlingStrategy":"try-catch","validationCode":"// Strip code fences and validate JSON before invoking the parser.\nfunction preprocessModelOutput(text) {\n  return text.replace(/^```(?:json)?|```$/g, '').trim();\n}","typeGuard":null,"tryCatchPattern":"try { ... } catch (e) {\n  if (isLangChainParserError(e)) {\n    // retry with a stricter prompt or a different model\n  } else throw e;\n}","preventionTips":["Always include format instructions in the guardrail prompt.","Pin langchain versions so parser behavior is stable.","Pre-clean model output (strip fences, whitespace) before parsing."],"tags":["guardrails","llm","output-parsing","langchain"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}