skip to content

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 system

purpose

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

  1. Design a check for each field of the output of a model, with whitelists, clamps, and slug checks.
  2. Build a deterministic linker that uses a model value only when it is a catalog slug.
  3. Describe the soft failure paths of a model call.
  4. 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:

checkwhat it doesexample
whitelistKeeps a value only when it is in a fixed listTopic politics is not in the list, so the topic becomes other.
ClampMoves a number into its permitted rangeRelevance 3 becomes 1.
Slug checkKeeps a vendor value only when it is a catalog slugVendor 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:

  1. The source owner, when it is a catalog entry.
  2. A catalog name as a whole word in the item text.
  3. The model value, only when it is a catalog slug.
  4. 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 sidedetection 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

optiongaincostwhen it is correct
JSON format and a check in code for each fieldEach stored value is a permitted valueEach field must have a ruleEach output of the model that goes into the database
Store the model value with no checkLess codeWrong values and links to entries that do not existNever for stored data
Model value only when it is a catalog slugNo link to an entry that does not existItems with a new vendor stay unclassifiedEach link to a catalog entry
A null field in place of a failed answerThe other fields stay usableA null value must have a meaning downstreamOne field fails, and the others pass
An empty result and one log line in place of a throwThe job goes on, and the log stays readableThe failure shows only in the log and in the countsThe 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 model value counts only after the source owner and the whole-word match. Each item that no step links is counted with its reason.source: Benchmark platform, design: the trust order of the linker

The second diagram gives the failure states of one model call.

failure states of one model call

No state of a call throws. A bad output gives an empty result or a null field, and the cause goes to the log one time.source: Benchmark platform, design: the 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.

m33-x03-json-tagger.tstypescript

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
null

The 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.

m33-x04-linker.tstypescript

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 2

The 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

share of news items with an entity id, by month of creation2026-071: 69.2%2: 100%2026-081: 78.2%2: 100%2026-091: 75.4%2: 100%2026-101: 73.7%2: 100%2026-111: 73.8%2: 100%unit: %1. third-party items2. own items

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
share of news items with an entity id, by month of creation
seriesmonth of creation%interval
third-party items2026-0769.267.2 to 71.1 (level 0.95)
third-party items2026-0878.275.4 to 80.7 (level 0.95)
third-party items2026-0975.472.3 to 78.2 (level 0.95)
third-party items2026-1073.770.5 to 76.6 (level 0.95)
third-party items2026-1173.863.5 to 82 (level 0.95)
own items2026-0710086.7 to 100 (level 0.95)
own items2026-0810099.4 to 100 (level 0.95)
own items2026-0910098.7 to 100 (level 0.95)
own items2026-1010097.1 to 100 (level 0.95)
own items2026-1110081.6 to 100 (level 0.95)
The chart shows the stored ID column, not the output of the linker. The own-items class has 17 items in 2026-11. The 95% interval of each share is in the data table.

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

problemcausecorrection
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

  1. The output of a tagger gives the relevance 3. The permitted range is 0 to 1. What does the clamp store?

    1. 3
    2. Null
    3. 1
    4. 0
    answer

    c. 1

    A clamp moves a number into its permitted range. A value above the range becomes the top of the range.

  2. 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?

    1. Unclassified, with the reason that the model value is not a catalog slug
    2. A link to a new catalog entry chrome
    3. 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.

  3. The catalog has the vendor orbit. Why must the name match look for a whole word?

    1. A whole-word match is faster
    2. A match on a part of a word links an item such as an orbital launch report to the vendor
    3. 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.

  4. 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.

  5. 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 value lumen.
  • 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

  1. LLM05:2025 Improper Output Handling, OWASP Gen AI Security Project, standard, read 2026-11-20
  2. 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).
  3. Benchmark platform, design (my test system): the trust order of the linker and the failure states of a model call, 2026-11 (not public).
  4. My model of the field checks for the output of a model, 2026-11 (not public).