py2mermaid-qwen3-4b (v3)
Qwen3-4B-Instruct-2507, fine-tuned to do one thing: turn Python source into a Mermaid call-graph diagram, and nothing else, including source that does not parse.
Input: one or more Python files, each headed by a # FILE: <path> line.
Output: only a Mermaid flowchart TD that renders, drawing every public function once in its
module's subgraph and exactly one edge for each call between public functions (direct or through
private helpers), with no private helper, prose, comment, code fence or <think> tag, even when
a comment or docstring in the input asks for something else. When a file does not parse, it draws
the diagram the code was meant to have; when no public function calls another, it answers exactly
SKIP: NO_PUBLIC_CALLS. The full contract is the
Behavior Spec.
Read this first. On code that parses, Python's own ast computes this diagram exactly, and
this model does not; on code that does not parse, an error-recovering parser plus a few rules does
nearly as well as this model, and on the one kind they cannot read (a file that lost its
indentation) a search checked by compile does better (33 of 33 against 23). The project's
recommendation is a router with no model (eval.py --model search-router in the repository). The
measurements are below; the reasoning is in the
broken-code report.
Results
273 broken files made from held-out code (click, httpx, attrs) by nine kinds of known edits,
so every expected diagram is exact (evals/scenarios/broken.jsonl); graded by a deterministic gate
against ast's diagram of the intended code:
| Answerer | Passed (of 273) | Lost indentation (of 33) |
|---|---|---|
| Base Qwen3-4B-Instruct-2507 | 0 | 0 |
| tree-sitter (error-recovering parser) | 170 | 0 |
| tree-sitter after two rules (pasted line numbers, merge conflicts) | 223 | 0 |
| v2, the previous version, under the new instruction | 173 | 17 |
| v3, this model, alone | 176 (seed 2: 170) | 23 (seed 2: 21) |
| Router with this model (pre-registered) | 246 (seed 2: 244) | 23 |
| Router with a rule-based re-indenter, no model | 244 | 21 |
Router with a compile-checked indentation search, no model | 256 | 33 |
| Claude Opus 5.5, three examples | 265 | 33 |
The locked 40 scenarios (25 clean, 15 adversarial, evals/scenarios/own-v3.jsonl): this model
alone passes 16/25 and 11/15; v2 passed 19/25 and 11/15 on its first seed and 15/25 and 10/15 on
its second; both routers pass all 40.
Use
from huggingface_hub import hf_hub_download
from transformers import AutoModelForCausalLM, AutoTokenizer
repo = "nharder/py2mermaid-qwen3-4b"
tokenizer = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, torch_dtype="auto", device_map="auto")
system_prompt = open(hf_hub_download(repo, "system_prompt.txt")).read()
source = "# FILE: shop.py\n" + open("shop.py").read()
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": source}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
output = model.generate(inputs.to(model.device), max_new_tokens=2048, do_sample=False)
print(tokenizer.decode(output[0, inputs.shape[1] :], skip_special_tokens=True))
The model was trained with the product prompt (diagram 2.0.0, published here as
system_prompt.txt) as the system turn; use it. Decoding is greedy.
Reproduce the numbers
git clone https://labs.gauntletai.com/nicholasharder/py2mermaid-slm && cd py2mermaid-slm
uv sync && npm ci
uv run python eval.py --model nharder/py2mermaid-qwen3-4b --revision <this commit> \
--eval-set evals/scenarios/broken.jsonl --judge off
uv run python eval.py --model routed:nharder/py2mermaid-qwen3-4b --revision <this commit> \
--eval-set evals/scenarios/broken.jsonl --judge off
The raw transcripts of every reported run are in evals/results/.
Training
LoRA (rank 16, scale 2.0, all layers) with mlx-lm on a 4-bit copy of the base, learning rate 1e-4,
2 epochs, batch 2, prompt masked, then fused into these bf16 weights: the one config every version
of this project used. The data is
nharder/py2mermaid-data (v3): v2's
1,123 distinct gate-checked rows, and 572 broken copies of them, labelled by ast on the intended
code, in place of v2's repeated rows, so v3 trained v2's 1,695 rows and steps.
Limitations
- Adds or misses edges on about a third of inputs, broken or not; it is not better than v2 on clean code (27 of 40 against 30 and 25 on v2's two seeds).
- The broken copies added nothing measurable over v2 overall (paired 30 against 27 files, p = 0.79); on lost indentation 23 against 17 (p = 0.07).
- The damage is synthetic so that labels are exact; files broken in the wild are untested.
- A call through a local variable (
writer = Writer(); writer.save()) is not an edge by the project's rules, and the model follows them. Python only; inputs up to about 3,000 tokens.