AI & Automation

Validating structured output from AI workflows

Treat generated JSON as external input even when it matches a requested format.

Anant Dhiman · · 1 min read

AI processor converting input documents into structured output
On this page

A language model can produce well-formed JSON that still violates business rules. Syntax validity, schema validity and semantic correctness are distinct checks.

Implementation approach

Define a narrow output schema with required fields and allowed values. Validate responses at runtime and reject unknown identifiers or unsupported actions before passing output to downstream systems.

Example

type Decision = { action: 'review' | 'skip'; reason: string };

Validation and edge cases

Test malformed JSON, unsupported actions and plausible but incorrect identifiers. Use bounded retries for formatting failures and route ambiguous or consequential decisions to manual review.

Key takeaway

Never let generated instructions directly trigger arbitrary commands or bypass permissions.

← All insights

Related articles