cd /news/large-language-models/reading-a-cutting-list-from-a-photo-… · home › topics › large-language-models › article
[ARTICLE · art-177727] src=dev.to ↗ pub= topic=large-language-models verified=true sentiment=↑ positive

Reading a cutting list from a photo with an LLM, without trusting the model

A developer built a Cloudflare Pages Function (functions/api/list-ai.js) for SyntaxFlow's glass cutting optimiser that uses a vision LLM to read handwritten cutting lists from photos or PDFs while treating the model's output as untrusted candidates rather than final data. The endpoint enforces a strict JSON schema with unit and confidence enums, re-parses and type-checks output server-side, rejects truncated responses, converts units in code, drops implausible dimensions with a count, and flags every response as needing human review. The design also marks attachments with a random nonce and includes an injectionSeen field to detect prompt-injection attempts embedded in the sheet.

recommendation

Validate LLM-extracted numbers with deterministic range checks and cross-check parsed values against the model's own quoted source text before letting any row into a production list.

more in playbook: Verifying model output
by read6 min views5 publishedNov 28, 2026

A glazier gets a photo of a handwritten sheet: "V1 1200 x 800, 2 uds", a crossed-out line, a phone number. A vision model can read that. The trouble is what happens when it reads it wrong and the numbers go straight into a machine that cuts expensive glass. A 1200 mistaken for 1700 is not a typo, it is a pane in the bin.

This is how the endpoint behind the "read the list from a photo or PDF" option of SyntaxFlow is built (functions/api/list-ai.js, a Cloudflare Pages Function). The design assumption is in one sentence of its header: the model's word is not trusted.

The model never decides what goes into the list. It returns candidate rows. Then deterministic code validates them, flags the doubtful ones, and the page shows everything for review. Nothing enters the list until the person says so. Every response carries review: true, and the header says it plainly: "ALWAYS needs-review".

The system prompt asks for one row per line, in sheet order, with the text of the line as read, the sizes as numbers in the unit written, never converted, a quantity only if written (0 otherwise), and a confidence of high, medium or low. It forbids inventing rows, adding up or converting. Conversion belongs to code, where it can be tested.

The file itself is untrusted: a handwritten sheet can contain "ignore your instructions". The prompt marks the attachment with a random nonce, says it is data only, and the schema has an injectionSeen boolean so the model can report it saw such text.

The request asks the API for structured output with a JSON schema, and no tools:

export const OUTPUT_SCHEMA = {
  type: "object", additionalProperties: false,
  required: ["readable", "unit", "rows", "injectionSeen"],
  properties: {
    readable: { type: "boolean" },
    unit: { type: "string", enum: ["mm", "cm", "m", "in", "unknown"] },
    injectionSeen: { type: "boolean" },
    rows: { type: "array", items: {
      type: "object", additionalProperties: false,
      required: ["text", "a", "b", "qty", "ref", "confidence", "unit"],
      properties: {
        text: { type: "string" }, a: { type: "number" }, b: { type: "number" },
        qty: { type: "integer" }, ref: { type: "string" },
        confidence: { type: "string", enum: ["high", "medium", "low"] },
        unit: { type: "string", enum: ["mm", "cm", "m", "in", "same"] } } } } } };

Enums for units and confidence leave the model nothing to improvise, and "unknown" is allowed so it is never forced to guess a unit. A per-row unit exists because real sheets mix: "1,20 x 0,80" and "60x40" in one list.

But a schema constrains the shape, not the truth, and I do not rely on it: the server parses the output again by hand (JSON.parse, type checks, a cap on the number of rows) and answers "invalid" on any deviation. A truncated output (stop_reason === "max_tokens") is rejected rather than repaired.

Units are converted on the server from a table, and each size must land inside what the trade can plausibly cut:

const TO_MM = { mm: 1, cm: 10, m: 1000, in: 25.4 };
ranges: { glass: [10, 6500], panel: [10, 6500], linear: [10, 15000] },
qty: [1, 999]

const a = Math.round(r.a * rk * 10) / 10;
const okA = isFinite(a) && a >= lo && a <= hi;
if (!okA || !okB || (r.qty !== 0 && (r.qty < qlo || r.qty > qhi))){
  dropped++; continue;           // counted in `dropped`
}

A "12000 x 800" glass pane never reaches the list. It is dropped and counted; silently losing a row would be as bad as accepting a wrong one.

When the sheet gives no unit, the guess is explicit and flagged (guessedUnit): if every size is under 10 and at least one has decimals, it is metres, otherwise millimetres. That mirrors the rule of the same tool's paste reader.

My favourite check. The model returns both the parsed numbers and the text it claims to have read. If the model reads "1200 x 800" but outputs b: 600, the two disagree, and a disagreement is a hallucination detector that costs nothing.

function figures(text){            // every number in the line, both decimal conventions
  const out = new Set();
  for (const m of String(text).matchAll(/\d+(?:[.,]\d+)*/g)){
    const t = m[0], digits = t.replace(/[.,]/g, "");
    out.add(String(+digits));                 // "1.250,5" → 12505 ...
    const lastSep = Math.max(t.lastIndexOf("."), t.lastIndexOf(","));
    if (lastSep > 0){                          // ... and "1250.5"
      out.add(String(+(t.slice(0, lastSep).replace(/[.,]/g, "") + "." + t.slice(lastSep + 1))));
    }
  }
  return out;
}
if (!same(figs, r.a) || !same(figs, r.b)) why.push("no-cuadra");   // does not add up
if (r.qty === 0) why.push("cantidad");                            // quantity not written
if (r.confidence === "low") why.push("legible");

Both readings of the separator are collected because 1,25 may be 1.25 m or 125 cm. I ran the real function on five invented rows:

Row as returned Result
V1 1200 x 800 2 uds , a=1200, b=800, qty=2 accepted, no flag
V2 1200 x 800 , but b=600 kept, flagged "does not add up"
V3 60x40 x3 , unit cm converted to 600 × 400 mm, no flag
V4 12000 x 800 dropped, counted
V5 1,25 x 0,8 , qty 0, unit m 1250 × 800 mm, flagged "quantity not written"

A row whose numbers do not appear in its own transcription is the exact signature of a model that "improved" the sheet. The same goes for a quantity above 1 that is not in the line. A flagged row is not discarded: the page asks the person to compare it with the photo.

The text and reference fields end up in a web page and possibly in a link. They are stripped of control characters, angle brackets, braces, backticks, URLs, and cut to 90 and 40 characters. A model that was fooled by an embedded instruction still cannot smuggle markup or a link through the reference field.

The cheap checks run first and refuse without spending: the file's real type from its magic bytes (not the declared one), size, pixel dimensions read from its own header, number of PDF pages; then a monthly quota and a euro budget reserved before the call in a single conditional insert. The file is not stored anywhere: it is read from the request, sent to the model provider and dropped. Anonymous visitors are counted by a hash with the month inside it, so one cannot be followed from month to month.

The tests use a fake provider, so they prove the checks above work, not how often the model reads a messy sheet correctly. In production it has read only a handful of real sheets so far (seven at the time of writing), far too few for an accuracy figure, and I will not invent one. The design is meant so that the answer to "how accurate is the model?" matters less: a wrong row is either dropped, flagged, or caught by a person looking at the photo next to the table.

The optimiser it feeds is at syntaxflow.io/glass/cutting-optimiser/.

I'm the author of SyntaxFlow, the set of workshop calculators this article is about.

Written with the help of an AI assistant; the code, the figures and the claims were checked against the repository before publishing.

── more in #large-language-models 4 stories · sorted by recency
── more on @syntaxflow 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/reading-a-cutting-li…] indexed:0 read:6min 2026-11-28 · —