About

Contract & Legal Document OCR — Clause Extraction, Party Identification, Structuring

Structuring contracts with Legal-BERT, CUAD and LayoutLMv3, and where to split API and local

Contract OCR doesn't end at extracting text. It becomes useful once you pull clauses, parties, deadlines and amounts out of that text. After reading this you can decide which models to build that pipeline with, and where to draw the line between "use the OpenAI API" and "run it ourselves".

Think of it as two steps

Step 1: OCR — contract image/PDF -> text
Step 2: Understanding — text -> structured data (clauses, parties, deadlines, amounts)

For step 1, general OCR (PaddleOCR, Marker and so on) is often enough. The hard part is step 2, understanding legal language.

Legal-BERT: BERT with extra legal reading

Plain BERT was trained on general text like Wikipedia. Legal-BERT adds 12GB of English legal text (legislation, case law, contracts) on top. So it handles terms like "indemnification" and "force majeure" better than plain BERT. It's an English model, though, so it won't work as is on Korean or Japanese contracts.

from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained("nlpaueb/legal-bert-base-uncased")
model = AutoModel.from_pretrained("nlpaueb/legal-bert-base-uncased")

CUAD: a contract understanding dataset

About 500 real contracts with over 13,000 expert annotations across 41 label types: termination, liability cap, IP ownership, non-compete, renewal terms and so on. Fine-tune Legal-BERT on CUAD and you get a model that answers "what's the termination clause?" or "what's the liability cap?".

from datasets import load_dataset
dataset = load_dataset("theatticusproject/cuad-qa")
print(dataset["train"][0])
# -> {'question': ..., 'context': 'contract text...', 'answers': {...}}

LayoutLMv3: it sees position too

Legal-BERT only reads text. LayoutLMv3 reads text, position and the page image together. In contracts, a lot of meaning comes from where something sits (signature blocks, date fields, clause numbers), so that difference matters.

Legal-BERT: "Is this text a termination clause?" (text only)
LayoutLMv3: "Is this text, in this position, a termination clause?" (text + position + image)

Using what already exists

OpenContracts is an MIT-licensed open-source document analysis platform. It ships an annotation UI for labeling PDFs, vector search and LLM-based structured extraction. Good for getting a feel for the screens and flows you need before building your own pipeline.

Azure Document Intelligence has a prebuilt-contract model that returns parties, jurisdictions, contract ID, title and more as JSON. Per the official docs it only supports English documents (checked 2026-10-19). For Korean or Japanese contracts, that road is closed.

Send it to an API or run it locally?

This section came from a question I got: "When you OCR and analyze contracts, how do you choose between the OpenAI API and running transformers yourself?" Compare on accuracy and you get the wrong answer, because the big APIs almost always win on accuracy. What actually decides it is three things: can the data leave, is the task structured, and how many documents.

And contract analysis isn't either-or. One pipeline mixes structured steps with open-ended reading.

Contract pipelineSmall or local modelLarge LLM APIOCRscan to textfitsField extractiondate, amount, partiesfitsClause classificationtermination, liabilityfitsFind risky clausesopen-ended readingfitsNot either-or: one pipeline, two kinds of steps
API (OpenAI, Azure, Claude) Self-hosted (transformers etc.)
Time to start Immediate Pick models, set up servers and GPUs
Accuracy on open-ended reading High (clause interpretation, risk summaries) Small models hit limits
Data leaving Goes out Stays in-house
Cost shape Per call, expensive at volume High upfront, cheaper as volume grows
Ops burden Almost none Model updates and incidents are yours
Offline No Yes

Signs it's time to move:

  • Contracts can't leave the company (security or legal requirements) -> self-host

  • Volume grew and per-call pricing hurts -> move just the structured steps local

  • What you extract is fixed (3 fields, 5 clause types) -> fine-tune a small model

  • You don't know what to extract yet, it's a PoC -> start with an API

Azure OpenAI is the middle option. Microsoft's docs say prompts and outputs aren't used to train models and aren't available to OpenAI. But abuse monitoring means flagged requests can be reviewed by humans, and turning that off requires a separate application. For contracts, read that part before deciding. Our own repo already wires Azure OpenAI in as a service object, and it's where we look first for internal documents.

A practical pipeline

Contract PDF
  | Marker/PaddleOCR (text extraction)
Text
  | Legal-BERT fine-tuned on CUAD (clause classification)
Structured data (example):

  - Parties: Company A, Company B

  - Term: 2026-01-01 to 2028-12-31

  - Termination: written notice 30 days in advance
  | LLM API only where needed ("which clauses here work against us?")

Some things I haven't measured yet: a cost comparison over the same 1,000 contracts, and Tesseract vs PaddleOCR vs cloud OCR accuracy on Korean and Japanese contracts. I'll write those up after measuring. The full comparison table is in OCR API vs self-hosted.

If you're adding contract or internal-document analysis and can't tell how much is safe to send to an API, you can ask negabaro (contact).

Key Concepts

1

Extract text with OCR; general tools like Marker/PaddleOCR are often enough

2

Classify clauses with Legal-BERT + CUAD, for English contracts

3

Use LayoutLMv3 to read position too: signature blocks, date fields, clause numbers

4

Structured steps on small or local models, only open-ended reading on an LLM API

5

Decide API vs local by whether data may leave, how structured it is, and volume

Use Cases

Automated contract review: extract termination, liability, non-compete clauses Contract comparison: auto-compare clause differences between contracts Structuring internal documents that cannot leave the company with local models
← PDF-Specialized OCR — Tools That Convert Complex Layouts, Tables, and Equations to Markdown Japanese-Specialized OCR — Manga, Vertical Text, Furigana, and Historical Documents →