n8n-io/n8n · error · GuardrailError

Invalid output format

Error message

Invalid output format

What it means

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'.

Source

Thrown at packages/@n8n/nodes-langchain/nodes/Guardrails/helpers/model.ts:118

		// FIXME: https://github.com/langchain-ai/langchainjs/issues/9012
		// This is a manual fix to extract the text from the response.
		// Replace with const chain = chatPrompt.pipe(model).pipe(outputParser); when the issue is fixed.
		const extractText = (content: MessageContent): string => {
			if (typeof content === 'string') {
				return content;
			}
			if (content[0].type === 'text') {
				return content[0].text as string;
			}
			throw new Error('Invalid content type');
		};

		const text = extractText(result.content);
		const { confidenceScore, flagged } = await outputParser.parse(text);

		// Validate output consistency
		if (typeof confidenceScore !== 'number' || typeof flagged !== 'boolean') {
			throw new GuardrailError(name, 'Invalid output format', 'Expected number and boolean fields');
		}

		return { confidenceScore, flagged };
	} catch (error) {
		if (isLangChainParserError(error)) {
			throw new GuardrailError(name, 'Failed to parse output', MODEL_OUTPUT_PARSER_ERROR_MESSAGE);
		}
		throw new GuardrailError(
			name,
			`Guardrail validation failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
			error?.description,
		);
	}
}

export async function runLLMValidation(
	name: string,
	inputText: string,

View on GitHub (pinned to 5ac6606e81)

Solutions

  1. Use a more capable or instruction-following model for the guardrail.
  2. Tighten the OutputParser / prompt to enforce the JSON shape and add a repair step that coerces 'true'/'false' strings.
  3. Increase max tokens / reduce input length so the model can complete the structured response.
  4. Catch GuardrailError and treat it as a conservative 'flagged' outcome if your policy prefers fail-closed behavior.

Example fix

// before: model returns { confidenceScore: "0.9", flagged: "true" }
// after (repair before validation):
const repaired = { confidenceScore: Number(parsed.confidenceScore), flagged: String(parsed.flagged) === 'true' };
Defensive patterns

Strategy: validation

Validate before calling

function isValidGuardrailOutput(o) {
  return typeof o?.confidenceScore === 'number' && typeof o?.flagged === 'boolean';
}

Type guard

function isParsedGuardrailResult(o): o is { confidenceScore: number; flagged: boolean } {
  return o != null && typeof o.confidenceScore === 'number' && typeof o.flagged === 'boolean';
}

Try / catch

try { ... } catch (e) {
  if (e instanceof GuardrailError && e.message === 'Invalid output format') {
    // fail-closed: treat as flagged
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12). Data as JSON: /api/errors/d2826aad3a05703e. Report an issue: GitHub.