Clause Risk Review
Does legal pretraining help a model find risky contract clauses?
Multi-label classification of commercial contract clauses into six risk types on CUAD v1, 510 contracts annotated by lawyers. A TextCNN baseline, BERT, Legal-BERT and Longformer train on the same per-contract splits, three runs each (Longformer two), and are scored per window and per contract on 76 held-out contracts, with every threshold tuned on validation data only.
- the problem
- Contracts are long (97% exceed BERT’s 512-token window) and each label appears in only 1.5–4.2% of windows, so a model that flags nothing would score above 95% accuracy.
- what I built
- A leakage-safe pipeline: splits per contract, identical windows for every model, per-label thresholds from validation, noisy-OR pooling up to the contract, and 90 tests that pin each of those decisions.
The question
BERT and Legal-BERT share an architecture, a parameter count (109,486,854), a context window and a chunk width; they differ only in pretraining corpus. So any gap between them measures domain adaptation. The prediction: the gain should concentrate in labels carried by legal vocabulary. A second question pairs BERT with Longformer, which reads 2,600 words at once against 300: does more context help?
Data
- CUAD v1 (The Atticus Project): 510 commercial contracts, clause spans annotated by lawyers
- Median contract 5,039 words, longest 47,733; 85% of windows carry no label
- Contracts split 358 / 76 / 76 for training, tuning and the final test; the test contracts were never seen in training or tuning
Results
The 76 test contracts, averaged over runs with different seeds. Contract scores pool each contract’s windows with noisy-OR. AP is average precision. *Longformer’s windows are nine times larger, so its window score is not comparable.
| Model | Contract F1 | Contract AP | Window F1 | Runs |
|---|---|---|---|---|
| TextCNN | 0.834 | 0.898 | 0.658 | 3 |
| BERT | 0.861 | 0.917 | 0.697 | 3 |
| Legal-BERT | 0.883 | 0.928 | 0.744 | 3 |
| Longformer | 0.831 | 0.886 | 0.743* | 2 |
What held, and what didn’t
- supportedLegal pretraining helps. Over three runs each, Legal-BERT beat BERT by 0.047 window F1 (0.744 against 0.697), its worst run beat BERT’s best, and it ranked real clauses better (window AP 0.805 against 0.750). At contract level the gap is smaller (0.883 against 0.861) and the runs overlap.
- supportedThe gain sits where the wording is legal: +0.107 window F1 on Termination for Convenience, +0.077 on Cap on Liability, and none on Insurance (−0.007), whose wording (“shall maintain insurance”) is ordinary English.
- not supportedReading more at once did not help. Longformer scored 0.831 contract F1, below every BERT and Legal-BERT run and level with the CNN; both its runs stopped improving by pass 7 of 10, so training was not cut short.
- measuredThe first comparison, one run per model, found no difference: one unusually low Legal-BERT run decided it, which is why every model was retrained with three seeds.
- measuredNoisy-OR pooling (0.846 F1) and keeping only the highest window (0.840) are within noise, so noisy-OR is not claimed as a win; it is kept because it counts all the evidence. Plain averaging scores 0.511.
Limitations
what these results do not show
- Trained on 358 contracts; the rarest clause types are the weakest.
- Longformer ran twice, on a free T4 GPU one window at a time; a larger GPU, or a long-context model pretrained on legal text, would be a fairer test of long context.
- CUAD is US commercial contracts; other kinds of contract are untested.
- The CNN’s attributions show it flags Cap on Liability on the word “liability” in insurance clauses, not on capping language.
Reproduce
sources last checked 2026-12-02
pip install -e ".[analysis]"
legal-risk-prepare
legal-risk-train-cnn --epochs 12
legal-risk-train-transformer
legal-risk-evaluate --run_dir outputs/cnn --split testThe pipeline
- Six labels: Cap on Liability, Non-Compete, License Grant, Audit Rights, Termination for Convenience, Insurance
- Splits assigned per contract and saved to disk, so overlapping windows never straddle train and test, and every model is scored on the same 76 contracts
- Windows counted in words, not tokens, so all models see identical boundaries and a score gap is a gap between models
- A yes/no cut-off per clause type, tuned on validation contracts, never on test
- Noisy-OR pooling from windows to contracts, chosen after measuring seven pooling rules and a learned aggregator
- An in-browser demo of the CNN that flags passages and the phrases behind each flag; nothing you paste leaves the page
- Every model trained with several random seeds, since one retrain moves F1 by up to about ±0.03
- 90 tests that run as plain scripts, including one showing accuracy would rate an all-negative model above 95%
How it's built
each layer feeds the one below
- CUAD v1: 510 contracts
- spans by category
- splits per contract, saved
- overlapping word windows
- TextCNN · BERT · Legal-BERT · Longformer
- one shared training loop
- thresholds from validation
- noisy-OR pooling to the contract
- window and document scores
Activity
- Dec 6Move the results page and demo links to nndl.nyxenn.xyz (#65)ff4643f
- Nov 15Merge pull request #64 from ManasDasri/cleanup/demo-parityf87deed
- Nov 15Make notebook 5's Gradio demo match the browser demo; remove unused deploy codee639b1a
- Nov 15Merge pull request #63 from ManasDasri/fix/demo-linke366f1f
- Nov 15Link the demo by file, so the link also works on the Hugging Face mirrora1b9a17
- Nov 15Merge pull request #62 from ManasDasri/fix/model-carde964f41
- Nov 15Model card: report the pooling actually used, and both threshold sets1f269ec
- Nov 15Merge pull request #61 from ManasDasri/fix/consistency0416952
- Nov 15Make Legal-BERT the default and bring the docs in line with the resultse103c67
- Nov 15Merge pull request #60 from ManasDasri/fix/demo-matches-evaluationa6e6edf
