language models in the pipeline · lesson 2 of 10 · sources checked 2026-11-20
structured output, validation, and failure handling
This lesson checks each field of the model's output, links items by fixed rules first, and gives each failed call a soft result.
status of this lesson: documented in my test systempurpose
The output of a model is text from a source that you do not control. One wrong value that goes into the database stays there and spreads to each page that reads it. A structured output and checks in code keep such a value out.
objectives
- Design a check for each field of the output of a model, with whitelists, clamps, and slug checks.
- Build a deterministic linker that uses a model value only when it is a catalog slug.
- Describe the soft failure paths of a model call.
- Explain what a share of stored items with an entity ID measures, and what it does not measure.
new terms
how it works
A large language model (LLM) can give an output in the correct format with a wrong value in it. Thus the format alone is not sufficient. Code checks each field before it stores the output.
The OWASP Gen AI Security Project advises to apply input validation on the responses that come from the model to backend functions OWASP Gen AI Security Project, source 1.
a check for each field
On the benchmark platform, my test system, the tagger asks the model for a JSON object. Then code checks each field by hand. A catalog slug is the short fixed name of one catalog entry, for example orbit. In my model, each field has one of these three checks:
| check | what it does | example |
|---|---|---|
| whitelist | Keeps a value only when it is in a fixed list | Topic politics is not in the list, so the topic becomes other. |
| Clamp | Moves a number into its permitted range | Relevance 3 becomes 1. |
| Slug check | Keeps a vendor value only when it is a catalog slug | Vendor chrome is not a slug, so the vendor becomes null. |
An output that code cannot parse as JSON gives a null result. The code never throws on a bad output.
a catalog name is a whole word
A name match must look for the catalog name as a whole word. A match on a part of a word links wrong items.
example The catalog has the vendor orbit. An item says "An orbital launch report". A match on a part of the word links this item to the vendor. A match with word boundaries does not.
On the benchmark platform, a match on a part of a word tagged items with a vendor whose name is a common English word. The correction is word boundaries.
the linker decides, not the model
A deterministic linker links each item by fixed rules in a fixed order:
- The source owner, when it is a catalog entry.
- A catalog name as a whole word in the item text.
- The model value, only when it is a catalog slug.
- Else, the unclassified state, with a reason.
On the benchmark platform, the model wrote vendor values such as chrome, and the catalog has no such slug. A direct write of these values gives links to entries that do not exist. The linker keeps them out.
The unclassified items are counted. For example, a rise of the count shows that the rules miss a new vendor name.
soft failure paths
A model call can fail in different ways. Each failure gives a soft result, not a throw. For example, an output with no JSON gives an empty result:
- No output, or no JSON: an empty result.
- One field fails its check: a null value for that field.
- The cause goes to the log one time for each process.
On the benchmark platform, a model endpoint that did not answer made each job throw. Each job went to the dead-letter queue (DLQ), and the log filled with the same error trace. The correction is an empty result and one log line for each process.
A missing key, a wrong request shape, and a refused setting all gave the same empty result. The correction selects the provider at build time and tests the request shape: Where a model belongs in a collection pipeline (not yet published).
numbers come from the database
A model can write a number that its input does not hold, for example a success rate for a study that gives no rate. Thus a number in a stored text comes from the database. The model writes only the words around it.
the two sides
| automation side | detection side |
|---|---|
| A structured output and a check for each field keep a wrong output of the model out of the database. The linker turns a free model value into a catalog slug or into a counted unclassified state. | A detection product can use the same design for its own classifier. Rules decide first, a model decides the cases that no rule decides, and an unknown state is counted. A site that tags its traffic with a model can keep the model value apart from the decision of its check. |
design decisions
| option | gain | cost | when it is correct |
|---|---|---|---|
| JSON format and a check in code for each field | Each stored value is a permitted value | Each field must have a rule | Each output of the model that goes into the database |
| Store the model value with no check | Less code | Wrong values and links to entries that do not exist | Never for stored data |
| Model value only when it is a catalog slug | No link to an entry that does not exist | Items with a new vendor stay unclassified | Each link to a catalog entry |
| A null field in place of a failed answer | The other fields stay usable | A null value must have a meaning downstream | One field fails, and the others pass |
| An empty result and one log line in place of a throw | The job goes on, and the log stays readable | The failure shows only in the log and in the counts | The model endpoint does not answer |
diagram
The first diagram gives the trust order of the linker.
trust order for one item: the strongest evidence links first, the model value last
The second diagram gives the failure states of one model call.
failure states of one model call
code example
The two examples use synthetic model outputs, items, and catalog entries. No code calls a model.
The first example checks each field of the JSON output of a tagger.
Check each field of the JSON output of a tagger with a whitelist, a clamp, and a slug check. Give a soft failure for a bad output.
// Validate the JSON output of a tagger model field by field.
// Whitelists, a clamp, and a slug check. A bad output gives null, never a throw.
// The model outputs and the catalog are synthetic.
type Tags = { topic: string; relevance: number; vendor: string | null };
const TOPICS = new Set(["detection", "proxy", "browser", "other"]);
const CATALOG_SLUGS = new Set(["orbit", "lumen"]);
const logged = new Set<string>();
function logOnce(key: string, text: string): void {
if (logged.has(key)) return;
logged.add(key);
console.log(`log: ${text}`);
}
function parseTags(raw: string): Tags | null {
let data: unknown;
try {
data = JSON.parse(raw);
} catch {
logOnce("parse", "output is not JSON; result is null");
return null;
}
if (typeof data !== "object" || data === null) return null;
const d = data as Record<string, unknown>;
const topic = typeof d.topic === "string" && TOPICS.has(d.topic) ? d.topic : "other";
const r = typeof d.relevance === "number" && Number.isFinite(d.relevance) ? d.relevance : 0;
const relevance = Math.min(1, Math.max(0, r)); // clamp to 0..1
const vendor = typeof d.vendor === "string" && CATALOG_SLUGS.has(d.vendor) ? d.vendor : null;
return { topic, relevance, vendor };
}
const outputs = [
'{"topic":"proxy","relevance":0.7,"vendor":"orbit"}',
'{"topic":"politics","relevance":3,"vendor":"chrome"}',
"Here is the JSON you asked for:",
"Sure! The answer is below.",
];
for (const o of outputs) console.log(JSON.stringify(parseTags(o)));output
The checked result of four synthetic outputs, and one log line.
{"topic":"proxy","relevance":0.7,"vendor":"orbit"}
{"topic":"other","relevance":1,"vendor":null}
log: output is not JSON; result is null
null
nullThe output shows the three checks on the second model output. The two model outputs with no JSON give null, and the log line occurs one time only.
The second example links items in the trust order of the first diagram.
Link each item to a catalog slug by fixed rules, or give the unclassified state with a reason, as a discriminated union.
// A deterministic linker: a catalog slug, or "unclassified" with a reason.
// The model value counts only if it is a catalog slug. The data is synthetic.
type Link =
| { state: "linked"; slug: string; by: "source owner" | "name match" | "model value" }
| { state: "unclassified"; reason: string };
const CATALOG: Record<string, string> = { orbit: "Orbit", lumen: "Lumen" };
function link(item: { owner: string | null; text: string; modelVendor: string | null }): Link {
if (item.owner && item.owner in CATALOG) return { state: "linked", slug: item.owner, by: "source owner" };
for (const [slug, name] of Object.entries(CATALOG)) {
if (new RegExp(String.raw`\b${name}\b`, "i").test(item.text)) return { state: "linked", slug, by: "name match" };
}
if (item.modelVendor === null) return { state: "unclassified", reason: "no rule and no model value" };
if (item.modelVendor in CATALOG) return { state: "linked", slug: item.modelVendor, by: "model value" };
return { state: "unclassified", reason: `model value "${item.modelVendor}" is not a catalog slug` };
}
const items = [
{ owner: "lumen", text: "Release notes", modelVendor: null },
{ owner: null, text: "Orbit adds a new check", modelVendor: "lumen" },
{ owner: null, text: "An orbital launch report", modelVendor: null },
{ owner: null, text: "A new browser test", modelVendor: "chrome" },
];
const counts: Record<string, number> = {};
for (const it of items) {
const r = link(it);
counts[r.state] = (counts[r.state] ?? 0) + 1;
console.log(r.state === "linked" ? `linked ${r.slug} by ${r.by}` : `unclassified: ${r.reason}`);
}
console.log(`counts: linked ${counts.linked ?? 0}, unclassified ${counts.unclassified ?? 0}`);output
The result of four synthetic items, and the count of each state.
linked lumen by source owner
linked orbit by name match
unclassified: no rule and no model value
unclassified: model value "chrome" is not a catalog slug
counts: linked 2, unclassified 2The output shows that the name match wins over the model value lumen on the second item. The word "orbital" does not match orbit, and the value chrome is not a slug.
data
An entity ID is the stored link of an item to a catalog entry. The chart gives the share of stored news items with an entity ID, for each month of creation.
share of news items with an entity id, by month of creation
status of this figure: measured by me
lines: interval, level 0.95
source: Benchmark platform, data (my test system): stored news items, entity ID column; date 2026-11-03; sample of 5,849
data table
| series | month of creation | % | interval |
|---|---|---|---|
| third-party items | 2026-07 | 69.2 | 67.2 to 71.1 (level 0.95) |
| third-party items | 2026-08 | 78.2 | 75.4 to 80.7 (level 0.95) |
| third-party items | 2026-09 | 75.4 | 72.3 to 78.2 (level 0.95) |
| third-party items | 2026-10 | 73.7 | 70.5 to 76.6 (level 0.95) |
| third-party items | 2026-11 | 73.8 | 63.5 to 82 (level 0.95) |
| own items | 2026-07 | 100 | 86.7 to 100 (level 0.95) |
| own items | 2026-08 | 100 | 99.4 to 100 (level 0.95) |
| own items | 2026-09 | 100 | 98.7 to 100 (level 0.95) |
| own items | 2026-10 | 100 | 97.1 to 100 (level 0.95) |
| own items | 2026-11 | 100 | 81.6 to 100 (level 0.95) |
These are measured values of the benchmark platform. The own items are the items that the benchmark platform itself wrote. The third-party items come from feeds of other publishers.
The chart shows that a part of the third-party items has no entity ID in each month. It does not show the effect of a change of the linker, because the data has no date of that change.
A stored column is not a count of the results of the linker. Count each result of the linker in its own metric.
failure modes
| problem | cause | correction |
|---|---|---|
| Links point to catalog entries that do not exist. | The code stores the vendor value of the model with no check. | Use the model value only when it is a catalog slug. |
| Items get a vendor whose name is a common word. | The name match finds a part of a word. | Match the catalog name as a whole word. |
| Each job goes to the dead-letter queue, and the log holds the same error trace again and again. | A model endpoint does not answer, and each job throws. | Give an empty result, and log the cause one time for each process. |
| A topic or a score outside its range goes into the database. | No whitelist and no clamp on the field. | Check each field with a whitelist or a clamp before the write. |
| A stored text holds a number that no source gives. | The model wrote the number. | Take each number from the database. The model writes only the words. |
check questions
The output of a tagger gives the relevance 3. The permitted range is 0 to 1. What does the clamp store?
- 3
- Null
- 1
- 0
answer
c. 1
A clamp moves a number into its permitted range. A value above the range becomes the top of the range.
An item has no source owner and no catalog name in its text. The model value is chrome, and the catalog has no slug chrome. What does the linker give?
- Unclassified, with the reason that the model value is not a catalog slug
- A link to a new catalog entry chrome
- A link to the nearest catalog slug
answer
a. Unclassified, with the reason that the model value is not a catalog slug
The linker uses a model value only when it is a catalog slug. Else the item is unclassified, with a reason, and counted.
The catalog has the vendor orbit. Why must the name match look for a whole word?
- A whole-word match is faster
- A match on a part of a word links an item such as an orbital launch report to the vendor
- The model writes only whole words
answer
b. A match on a part of a word links an item such as an orbital launch report to the vendor
A match on a part of a word links wrong items. Word boundaries keep them out.
Describe the soft failure paths of a model call when the endpoint does not answer, and when one field fails its check.
sample answer
When the endpoint does not answer, or the output is not JSON, the call gives an empty result. The cause goes to the log one time for each process, and the job does not throw. When one field fails its check, that field gets a null value, and the other fields stay usable.
Explain what a share of stored news items with an entity ID measures, and what it does not measure.
sample answer
It measures the presence of a value in the stored ID column. It does not measure the results of the linker. The data has no date of a change of the linker, so it does not show the effect of that change. Each result of the linker must have its own metric.
exercise
exercise Do this task on paper, or in your own lab with the second code example and synthetic data only. Do not send a request to a site or to a model.
The catalog has the slugs orbit and lumen, with the names "Orbit" and "Lumen". Give the linker result and its reason for each item:
- Item 1: source owner
orbit, text "Weekly notes", model valuelumen. - Item 2: no source owner, text "Lumen changes its check", model value none.
- Item 3: no source owner, text "Illuminated signs", model value
lumens. - Item 4: no source owner, text "A report on browsers", model value none.
sample answer
The answer uses the trust order of the linker.
Item 1 is linked to orbit by the source owner. The model value does not count, because the owner comes first. Item 2 is linked to lumen by the name match. Item 3 is unclassified: "Illuminated" holds no whole word "Lumen", and lumens is not a catalog slug. Item 4 is unclassified with the reason "no rule and no model value". The counts are 2 linked and 2 unclassified.
sources
- LLM05:2025 Improper Output Handling, OWASP Gen AI Security Project, standard, read 2026-11-20
- Benchmark platform, code (my test system): the JSON check of the tagger, the word boundaries, the linker order, and the soft failure paths, 2026-11 (not public).
- Benchmark platform, design (my test system): the trust order of the linker and the failure states of a model call, 2026-11 (not public).
- My model of the field checks for the output of a model, 2026-11 (not public).