Building an Open-Source Large Language Model for the Legal Domain: A Practical Guide
This guide explains how to build an open-source large language model for legal work. It covers why legal tasks need domain-specific models, the three-stage pipeline of continual pre-training, supervised fine-tuning, and reinforcement learning post-training, and how to choose a base model. It also describes evaluation methods, workflow integration patterns such as in-document review and agent-based orchestration, and an open-source iteration process driven by feedback from legal practitioners.
1. Background: Why Legal Scenarios Need Dedicated Large Models
General-purpose large models perform well on everyday question answering and text generation. Legal workflows, however, place requirements on a model that go well beyond general scenarios:
- Terminology precision: Legal terms have strictly defined boundaries. Confusing “offer” (要约) with “invitation to offer” (要约邀请), or the Chinese terms 定金 and 订金, which sound identical but carry different legal effects, can lead directly to errors in practice.
- Complete reasoning chains: Analyzing the validity of a contract clause often requires multi-step reasoning that combines statutes, judicial interpretations, and transaction context.
- Traceable outputs: Legal conclusions must trace back to specific clause text or legal provisions. Outputs that look plausible but are fabricated cannot be tolerated.
- Domain distribution shift: The language style and document structure of legal documents differ significantly from general corpora, and general models lose capability noticeably on this distribution.
Therefore, domain-specific legal models typically follow a three-stage paradigm: general base model, then domain continual pre-training, then task post-training. This article follows that paradigm and explains how to build an open-source model for real legal workflows.
2. Technical Route Overview
A typical workflow for building a legal domain model looks like this:
Generic base model (lightweight)│▼Domain continual pre-training- Laws and regulations, court judgments, contract template corpora│▼Supervised fine-tuning (SFT)- Contract review, clause extraction, and legal Q&A instruction data│▼Reinforcement learning post-training (RLHF / RLVR)- Reward signals based on rule verification│▼Domain evaluation and iteration
2.1 Base Model Selection
Choosing a base model requires balancing capability and cost. Lightweight base models, with roughly several hundred million to about 10 billion parameters, offer the following advantages:
- Low inference cost: Contract review often involves batch processing of long documents, so the cost per document directly affects whether a product is viable.
- Easy private deployment: Legal data is highly sensitive. Corporate legal teams and law firms commonly require on-premises deployment, and lightweight models can run on a single GPU or a small number of GPUs.
- High fine-tuning efficiency: When domain data is limited, lightweight models absorb domain knowledge more readily and avoid the “knowledge dilution” problem seen in large models.
Practical experience shows that for legal tasks with a high degree of structure, such as clause identification and risk point classification, a lightweight model with sufficient domain training can approach or even exceed the performance of a larger model without domain adaptation.
2.2 Domain Continual Pre-training
Continual pre-training is the key step for immersing a general model in the legal domain. Recommended corpus composition:
| Corpus type | Purpose | Recommended share |
|---|---|---|
| Laws, regulations, and judicial interpretations | Build a normative knowledge base | 20% |
| Court judgments | Learn judicial reasoning and argument structure | 35% |
| Contract templates and practical documents | Master the language patterns of contracts | 30% |
| Legal theory and commentaries | Provide background for reasoning | 15% |
Two points need attention. First, deduplicate and clean the data. Court judgments contain many templated passages that should be downsampled. Second, mix in a proportion of general-purpose data, typically 10% to 20%, to prevent catastrophic forgetting, which would degrade general capabilities.
2.3 Supervised Fine-Tuning: Designing Tasks Around Real Workflows
Fine-tuning data should be organized around real legal workflows rather than generic question-answer pairs. Core tasks include:
- Contract clause risk identification: Given a clause, identify unfavorable terms, missing terms, and ambiguous wording.
- Clause revision suggestion generation: Point out risks and also provide rewritten text that complies with legal norms.
- Evidence element analysis: Extract time, parties, actions, and causal relationships from evidence materials.
- Compliance planning: For a given business scenario, produce a compliance checklist and the corresponding legal basis.
For data construction, use a mixed strategy: anonymized real cases, expert annotation, and model-assisted augmentation. Licensed legal professionals should review key samples.
2.4 Reinforcement Learning Post-Training
Legal tasks suit reinforcement learning with verifiable rewards (RLVR) well:
- Clause extraction tasks: Answers can be matched exactly against annotations, so F1 can serve directly as the reward.
- Revision suggestion tasks: Process rewards can be built from rule checks, such as whether the cited legal provisions are valid and whether the revised clause is internally consistent.
- Format constraints: Require output in a fixed structure of risk level, original text citation, revision suggestion, and legal basis. Format compliance can serve as an auxiliary reward.
The reward model should be trained on preference annotations from legal professionals. It should penalize two types of errors in particular: fabricated legal provisions, and over-revision, where a risk-free clause is flagged as risky.
3. Evaluation System Design
Evaluating a legal model should not rely solely on general benchmarks. Build a multi-layer evaluation system:
- Academic benchmarks: Cover standardized tasks such as legal multiple-choice questions and case analysis, used for horizontal comparison.
- **Task-level
