Blog/AI Security

LLM output validation: check AI answers before they touch your systems

Set up schema, policy and content checks so model output is treated as untrusted input before it reaches users, databases or other software.

By Sagar Makker · 7 min read · October 2026
Cctv Camera
In short
Treat every model response as untrusted input, exactly like a form field filled in by a stranger.
Validate structure, values and policy in code before the output is stored, rendered or executed.
Define a safe fallback for every failed check so the feature degrades instead of breaking or leaking.
The problem

This is for engineering heads and product owners at companies shipping AI features into real workflows: ticket routing, quote drafting, CRM updates, claim summaries. The model writes text, and that text often flows straight into a database, a web page or another API call. When output is trusted by default, one malformed or manipulated answer can corrupt records, break a screen or trigger the wrong action. This post gives you a validation layer you can design in a week, with clear owners and checks.

A sales team uses an AI feature that reads inbound emails and fills CRM fields. One day a supplier email contains odd formatting, the model returns a discount field with text instead of a number, and the integration writes it anyway. Reports break, a pricing rule fires on garbage, and nobody notices for a week because the pipeline never complained.

The same pattern appears in customer-facing chat, where model output is rendered as HTML without encoding, and in internal agents whose answers are passed straight into a search query or a workflow step. Teams test whether the answer is helpful, not whether it is safe to consume. The model becomes a trusted component it was never designed to be.

Why it happens
01Output is consumed directly by code with no schema or type checks in between
02Teams assume the system prompt alone will keep responses in the right format
03Rendered output is not encoded, so markup in a response is interpreted by the browser
04No allow-list for values such as status codes, account IDs or action names
05No defined fallback, so a failed answer is either passed through or crashes the flow
How to fix it
1
Map every place output is consumed
The tech lead lists each destination for model output: screens, database writes, API calls, emails, files. Mark which ones change data or reach customers, because those need the strictest checks and should be validated first.
2
Define a strict schema per destination
For each destination, the developer writes a JSON schema or typed model with required fields, types, lengths and enumerated values. Reject anything that does not match rather than trying to repair it silently in production.
3
Add allow-lists and business rule checks
Product and engineering agree on rules beyond format: discount within a ceiling you set, account ID must belong to the current customer, action must be on an approved list. Encode these as plain code checks after schema validation.
4
Encode and constrain before rendering
Front-end owners render model text as plain text or sanitised markdown, never raw HTML. Strip links to unknown domains if your use case does not need them, and never pass output into a shell, query or template unescaped.
5
Design fallbacks and log every rejection
The service owner defines what happens on failure: retry once, show a neutral message, or route to a person. Every rejection is logged with the check that failed so the team can spot patterns weekly.
Worked example
A B2B distributor's order-capture assistant

A mid-sized industrial distributor in Ahmedabad used an AI assistant to turn dealer emails into draft sales orders. Before, the assistant's output went straight into the order table, and occasional free-text quantities or invented product codes created orders that the warehouse had to untangle by hand. The team added a schema for each order line, an allow-list check against the live product master, a quantity ceiling per dealer, and a rule that any failure routes the draft to the order desk. After the change, bad drafts stopped reaching the warehouse, the order desk saw a short daily queue of flagged items with the reason attached, and the engineering team used the rejection log to improve prompts where failures clustered.

Common mistakes
×
Asking the model to validate itself
A second prompt saying check your answer is still probabilistic. Use deterministic code for format and business rules, and keep model-based review only for judgement calls a person later confirms.
×
Silently repairing malformed output
Auto-fixing broken fields hides real problems and can turn a harmless error into a wrong value. Reject, log the reason, and use the fallback path you defined instead.
×
Validating only the happy-path fields
Attackers and edge cases live in optional fields, comments and free text. Apply length limits, encoding and content checks to every field, including those your screen does not show today.
Where an AI agent helps
Input
Raw model responses and the schema plus business rules for each destination
Agent does
Checks structure, values and policy, blocks unsafe content, applies fallbacks and logs rejections
Human gate
A reviewer approves any change to schemas, allow-lists or value ceilings
Outcome
Validated payloads, a rejection log, and a weekly failure pattern summary
Checklist
List every system that consumes model output
Write a strict schema for each output destination
Add allow-lists for IDs, statuses and action names
Render model text as plain text or sanitised markdown only
Define a fallback path for every failed check
Review the rejection log every week
What to measure
Share of responses passing validation
Rejections by check type per week
Invalid records reaching production tables
Time to fix a recurring failure pattern
Questions
Does structured output mode from the model provider remove the need for validation?

No. It improves format consistency, but it does not check business rules, tenant ownership or safe values. Keep your own schema and rule checks in code, because they are the only layer you fully control and can test.

Will validation make the AI feature slower?

Schema and rule checks in code typically add very little time compared with the model call itself. The occasional retry or human hand-off costs more, but that is the price of not writing bad data.

What should we do with output that fails validation?

Follow a fallback you defined in advance: one retry with a clearer instruction, a neutral message to the user, or routing to a person. Never pass it through, and always log the failed check with a request ID.

Who should own output validation rules?

Engineering owns the schemas and code, while the product or business owner signs off on value ceilings and allow-lists. Security reviews any destination that can change data, move money or reach customers directly.

Keep reading
All posts →
Cctv CameraAI Security
AI supply chain security: checking model provenance before you deploy
7 min readRead →
Computer CodeAI Security
A practical AI security checklist before you launch an AI feature
5 min readRead →
Computer CodeAI Security
Tenant isolation for AI features in multi-tenant SaaS products
7 min readRead →
Next step

Want this fixed in your business?

Send the workflow. You get a scope and a fixed price within two working days — or a straight answer that it isn't a fit.

Secure by default · find → fix → verify · authorised scope only
Send a brief →enquiry@xdqlabs.com