The LeanDojo v2 external API is a lightweight FastAPI server that bridges Lean 4 editor extensions with large language models. When a user invokes a tactic suggestion in the Lean editor via LeanCopilot, the extension sends the current proof state to this server over HTTP, receives ranked tactic candidates, and inserts the best suggestion into the editor. The server is entirely separate from the in-process Python prover stack — it is designed to be deployed on a GPU node and queried remotely.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/lean-dojo/LeanDojo-v2/llms.txt
Use this file to discover all available pages before exploring further.
Setting Up the Server
Create a dedicated conda environment
Isolate the server’s dependencies from your main LeanDojo environment to avoid version conflicts with
vllm and other heavy packages.Install Python dependencies
Install PyTorch first (select the wheel that matches your CUDA version), then the remaining packages:
vllm requires a CUDA-capable GPU. Omit it if you are running on CPU or will only use the HuggingFace Inference API backend.Available Endpoints
The server (server.py) exposes two POST routes:
POST /generate
Accepts a tactic generation request and returns ranked tactic candidates. Optionally integrates a LeanProgress scorer to attach reward signals to each candidate.
Request body:
| Field | Type | Description |
|---|---|---|
name | string | Model bundle key registered in server.py (e.g., "deepseek"). |
input | string | The current Lean proof state (goal string). |
prefix | string | null | Optional tactic prefix to constrain generation. |
use_reward | boolean | If true, runs each candidate through the LeanProgress scorer and returns steps_remaining and reward fields. Requires LEANPROGRESS_MODEL to be set. |
Generation object in outputs has:
| Field | Type | Description |
|---|---|---|
output | string | The generated tactic text. |
score | float | Composite score (equals reward when use_reward=true, otherwise the model log-probability score). |
model_score | float | null | Raw log-probability score from the generation model. Only populated when use_reward=true. |
steps_remaining | float | null | Predicted steps remaining from LeanProgress (only populated when use_reward=true). |
reward | float | null | Negative of steps_remaining — higher is better (only when use_reward=true). |
POST /encode
Returns a dense embedding vector for the given input string. Only available for model bundles whose generator implements the Encoder mixin.
Request body:
Model Bundles
The server registers models asModelBundle objects that pair a Generator with an optional LeanProgressScorer:
server.py wraps deepseek-ai/DeepSeek-Prover-V2-671B:novita via the HuggingFace Inference router:
models dictionary with new keys before starting the server.
HFTacticGenerator — HuggingFace Inference API Backend
The HFTacticGenerator class (external_models/hf_runner.py) calls the HuggingFace Inference Router (https://router.huggingface.co/v1/chat/completions) and requires the HF_TOKEN environment variable:
HFTacticGenerator also deduplicates across calls: once a (input, tactic) pair has been returned, it skips that pair and tries the next candidate, reducing redundant suggestions during beam search.
ExternalProver and ExternalAgent
The Python-side ExternalAgent / ExternalProver classes use HFTacticGenerator directly — they do not go through the HTTP server. They call the HuggingFace Inference API in-process:
The HTTP server is for in-editor use via LeanCopilot’s Lean 4 frontend. The Python
ExternalAgent and ExternalProver classes bypass the server entirely and call the HuggingFace Inference API directly from Python. You only need to run the server if you are using the Lean 4 editor extension.Lean 4 Editor Frontend
The Lean 4 side of LeanCopilot communicates with the running server using theExternalAPI.lean module, which provides a #suggest_tactics command and related tactics. After the server is running:
- Open a Lean 4 file that imports
LeanCopilot. - Invoke the suggestion tactic — LeanCopilot sends the goal over HTTP and displays the returned candidates inline.
Testing the Server
After the server is up, run the integration tests inLeanCopilotTests/ModelAPIs.lean to verify the connection: