A shop’s estimate reaches us as a PDF. We pull the text out, a model turns the text into structured data, and the report is built from that. During a review we ran the text extraction on a real estimate and looked for the things we knew were in it. The estimate number was there. So were the brands and the totals. The VIN was not.
It was there, just not in one piece. The PDF lays the VIN out across what looks like a single line, but the extracted text had a space in the middle of it:
2003 Honda Accord 1HGCM8263 3A004352 Mileage 81,204A VIN is seventeen characters. That is nine, a space, and eight. A person reads straight through it. A model usually does too, but "usually" is the problem: it might return the two halves joined, or return them with the space, or decide the first half is the VIN and the second is a part number.
What we did not do
We did not solve this by telling the model to be careful. We do tell it, in one line of the prompt, because it helps. But a prompt is a request, and a VIN is a key: it is how a shop knows which car a job belongs to, across every visit. A key should not depend on a request being honored.
A VIN can prove itself
Since 1981, the ninth character of a North American VIN has been a check digit. Each of the other sixteen characters is mapped to a number, multiplied by a weight for its position, and summed. The remainder after dividing by eleven is the ninth character, with ten written as X.
const WEIGHTS = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2];
export function hasValidCheckDigit(vin: string): boolean {
if (!isVinShaped(vin)) return false;
let sum = 0;
for (let i = 0; i < 17; i++) {
const c = vin[i];
const value = c >= '0' && c <= '9' ? Number(c) : TRANSLITERATION[c];
if (value === undefined) return false;
sum += value * WEIGHTS[i];
}
const remainder = sum % 11;
return vin[8] === (remainder === 10 ? 'X' : String(remainder));
}This turns "does this look like a VIN" into "is this a VIN." A random seventeen-character string passes about one time in eleven, and a real VIN with one character misread usually fails.
Recovering it from the source
With a way to recognize a real VIN, the rest is a search. We split the extracted text on whitespace, and consider every single token and every adjacent pair of tokens that together make seventeen characters from the VIN alphabet, which has no I, O or Q. Candidates that pass the check digit win.
Then the rule for what we store:
- If the model’s VIN passes the check digit, keep it.
- Otherwise, if the source text contains one that does, use that instead.
- Otherwise keep the model’s answer, cleaned up, and let a person see it.
So the model’s output is the first candidate, not the final word. If it returns the VIN with the space, we join it. If it returns half, we find the other half. If it gets one character wrong, the check digit fails and the text is searched.
Where it stops
The check digit is a North American requirement. Many vehicles built for other markets do not carry one, so a VIN that fails it is not necessarily wrong. That is why failing the check does not reject anything. It only decides which candidate we prefer when there is a choice, and the cleaned-up original is always the fallback.
The rule we wrote down afterwards: when a value has a way to verify itself, verify it in code. Save the model for the parts of the document that do not.