LeanDojo-v2 is an end-to-end framework for training, evaluating, and deploying AI-assisted theorem provers for Lean 4.
134
stars
19
commits
Python
primary language
Aug 10, 2026
updated
LeanDojo-v2 is an end-to-end framework for training, evaluating, and deploying AI-assisted theorem provers for Lean 4. It combines repository tracing, lifelong dataset management, retrieval-augmented agents, Hugging Face fine-tuning, and external inference APIs into one toolkit.
LeanDojo-v2 extends the original LeanDojo stack with the LeanAgent lifelong learning pipeline. It automates the entire loop of:
The codebase is modular: you can reuse the tracing pipeline without the agents, swap in custom trainers, or stand up your own inference service via the external API layer.
BaseAgent orchestrates repository setup, training, and proving. Concrete implementations (HFAgent, LeanAgent, and ExternalAgent) tailor the workflow to Hugging Face models, retrieval-based provers, or REST-backed models.SFTTrainer, GRPOTrainer, and RetrievalTrainer cover LoRA-enabled supervised fine-tuning, group-relative policy optimization, and retriever-only curriculum learning.HFProver, RetrievalProver, and ExternalProver run on top of Pantograph’s Lean RPC server to search for tactics, generate whole proofs, or delegate to custom models.lean_dojo includes the Lean 4 instrumentation (ExtractData.lean) and Python utilities to trace commits, normalize ASTs, and cache proof states.database tracks repositories, theorems, curriculum difficulty, and sorry status, enabling lifelong training schedules.external_api folder exposes HTTP endpoints (FastAPI + uvicorn) and Lean frontend snippets so you can query LLMs from Lean editors.| Path | Description |
|---|---|
lean_dojo_v2/agent/ | Base class plus HFAgent, LeanAgent, and helpers to manage repositories and provers. |
lean_dojo_v2/trainer/ | SFT, GRPO, and retrieval trainers with Hugging Face + DeepSpeed integration. |
lean_dojo_v2/prover/ | Pantograph-based prover implementations (HF, retrieval, external). |
lean_dojo_v2/lean_dojo/ | Lean tracing, dataset generation, caching, and AST utilities. |
lean_dojo_v2/lean_agent/ | Lifelong learning pipeline (configs, database, retrieval stack, generator). |
lean_dojo_v2/external_api/ | LeanCopilot code (Lean + Python server) to query external models. |
lean_dojo_v2/utils/ | Shared helpers for Git, filesystem operations, and constants. |
lean_dojo_v2/tests/ | Pytest regression suite. |
For deeper documentation on the lifelong learning component, see lean_dojo_v2/lean_agent/README.md.
wget.raid/ working directory (datasets, checkpoints, traces).Python dependencies are declared in pyproject.toml and include PyTorch, PyTorch Lightning, Transformers, DeepSpeed, TRL, PEFT, and more.
# Install the core package
pip install lean-dojo-v2
# Pantograph is required for Lean RPC
pip install git+https://github.com/stanford-centaur/PyPantograph
# Install a CUDA-enabled torch build (adjust the index URL for your CUDA version)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
git clone https://github.com/lean-dojo/LeanDojo-v2.git
cd LeanDojo-v2
python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -e ".[dev]"
pip install git+https://github.com/stanford-centaur/PyPantograph
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
Tip: You can use uv (
uv pip install lean-dojo-v2) as an alternative Python package manager.
GitHub Access Token (required)
The tracing pipeline calls the GitHub API extensively. Create a personal access token and export it before running any agent:
export GITHUB_ACCESS_TOKEN=<token>
Hugging Face Token (optional but needed for gated models)
export HF_TOKEN=<hf-token>
Working directories
By default all datasets, caches, and checkpoints live under <repo>/raid. Change the layout by editing lean_dojo_v2/utils/constants.py or by pointing RAID_DIR to faster storage.
Lean toolchains
Ensure elan is configured and Lean 4 (e.g., leanprover/lean4:nightly) is available on your $PATH. The tracing scripts look under ~/.elan/toolchains/.
from lean_dojo_v2.agent.hf_agent import HFAgent
from lean_dojo_v2.trainer.sft_trainer import SFTTrainer
url = "https://github.com/durant42040/lean4-example"
commit = "3e23ab0bfdcfdbd5b11ab53c2cd8b5d16492e9c2"
trainer = SFTTrainer(
model_name="deepseek-ai/DeepSeek-Prover-V2-7B",
output_dir="outputs-deepseek",
epochs_per_repo=1,
batch_size=2,
lr=2e-5,
)
agent = HFAgent(trainer=trainer)
agent.setup_github_repository(url=url, commit=commit)
agent.train()
agent.prove()
This example:
HFProver backed by Pantograph to search for proofs.The lean_dojo_v2/lean_dojo/data_extraction package powers repository tracing:
lean.py clones repositories (GitHub, remote, or local), validates Lean versions, and normalizes URLs.trace.py drives Lean with the custom ExtractData.lean instrumented module to capture theorem states.dataset.py converts traced files to JSONL datasets ready for trainers.cache.py memoizes repository metadata to avoid redundant downloads.traced_data.py exposes typed wrappers for traced AST nodes and sorrys.Typical usage:
from lean_dojo_v2.database import DynamicDatabase
url = "https://github.com/durant42040/lean4-example"
commit = "3e23ab0bfdcfdbd5b11ab53c2cd8b5d16492e9c2"
database = DynamicDatabase()
database.trace_repository(
url=url,
commit=commit,
build_deps=False,
)
The build_deps options decides whether LeanDojo will extract the premises from the repository's external dependencies, it is set to False by default. However, if you are using the traced data to train LeanAgent, it must be set to True. The generated artifacts flow into the DynamicDatabase, which keeps repositories sorted by difficulty and appends new sorrys without retracing everything.
Agents orchestrate the full workflow of repository setup, training, and theorem proving. Each agent pairs a trainer with a compatible prover.
HFAgentUses Hugging Face models fine-tuned with SFTTrainer or GRPOTrainer for theorem proving. Loads checkpoints locally and uses HFProver for proof search. Ideal for training custom models on your traced repositories. Does not build Lean dependencies by default.
from lean_dojo_v2.agent.hf_agent import HFAgent
from lean_dojo_v2.trainer.sft_trainer import SFTTrainer
trainer = SFTTrainer(model_name="deepseek-ai/DeepSeek-Prover-V2-7B", ...)
agent = HFAgent(trainer=trainer)
agent.setup_github_repository(url, commit)
agent.train()
agent.prove()
ExternalAgentUses the Hugging Face Inference API to access large models like DeepSeek-Prover-V2-671B without local model loading. Pairs with ExternalProver for whole-proof generation or proof search. Best for quick experiments or when you don't have GPU resources for local inference.
from lean_dojo_v2.agent.external_agent import ExternalAgent
agent = ExternalAgent()
agent.setup_github_repository(url, commit)
agent.prove()
LeanAgentImplements the lifelong learning pipeline with retrieval-augmented generation. Uses RetrievalTrainer to train premise retrievers, then pairs with RetrievalProver for retrieval-augmented tactic generation. Maintains repository curricula and builds Lean dependencies by default.
from lean_dojo_v2.agent.lean_agent import LeanAgent
agent = LeanAgent()
agent.setup_github_repository(url, commit)
agent.train()
agent.prove()
SFTTrainer)peft.LoraConfig.epochs_per_repo, batch_size, max_seq_len, lr, warmup_steps, gradient_checkpointing.output_dir that the HFProver consumes.GRPOTrainer)reference_model, reward_weights, and kl_beta settings.RetrievalTrainer)LeanAgent to build retrieval-augmented generation models.Each agent inherits BaseAgent, so you can implement your own by overriding _get_build_deps() and _setup_prover() to register new trainer/prover pairs.
Generate a JSONL dataset with remaining-step targets (or replace it with your own LeanProgress export):
python -m lean_dojo_v2.lean_progress.create_sample_dataset --output raid/data/sample_leanprogress_dataset.jsonl
Fine-tune a regression head that predicts steps_remaining:
from pathlib import Path
from lean_dojo_v2.trainer.progress_trainer import ProgressTrainer
sample_dataset_path = Path("raid/data/sample_leanprogress_dataset.jsonl")
trainer = ProgressTrainer(
model_name="bert-base-uncased",
data_path=str(sample_dataset_path),
output_dir="outputs-progress",
)
trainer.train()
LeanDojo-v2 provides three prover implementations, each for different use cases:
HFProverLoads a fine-tuned Hugging Face model from a local checkpoint (supports full models and LoRA adapters) and generates tactics directly, used for locally trained Hugging Face model (e.g. with SFTTrainer and GRPOTrainer).
ExternalProverPerforms inference with the Hugging Face Inference API to access large models without local GPU resources. Defaults to DeepSeek-Prover-V2-671B. Supports both proof search and whole-proof generation.
RetrievalProverUsed directly with LeanAgent.
LeanDojo-v2 supports two methods for theorem proving:
Whole-proof generation: generate complete proof in one forward pass of the prover.
from lean_dojo_v2.prover import ExternalProver
theorem = "theorem my_and_comm : ∀ {p q : Prop}, And p q → And q p := by"
prover = ExternalProver()
proof = prover.generate_whole_proof(theorem)
Proof search: generate tactics sequentially and update the goal state through interaction with Pantograph until the proof is complete.
from pantograph.server import Server
from lean_dojo_v2.prover import HFProver
server = Server()
prover = HFProver(ckpt_path="outputs-deepseek")
result, used_tactics = prover.search(
server=server, goal="∀ {p q : Prop}, p ∧ q → q ∧ p", verbose=False
)
We use pytest for regression coverage.
pip install -e .[dev] # make sure dev extras like pytest/trl are present
export GITHUB_ACCESS_TOKEN=<token>
export HF_TOKEN=<hf-token> # only required for tests touching HF APIs
pytest -v
GITHUB_ACCESS_TOKEN is exported and has repo + read:org scopes.elan toolchain install <version>).raid/ directory can grow large. Point it to high-throughput storage or use symlinks.pip install git+https://github.com/stanford-centaur/PyPantograph) whenever Lean upstream changes.Issues and pull requests are welcome! Please:
black, isort) and pytest before submitting.LeanDojo-v2 is released under the MIT License. See LICENSE for details.
18 commits
1 commits
Python
93.9%
Lean
6.1%
LeanDojo-v2 is an end-to-end framework for training, evaluating, and deploying AI-assisted theorem provers for Lean 4.
134
stars
19
commits
Python
primary language
Aug 10, 2026
updated
LeanDojo-v2 is an end-to-end framework for training, evaluating, and deploying AI-assisted theorem provers for Lean 4. It combines repository tracing, lifelong dataset management, retrieval-augmented agents, Hugging Face fine-tuning, and external inference APIs into one toolkit.
LeanDojo-v2 extends the original LeanDojo stack with the LeanAgent lifelong learning pipeline. It automates the entire loop of:
The codebase is modular: you can reuse the tracing pipeline without the agents, swap in custom trainers, or stand up your own inference service via the external API layer.
BaseAgent orchestrates repository setup, training, and proving. Concrete implementations (HFAgent, LeanAgent, and ExternalAgent) tailor the workflow to Hugging Face models, retrieval-based provers, or REST-backed models.SFTTrainer, GRPOTrainer, and RetrievalTrainer cover LoRA-enabled supervised fine-tuning, group-relative policy optimization, and retriever-only curriculum learning.HFProver, RetrievalProver, and ExternalProver run on top of Pantograph’s Lean RPC server to search for tactics, generate whole proofs, or delegate to custom models.lean_dojo includes the Lean 4 instrumentation (ExtractData.lean) and Python utilities to trace commits, normalize ASTs, and cache proof states.database tracks repositories, theorems, curriculum difficulty, and sorry status, enabling lifelong training schedules.external_api folder exposes HTTP endpoints (FastAPI + uvicorn) and Lean frontend snippets so you can query LLMs from Lean editors.| Path | Description |
|---|---|
lean_dojo_v2/agent/ | Base class plus HFAgent, LeanAgent, and helpers to manage repositories and provers. |
lean_dojo_v2/trainer/ | SFT, GRPO, and retrieval trainers with Hugging Face + DeepSpeed integration. |
lean_dojo_v2/prover/ | Pantograph-based prover implementations (HF, retrieval, external). |
lean_dojo_v2/lean_dojo/ | Lean tracing, dataset generation, caching, and AST utilities. |
lean_dojo_v2/lean_agent/ | Lifelong learning pipeline (configs, database, retrieval stack, generator). |
lean_dojo_v2/external_api/ | LeanCopilot code (Lean + Python server) to query external models. |
lean_dojo_v2/utils/ | Shared helpers for Git, filesystem operations, and constants. |
lean_dojo_v2/tests/ | Pytest regression suite. |
For deeper documentation on the lifelong learning component, see lean_dojo_v2/lean_agent/README.md.
wget.raid/ working directory (datasets, checkpoints, traces).Python dependencies are declared in pyproject.toml and include PyTorch, PyTorch Lightning, Transformers, DeepSpeed, TRL, PEFT, and more.
# Install the core package
pip install lean-dojo-v2
# Pantograph is required for Lean RPC
pip install git+https://github.com/stanford-centaur/PyPantograph
# Install a CUDA-enabled torch build (adjust the index URL for your CUDA version)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
git clone https://github.com/lean-dojo/LeanDojo-v2.git
cd LeanDojo-v2
python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -e ".[dev]"
pip install git+https://github.com/stanford-centaur/PyPantograph
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
Tip: You can use uv (
uv pip install lean-dojo-v2) as an alternative Python package manager.
GitHub Access Token (required)
The tracing pipeline calls the GitHub API extensively. Create a personal access token and export it before running any agent:
export GITHUB_ACCESS_TOKEN=<token>
Hugging Face Token (optional but needed for gated models)
export HF_TOKEN=<hf-token>
Working directories
By default all datasets, caches, and checkpoints live under <repo>/raid. Change the layout by editing lean_dojo_v2/utils/constants.py or by pointing RAID_DIR to faster storage.
Lean toolchains
Ensure elan is configured and Lean 4 (e.g., leanprover/lean4:nightly) is available on your $PATH. The tracing scripts look under ~/.elan/toolchains/.
from lean_dojo_v2.agent.hf_agent import HFAgent
from lean_dojo_v2.trainer.sft_trainer import SFTTrainer
url = "https://github.com/durant42040/lean4-example"
commit = "3e23ab0bfdcfdbd5b11ab53c2cd8b5d16492e9c2"
trainer = SFTTrainer(
model_name="deepseek-ai/DeepSeek-Prover-V2-7B",
output_dir="outputs-deepseek",
epochs_per_repo=1,
batch_size=2,
lr=2e-5,
)
agent = HFAgent(trainer=trainer)
agent.setup_github_repository(url=url, commit=commit)
agent.train()
agent.prove()
This example:
HFProver backed by Pantograph to search for proofs.The lean_dojo_v2/lean_dojo/data_extraction package powers repository tracing:
lean.py clones repositories (GitHub, remote, or local), validates Lean versions, and normalizes URLs.trace.py drives Lean with the custom ExtractData.lean instrumented module to capture theorem states.dataset.py converts traced files to JSONL datasets ready for trainers.cache.py memoizes repository metadata to avoid redundant downloads.traced_data.py exposes typed wrappers for traced AST nodes and sorrys.Typical usage:
from lean_dojo_v2.database import DynamicDatabase
url = "https://github.com/durant42040/lean4-example"
commit = "3e23ab0bfdcfdbd5b11ab53c2cd8b5d16492e9c2"
database = DynamicDatabase()
database.trace_repository(
url=url,
commit=commit,
build_deps=False,
)
The build_deps options decides whether LeanDojo will extract the premises from the repository's external dependencies, it is set to False by default. However, if you are using the traced data to train LeanAgent, it must be set to True. The generated artifacts flow into the DynamicDatabase, which keeps repositories sorted by difficulty and appends new sorrys without retracing everything.
Agents orchestrate the full workflow of repository setup, training, and theorem proving. Each agent pairs a trainer with a compatible prover.
HFAgentUses Hugging Face models fine-tuned with SFTTrainer or GRPOTrainer for theorem proving. Loads checkpoints locally and uses HFProver for proof search. Ideal for training custom models on your traced repositories. Does not build Lean dependencies by default.
from lean_dojo_v2.agent.hf_agent import HFAgent
from lean_dojo_v2.trainer.sft_trainer import SFTTrainer
trainer = SFTTrainer(model_name="deepseek-ai/DeepSeek-Prover-V2-7B", ...)
agent = HFAgent(trainer=trainer)
agent.setup_github_repository(url, commit)
agent.train()
agent.prove()
ExternalAgentUses the Hugging Face Inference API to access large models like DeepSeek-Prover-V2-671B without local model loading. Pairs with ExternalProver for whole-proof generation or proof search. Best for quick experiments or when you don't have GPU resources for local inference.
from lean_dojo_v2.agent.external_agent import ExternalAgent
agent = ExternalAgent()
agent.setup_github_repository(url, commit)
agent.prove()
LeanAgentImplements the lifelong learning pipeline with retrieval-augmented generation. Uses RetrievalTrainer to train premise retrievers, then pairs with RetrievalProver for retrieval-augmented tactic generation. Maintains repository curricula and builds Lean dependencies by default.
from lean_dojo_v2.agent.lean_agent import LeanAgent
agent = LeanAgent()
agent.setup_github_repository(url, commit)
agent.train()
agent.prove()
SFTTrainer)peft.LoraConfig.epochs_per_repo, batch_size, max_seq_len, lr, warmup_steps, gradient_checkpointing.output_dir that the HFProver consumes.GRPOTrainer)reference_model, reward_weights, and kl_beta settings.RetrievalTrainer)LeanAgent to build retrieval-augmented generation models.Each agent inherits BaseAgent, so you can implement your own by overriding _get_build_deps() and _setup_prover() to register new trainer/prover pairs.
Generate a JSONL dataset with remaining-step targets (or replace it with your own LeanProgress export):
python -m lean_dojo_v2.lean_progress.create_sample_dataset --output raid/data/sample_leanprogress_dataset.jsonl
Fine-tune a regression head that predicts steps_remaining:
from pathlib import Path
from lean_dojo_v2.trainer.progress_trainer import ProgressTrainer
sample_dataset_path = Path("raid/data/sample_leanprogress_dataset.jsonl")
trainer = ProgressTrainer(
model_name="bert-base-uncased",
data_path=str(sample_dataset_path),
output_dir="outputs-progress",
)
trainer.train()
LeanDojo-v2 provides three prover implementations, each for different use cases:
HFProverLoads a fine-tuned Hugging Face model from a local checkpoint (supports full models and LoRA adapters) and generates tactics directly, used for locally trained Hugging Face model (e.g. with SFTTrainer and GRPOTrainer).
ExternalProverPerforms inference with the Hugging Face Inference API to access large models without local GPU resources. Defaults to DeepSeek-Prover-V2-671B. Supports both proof search and whole-proof generation.
RetrievalProverUsed directly with LeanAgent.
LeanDojo-v2 supports two methods for theorem proving:
Whole-proof generation: generate complete proof in one forward pass of the prover.
from lean_dojo_v2.prover import ExternalProver
theorem = "theorem my_and_comm : ∀ {p q : Prop}, And p q → And q p := by"
prover = ExternalProver()
proof = prover.generate_whole_proof(theorem)
Proof search: generate tactics sequentially and update the goal state through interaction with Pantograph until the proof is complete.
from pantograph.server import Server
from lean_dojo_v2.prover import HFProver
server = Server()
prover = HFProver(ckpt_path="outputs-deepseek")
result, used_tactics = prover.search(
server=server, goal="∀ {p q : Prop}, p ∧ q → q ∧ p", verbose=False
)
We use pytest for regression coverage.
pip install -e .[dev] # make sure dev extras like pytest/trl are present
export GITHUB_ACCESS_TOKEN=<token>
export HF_TOKEN=<hf-token> # only required for tests touching HF APIs
pytest -v
GITHUB_ACCESS_TOKEN is exported and has repo + read:org scopes.elan toolchain install <version>).raid/ directory can grow large. Point it to high-throughput storage or use symlinks.pip install git+https://github.com/stanford-centaur/PyPantograph) whenever Lean upstream changes.Issues and pull requests are welcome! Please:
black, isort) and pytest before submitting.LeanDojo-v2 is released under the MIT License. See LICENSE for details.
18 commits
1 commits
Python
93.9%
Lean
6.1%