Skip to content
Visual explainer 3h to learn 5 steps

Validate structured AI output before n8n branches on it

Check model output against a schema and allowed values, and send refusals, invalid fields and ambiguous requests to review.

By Invicly Tech, , in n8n, AI Automation and Voice Agents. About 10 minutes to read.

Topic 11 of 43

In short

A model can return text that looks right but has a missing field, a wrong value or a refusal. Define a JSON Schema, use the Structured Output Parser, then check allowed values and ranges in the workflow. Anything invalid, refused or unclear goes to a review branch instead of the booking path.

Think of it like this: Think of a customs desk. Every parcel the model hands over is opened and checked against the form before it passes into the country.

Explained simply

A model's reply is a suggestion, not a fact. It may leave out a field, use a category you never allowed, return a number outside the range, or politely refuse to answer. If your workflow trusts the reply blindly, a bad reply can send a booking to the wrong place.

The fix is to describe the answer you want in a JSON Schema. A schema is a small description: these fields exist, this one is a string, this one must be one of these words, this one is a number between 0 and 1. In n8n a Structured Output Parser sub-node tells the model about the shape and checks the reply against it.

Checking the shape is only the first step. A reply can have the right shape and still be wrong in meaning. So add checks of your own in the workflow: is the category on the allowed list, is the confidence at least your threshold, is the date in the future?

Anything that fails goes to review, not to the booking path. That includes refusals, invalid fields and cases where the model is unsure. Review is cheap compared with a wrong booking. When a run fails, open the execution and read the exact reply to see why.

Press Play on a diagram to walk through it one step at a time.

FlowValidate before you branch
  1. 1
    Model call

    With a structured output parser

  2. 2
    Schema check

    Fields and types present

  3. 3
    Value check

    Category in allowed list, score 0 to 1

  4. 4
    Confidence gate

    Low or ambiguous goes to review

  5. 5
    Branch

    Booking path or review path

MapWhere each output goes
Model output
  • Valid and clear
    • Continue to booking path
  • Invalid fields
    • Wrong type
    • Missing field
    • Category not allowed
  • Refusal
    • Model declined to answer
  • Ambiguous
    • Two categories close
    • Low confidence
CompareGood and bad outputs for the same request
Passes
  • category is cancel
  • confidence 0.92
  • summary is one sentence
  • All required fields present
Goes to review
  • category is refund (not allowed)
  • confidence 1.4 (out of range)
  • Reply says 'I cannot help'
  • Two close categories, low confidence

In depth

Define the output with JSON Schema

Write down the fields you need and no more: category, summary, confidence, and maybe needs_review. For each, give a type. For category use an enum, a fixed list of allowed words. For confidence give a minimum of 0 and a maximum of 1. Mark required fields. Set additional properties to false if you want extra fields rejected.

A schema is also documentation. It tells the next person exactly what the workflow expects, and you can reuse the same schema in other tools, such as a Python service in the AI Engineering path.

  • Few fields, each with a type
  • Enum for closed lists
  • Minimum and maximum for numbers
  • Required list for essential fields
LayersTwo kinds of check on a model reply
  1. 1
    Shape

    Valid JSON, required fields, right types (parser)

  2. 2
    Allowed values

    Category in the list, number in range (your check)

  3. 3
    Meaning and policy

    Not a refusal, not ambiguous, not unsafe (review)

Use the Structured Output Parser

In nodes that support it, such as the Basic LLM Chain, the Information Extractor and the AI Agent, you can turn on the output parser and connect a Structured Output Parser sub-node. It accepts either an example JSON or a full JSON Schema. It adds format instructions to the prompt and parses the reply.

If the model's reply does not fit, the node fails. You can add an Auto-fixing Output Parser, which asks a model to repair the reply once, but that costs another call and still needs checking. Either way, set the node's error handling so a failure goes to review rather than stopping silently.

  • Give an example or a full JSON Schema
  • Parsing failures are errors you can route
  • Auto-fixing adds a call and may still fail
  • Choose On Error: continue using error output

Check allowed values and ranges

After the model node, add an If or Code node that applies your own rules. Is category in the allowed list? Is confidence a number between 0 and 1 and at least 0.7? Is the requested start time in the future and inside opening hours?

These checks use code you control, so they behave the same every time. The threshold is a business choice. Start cautious, measure how many good answers go to review, and move the line only with evidence from a labelled set.

  • Same code, same answer every time
  • Start with a cautious confidence line
  • Check dates against real rules
  • Record why each item failed

Review for refusals and ambiguity

Sometimes a model declines: it says it cannot help, or returns an empty category. Sometimes two categories are close, or the message mixes two requests. None of these should reach a write action.

Send them to a review branch that records the original message, the model output and the reason. A person decides. Then look at the failed execution in n8n: the executions view shows the data that each node received and returned, so you can see whether the fault was the prompt, the schema or the input.

  • Review branch keeps message, output and reason
  • Do not auto-retry refusals forever
  • Look at the execution to find the cause
  • Update the schema or prompt, then retest the set

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Define a JSON Schema for the output~35m
  2. 2
    Use a structured output parser~35m
  3. 3
    Check allowed values and ranges~35m
  4. 4
    Route refusals and ambiguity to review~35m
  5. 5
    Inspect the failed execution~35m

Key points

  • Write the JSON Schema first. It lists each field, its type, and which fields are required.
  • Attach a Structured Output Parser so the model is told the format and its reply is parsed.
  • Parsing is not enough. Check that the category is in your allowed list and numbers are in range.
  • Send refusals, invalid output and unclear cases to a review branch, never to the booking action.
  • Open the failed execution to see the exact model output and which check failed.

Key terms

Tap a card to flip it. 0/5 known

Examples

A JSON Schema for a classification result

Paste this into the Structured Output Parser (define using JSON Schema). Evidence: replies with another category or a missing summary fail the parser.

json
{
  "type": "object",
  "properties": {
    "category": { "type": "string", "enum": ["new_booking", "change", "cancel", "question", "other"] },
    "summary": { "type": "string", "maxLength": 200 },
    "confidence": { "type": "number", "minimum": 0, "maximum": 1 }
  },
  "required": ["category", "summary", "confidence"],
  "additionalProperties": false
}

Gate before the branch

This Code node adds a route field. Only items marked ok continue to the booking path; the rest go to review with a reason.

javascript
const allowed = ["new_booking", "change", "cancel", "question", "other"];
return $input.all().map(({ json: j }) => {
  let reason = null;
  if (!j.output) reason = "no output";
  else if (!allowed.includes(j.output.category)) reason = "bad category";
  else if (typeof j.output.confidence !== "number") reason = "bad confidence";
  else if (j.output.confidence < 0.7) reason = "low confidence";
  return { json: { ...j, route: reason ? "review" : "ok", reason } };
});

Quick reference

  • Describe the reply with a JSON Schema: types, enum, min and max, required.
  • Structured Output Parser makes the model follow the shape and parses the reply.
  • A valid shape is not a valid meaning: check allowed values and ranges yourself.
  • Auto-fixing Output Parser makes an extra model call to repair a bad reply.
  • Refusals, invalid fields and ambiguity go to review, never to a write action.
  • Set On Error to continue using the error output on AI nodes.
  • Debug by opening the failed execution and reading each node's data.

Common mistakes

  • Trusting that valid JSON means valid meaning. A well-formed answer can still hold a category you never allowed.
  • Dropping failed items silently. They need a visible review queue or alert.
  • Setting a confidence threshold once and never checking it against real mistakes.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A reply is valid JSON with the right fields but the category is refund, which you never allowed. What catches it?

Question 2

2Where should a refusal from the model be sent?

Question 3

3What does the Structured Output Parser do?

Question 4

4What is a sensible way to choose a confidence threshold?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write a JSON Schema for a reply with category (enum of three values) and confidence (0 to 1).

  2. Exercise 2medium

    Test the gate with five hand-written replies: one good, one bad category, one missing output, one low confidence, one refusal text. Show the route each gets.

  3. Exercise 3hard

    Connect a real model in a lab to the parser and the gate. Force failures (empty message, an off-topic message) and use the executions view to explain each.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is JSON Schema hard to learn?
The basics are easy: type, properties, enum, required, minimum and maximum. They cover most workflow needs.
Why not just trust the model?
Models can leave out fields, invent values or refuse. A short check costs little and prevents wrong actions.
Should I retry an invalid reply?
One retry or one auto-fix can help. After that, send it to review so cost and delay stay bounded.
Where do I see what the model returned?
Open the execution in n8n and click the node. The panel shows the input and output items.

Go deeper

ShareXLinkedInWhatsApp