{"record":{"id":"d2826aad3a05703e","repo":"n8n-io/n8n","slug":"invalid-output-format","errorCode":null,"errorMessage":"Invalid output format","messagePattern":"Invalid output format","errorType":"exception","errorClass":"GuardrailError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/nodes-langchain/nodes/Guardrails/helpers/model.ts","lineNumber":118,"sourceCode":"\t\t// FIXME: https://github.com/langchain-ai/langchainjs/issues/9012\n\t\t// This is a manual fix to extract the text from the response.\n\t\t// Replace with const chain = chatPrompt.pipe(model).pipe(outputParser); when the issue is fixed.\n\t\tconst extractText = (content: MessageContent): string => {\n\t\t\tif (typeof content === 'string') {\n\t\t\t\treturn content;\n\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,","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/nodes-langchain/nodes/Guardrails/helpers/model.ts#L100-L136","documentation":"Thrown by the Guardrails model helper after an LLM guardrail call when the parsed output does not contain a numeric confidenceScore and a boolean flagged. The LLM is expected to return text that the OutputParser turns into { confidenceScore: number, flagged: boolean }; if either field has the wrong type the result is considered untrustworthy and a GuardrailError is raised with description 'Expected number and boolean fields'.","triggerScenarios":"extractText(result.content) yields text, outputParser.parse(text) returns an object, then the runtime checks typeof confidenceScore !== 'number' || typeof flagged !== 'boolean'. Fires when the model returns malformed structured output, the parser partially succeeds but yields null/undefined fields, or the model ignores the output format instructions.","commonSituations":"Lower-tier or non-instruction-tuned models ignoring the output schema; token limits truncating the model response before the fields appear; an output parser mismatch after a langchain version upgrade; the model returning a stringified boolean like \"true\" instead of a real boolean.","solutions":["Use a more capable or instruction-following model for the guardrail.","Tighten the OutputParser / prompt to enforce the JSON shape and add a repair step that coerces 'true'/'false' strings.","Increase max tokens / reduce input length so the model can complete the structured response.","Catch GuardrailError and treat it as a conservative 'flagged' outcome if your policy prefers fail-closed behavior."],"exampleFix":"// before: model returns { confidenceScore: \"0.9\", flagged: \"true\" }\n// after (repair before validation):\nconst repaired = { confidenceScore: Number(parsed.confidenceScore), flagged: String(parsed.flagged) === 'true' };","handlingStrategy":"validation","validationCode":"function isValidGuardrailOutput(o) {\n  return typeof o?.confidenceScore === 'number' && typeof o?.flagged === 'boolean';\n}","typeGuard":"function isParsedGuardrailResult(o): o is { confidenceScore: number; flagged: boolean } {\n  return o != null && typeof o.confidenceScore === 'number' && typeof o.flagged === 'boolean';\n}","tryCatchPattern":"try { ... } catch (e) {\n  if (e instanceof GuardrailError && e.message === 'Invalid output format') {\n    // fail-closed: treat as flagged\n  } else throw e;\n}","preventionTips":["Use instruction-tuned models that respect the output schema.","Coerce stringified booleans/numbers before validating.","Catch the specific GuardrailError and apply a fail-closed policy."],"tags":["guardrails","llm","output-parsing","validation"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}