Someone brought a 30,000-row customer payment table and asked whether it could just be handed to a model to clean up. The problems in the table look varied: customer names appear both as abbreviations and full names, phone numbers appear with and without hyphens, the province column contains both “北京市” and “北京”, and the remarks column mixes in statuses like “已开票” and “走对公”.
What this table needs is not just “an AI cleaning tool”—the first step is to separate these categories of problems. Their criteria are completely different, and lumping them together and handing them to any single tool will cause problems.
There is only one criterion: can this error be written as an assertion
My criterion is narrow: if a cleaning rule can be written as an assertion of the form “given this input, the output is unique and verifiable”, it should not use a model.
“Phone numbers must be 11 digits” is an assertion—one line of regex in a script, run it ten thousand times and the result is the same. “Customer names should have the ‘有限公司’ suffix removed” is also an assertion, provided the suffix list is exhaustive and maintainable.
What cannot be written as an assertion is the other category: are “北京某某科技有限公司” and “某某科技” the same customer? This depends on how the business defines it, on which entity signed the contract, and on whether sales mixed up groups and subsidiaries when entering data. This judgment requires external world knowledge and business definitions; a script cannot express it, but a model can give a probabilistic answer.
Once the criterion is clear, the rest is an engineering problem. The trouble is that many people mistake the second category for the first and pile up rules, or mistake the first category for the second and repeatedly tune prompts.
The cost structures of the two technologies are not the same thing
The cost of scripts is upfront: writing rules takes time, writing tests takes more time, but after that each run is essentially free, behavior is fully reproducible, and problems can be traced to a specific rule ID.
The cost of models is back-loaded: getting started is nearly zero cost—write a prompt and it runs—but you pay for every row processed, and results drift with model version, temperature, and context order.
The order of magnitude differs greatly. A row of table data plus instructions comes to roughly a few hundred tokens in input and output combined; 30,000 rows is on the order of one to ten million tokens, which at public API unit prices comes to tens of yuan for a full run, and several times that with a stronger model. That number isn’t scary; the real problem is that this money gets spent repeatedly: a monthly reconciliation table runs once a month, twelve times a year; changing the prompt means rerunning, changing the model means rerunning, and when the business says “last time’s results don’t match”, that’s another rerun.
Latency is more direct. Batch scenarios can tolerate it, but if cleaning is embedded in a spreadsheet tool and the user wants to see results immediately after editing a column, per-row model calls simply cannot keep up; adding concurrency means rate limiting, retries, and an inevitable portion of failed rows.
There is another layer that is often overlooked: explainability. What finance and compliance ask is never “is the result correct” but “why was this row changed to this”. A script can answer “hit rule_017, removed the place name in parentheses”; a model can only answer “the model thinks so”. The latter is fine for internal exploration, but not for external reports.
Rule stacking and full model processing each have their own failure modes
First, the typical failure mode of the rules path. For customer name normalization, three rules cover 80% at first: remove “有限公司”, “有限责任公司”, “股份公司”; remove place names in parentheses; convert full-width to half-width. Then the long tail arrives: “某某(中国)有限公司” and “某某中国” each get cut by two different rules, producing two different results; traditional Chinese names aren’t handled; whether “某某集团” and “某某集团有限公司” are one entity or two cannot be determined by rules.
So you add a fourth rule, a tenth, a fortieth. After a certain point, the rules start conflicting with each other: fixing customer A breaks customer B, and you don’t have enough test cases to notice. The real cost of a rule system is not in writing it the first time, but in the fact that every change requires a full regression run, and the regression set itself needs human maintenance.
The model path fails differently. The first version often performs better than expected, because the model does recognize that “某某科技” and “北京某某科技有限公司” are the same company. The problem appears on the second run: the results are not exactly the same as the first, and no one can say which rows changed and why. When the business brings a row and asks “last month this was A, this month it’s B, which is right”, if the output contains no original text snippet and no basis for the judgment, that question cannot be answered.
A more insidious failure mode is treating the model as a rule engine: to stabilize output, piling twenty if-else-style constraints into the prompt, ending up with an intermediate product that is neither stable nor explainable. It is more expensive than a script and dumber than a model.
Default architecture: scripts do the first layer of validation, the model makes judgments, humans make the final confirmation
I now prefer a layered structure, with boundaries drawn by degree of determinism rather than by field.
flowchart TD
A[原始表格] --> B[硬校验层]
B -->|字段缺失 类型错误| R[拒绝并打标]
B --> C[字典归一层]
C -->|命中词典| D[输出标准值]
C -->|未命中| E[标记为待判定]
E --> F[模型判定层]
F --> G[带证据的候选结果]
G --> H[抽样人工复核]
D --> H
H --> I[写入结果表]The first layer is hard validation: types, enums, ranges, uniqueness, foreign key existence. This layer makes no guesses; it only judges legality, and anything illegal is flagged and sent back without entering the later stages.
The second layer is dictionary normalization: provinces/cities/districts, industry classifications, customer aliases, unit conversions. Dictionary-driven rules handle most dirty data, and every alias added is a deterministic improvement that can be tested and code-reviewed.
Only the third layer goes to the model, and it only handles the residue marked “pending judgment” by the second layer. This ratio is critical: if the first two layers handle 85%, the model faces not 30,000 rows but just over 4,000, cutting both cost and uncertainty by an order of magnitude. If the first two layers only handle 30%, then the problem is not cleaning but the upstream data entry process—you should fix the form, not swap the model.
The fourth layer is human review, sampled proportionally, focusing on the model-judged portion and newly appearing dirty types.
The value of this structure lies in “each layer’s failure mode is separate”, not merely in “using AI”. If the validation layer misjudges, look at the rules; if the model layer misjudges, look at the evidence; if neither is wrong but the business disagrees, that’s a definition problem, and you have to go back to the definition itself.
Four rules the model layer must establish
If I decide to use a model, I would first fix the output shape rather than tune the wording. Letting the model return free text is equivalent to pushing the parsing work downstream.
1 | { |
First, the output must be enums plus fields constrained by a schema. decision can only take preset values, and reason_code can only be preset reason codes. Free text has no value in cleaning scenarios, because downstream needs it for aggregation and statistics.
Second, every conclusion must carry evidence, i.e., the snippet from the original cell. A judgment without evidence cannot be reviewed, nor can it defend itself when the business challenges it.
Third, results must be idempotent. Use “original text + prompt version + model version” as the cache key, so the same input isn’t paid for twice and reruns are reproducible. This rule narrows the scope of “last month and this month differ” investigations to a minimum, because differences can only come from input changes or version changes.
Fourth, the model version must be pinned. Upgrading the model requires running the regression set, which is the historically human-confirmed judgments, including the ones judged wrong. Upgrading a model without a regression set is experimenting in production.
These four rules aren’t hard; what’s hard is that someone always thinks “let’s get it running first”. Cleaning has the characteristic that a single error gets amplified through downstream reports, and adding these four rules after it’s running costs far more than writing them from the start.
The Reverse Order: Use the Model to Discover Dirty Data Types First, Then Stabilize with Rules
The above describes the endpoint, but the starting point can be reversed.
If you have no idea where a table is dirty, writing rules first is inefficient because you are guessing. A more reasonable approach is to sample a few hundred rows, run them through the model, and manually review its output to get a “dirty type list”: which are format issues, which are entity normalization, which are field extraction from remarks, and which are actually inconsistent business definitions.
This list is the most underrated use of models: it is mainly meant to help you discover which rules need to be written, not to go straight into production. Once the list is out, solidify each item into scripts and dictionaries one by one, and each time you solidify one, take back a batch of rows from the model. The model gradually retreats to the long tail—to the corner cases that appear a few dozen times a year and are not worth writing rules for.
The endpoint is usually neither “all model” nor “all scripts,” but rules handling the bulk and the model covering the edges. This ratio stabilizes as the business stabilizes; once stable, the rules part barely changes, and the model’s call volume becomes small enough that cost is negligible.
When It Is Acceptable to Throw the Whole Table at the Model
There are reasonable cases too. The boundary is roughly when these conditions hold simultaneously: a one-off task, within a few thousand rows, results only for yourself or a small team, no downstream system dependencies, and the data structure is unclear beforehand.
For example, if you receive a market list from someone and want to quickly see how many duplicates there are and what the regional distribution looks like, using a model for this kind of exploratory analysis is completely reasonable—writing scripts would be a waste of time.
Conversely, if any of the following appears, you should extract the deterministic part: the table will be rerun periodically; it involves amounts, settlement, or external disclosure; the results will go into a database or be consumed by other systems; someone will ask “why was this row changed.”
There is another case that is easy to misjudge: the table is mainly long text, such as ticket descriptions, user feedback, or contract remarks. That is essentially a text extraction task, not table cleaning. Using a model is correct, but do not expect its output to be directly reconcilable—human confirmation or rule validation is still needed in between.
If someone new to the field asks me whether a table can be cleaned with AI, I would first ask three questions: How often is this table run? Who bears the cost if it is wrong? Can this error be written as an assertion? The three answers basically determine which part should be scripted and which part is left to the model.
Comments