Add a Validation Gate: If Required Keys Missing or Invalid
Explore how to implement a validation gate in AI automation workflows to detect missing or invalid keys in LLM output. This lesson teaches adding a retry loop with bounded attempts and a human-in-the-loop review to handle failures safely, preventing unvalidated data from routing and ensuring reliable decision making.
We'll cover the following...
The last lesson trusted the router to branch correctly as long as the triage fields were normalized, but normalization only reshapes what's there, it doesn't catch output that's simply wrong. A triage run can fail without any node throwing an error if the LLM returns text that looks close to JSON but is not usable by the router. One common symptom is needs_human arriving as the string "true" instead of the boolean true, so a condition like needs_human is true never matches and the workflow falls into a default branch. This lesson closes that gap: we'll add a validation gate before the router, wire a bounded retry loop for correctable failures, and hand off to a human reviewer when retries run out.
Here is one representative broken output that creates that silent misroute.
Sure, here's the triage result:{"category":"billing","priority":"normal","next_action":"reply","needs_human":"true"}
A validation gate prevents this by sitting between the LLM node and the router node, checking that the output can be parsed and that required fields match the schema before any branch can trigger side effects. Nothing else changes yet, but control flow becomes explicit. The rule to keep is simple.
Rule: Never let unvalidated model output control routing or external side effects.
To make the flow concrete before wiring it into n8n, we can compare the before and after paths at the workflow level.
When the gate is in place, a parse failure or type mismatch no longer falls through to an unrelated route. The validation node emits a pass signal that flows to routing, or a fail signal that flows to a retry loop and eventually to human review, so misroutes become observable states instead of hidden behavior.
In the Support Inbox Triage workflow, the gate sits between the storage step and the normalization step from the last lesson.
Validate: triage JSON: A Code node that checks the model's text and adds is_valid and validation_errors to the item.
Gate: is_valid?: An If node. Its true output continues to Normalize: triage fields and the router; its false output goes to the retry check.
Retry budget left? (attempt_count < 3): An If node that decides between one more correction attempt and a human.
Retry: count attempt, Action: ...