Molecular fragments provide chemically meaningful units, but existing molecular languages rarely make their locations and connections explicit for editing. Here we introduce STAMP (Sequential Tree Attachment-based Motif-Preserving representation), a topology-aware molecular language that connects fragment-level representation learning with precise structural editing. STAMP separately encodes motif identity, attachment sites and tree topology, making fragments reusable across contexts and subtrees directly accessible. Learned representations capture motif similarity and recurring transformations, and lower activity-cliff prediction error on 23 of 30 targets versus a matched SMILES encoder. Compatible subtree operations support scaffold-preserving optimization, ranking first among six methods on 15 of 23 benchmark objectives. A deterministic compiler enables frozen language models to act on indexed fragments, improving molecular editing, optimization, transformation transfer and change identification by 9 to 56 percentage points over plain SMILES. Together, these results establish an executable molecular language in which learned chemical units remain directly operable during design.
Figures & tables
Figure 1: A factorized fragment-tree language connects molecular structure, sequence, learning and action. a , The reference molecule in an atom-sequential language, a fragment-assembly language and STAMP. Motif tokens encode fragment identity; joint and branch tokens encode attachment sites and tree topology. b , Motif mining, encoding into an addressed fragment tree and deterministic reconstruction. Motif groups align tree nodes with sequence spans. c , Four applications of the shared state: fragment-constrained molecular construction, property prediction, objective-guided fragment optimization and instruction-guided editing. For editing, the language model selects a node and fragment, and the compiler executes the action.
Figure 2: STAMP improves molecular generation across unconditional and conditioned settings. a , SA and QED histograms for eight representations trained with the same four-layer causal Transformer (dimension 256) for 10 epochs with a shared 100,000-molecule ZINC-250K training input set. Each model produced 10,000 requested samples. Histograms count unique valid molecules in common bins; dashed lines indicate means. b , Larger-scale generation with a 12-layer STAMP AO-GPT (dimension 768). Points show mean ± s.d., using five seeds for STAMP and published values for SAFE-GPT and GenMol. The displayed GenMol configuration uses τ = 0.5 and r = 0.5. c , Constraint-aware quality for four fragment-constrained tasks. The linker example retains the supplied fragments and ports and generates the highlighted connecting path. STAMP error bars show s.d. across five seeds; GenMol values are published means. d , A ChEBI-20 text-to-molecule example and six aggregate metrics on a common 0–1 scale. Levenshtein distance is shown separately. The reference molecule is the evaluation target.
Figure 3: STAMP improves property prediction and organizes chemical structure at multiple scales. a , MoleculeNet results for STAMP and published ChemBERTa-2 10M baselines, paired by MLM or MTR objective; bold indicates the better value. The scatter plot compares activity-cliff RMSE on 30 MoleculeACE targets after matched fine-tuning. b , Mean ECFP4 Tanimoto similarity of contextual motif neighbours as neighbourhood size increases, with random motif pairs as the reference. c , Frozen-embedding distances for attachment-position, motif-identity and unrelated molecular changes, followed by the leave-one-context-out consistency of recurring transformations. Boxes span the 25th–75th percentiles, centre lines mark medians, and whiskers extend to the most extreme observations within the 5th–95th percentile interval; outliers are not shown. The dashed null uses displacements from different transformations. d , Structural and embedding distances used in the alignment analysis, and target-level Spearman correlations on the structure-matched subset of 90,620 pairs.
Figure 4: Fragment-tree search supports executable, core-constrained molecular optimization. a , STAMP subtree crossover under the default exocyclic-single-bond decomposition. Each box is a motif token and each edge is a parent–child relation. The selected recipient subtree (blue) and compatible donor subtree (dark grey) are contiguous two-node spans. Open circles mark the matched single-bond attachments; heavy-atom size tolerance is checked separately. The right-hand tree shows the offspring serialization immediately after crossover; deterministic decoding yields the molecular product above. b , Four replacement operators evaluated over 1,000 recipient–donor trials each. Both strict validity and strict-valid scaffold retention use all trials as the denominator. c , QuickVina2 scores on the lead-optimization benchmark. Rows identify the target and starting molecule. Cell text gives the absolute score; colour gives the change from the starting molecule. Lower scores are better, and bold marks the best score within each three-method group at similarity threshold δ = 0.4 or 0.6. Dashes indicate no improving candidate satisfying all constraints. Coverage is reported separately. STAMP values are three-seed means; baseline values are from GenMol. The 5ht1b/2 result at δ = 0.6 was reached by the atom-level action.
Oracle
STAMP
GenMol
f -RAG
Gen. GFN
Mol GA
REINVENT
albuterol_similarity
0.9778
0.937
0.977
0.949
0.896
0.882
amlodipine_mpo
0.8105
0.810
0.749
0.761
0.688
0.635
celecoxib_rediscovery
0.8747
0.826
0.778
0.802
0.567
0.713
deco_hop
0.9279
0.960
0.936
0.733
0.649
0.666
drd2
0.9994
0.995
0.992
0.974
0.936
0.945
fexofenadine_mpo
0.8720
0.894
0.856
0.856
0.825
0.784
Table 1: AUC-Top10 on the Practical Molecular Optimization benchmark. STAMP values are means across three seeds under a 10,000-call online oracle budget. Bold marks the highest mean in the six-method comparison. STAMP uses the same target-aware prescreening protocol as GenMol; offline-bank construction is specified in Methods. Baseline means and aggregate scores are taken directly from the original publications [ 28 , 32 , 47 ] .
Figure 5: Compiled fragment actions support instruction-guided molecular editing. a , An instruction is converted into an action on an indexed STAMP state and executed by the compiler. The MolEdit example specifies hydroxyl-to-aldehyde replacement; the MolOpt example specifies increased QED and carboxyl growth. Blue marks the addressed node and red the executed change. Molecules are task illustrations. b , Success on all 500 examples in each of six S 2 -Bench subtasks for frozen Qwen-2.5-7B-Instruct and DeepSeek-V4-Pro, comparing STAMP subtree actions with complete-SMILES and complete-STAMP rewriting. Success tests group-count changes for MolEdit and property direction for MolOpt. c , Two-stage transfer on 120 examples per model and independent change identification on 100 pairs per model. Transfer predicts a removed/added plan from A/A’, then edits B using that plan in a separate model call. Identification matches chemical information across representations, requires both group names to be correct and executes no edit. STAMP uses port-preserving single-node replacement for transfer and node views for identification. Atom-indexed controls and shared-label transfer results are in Supplementary Note 11.
Molecular optimization seeks to improve a molecule through small structural edits while preserving similarity to the starting compound. Recent language-model approaches typically treat this task as prompt-conditioned sequence generation. However, relying on natural language introduces an inherent data-scaling bottleneck, often leads to chemical hallucinations, and ignores the strong context dependence of fragment effects. We present FORGE, a two-stage framework that reformulates molecular optimization as context-aware local editing. By utilizing automatically mined, verified low-to-high edit pairs instead of expensive human text annotations, Stage 1 ranks candidate fragments by their property contribution under the full molecular context to inject chemical prior, and Stage 2 generates explicit fragment replacements. Built on a compact 0.6B language model, FORGE further adapts to unseen black-box objectives through in-context demonstrations. Across Prompt-MolOpt, PMO-1k and ChemCoTBench, FORGE consistently outperforms prior methods, including substantially larger language models and graph methods. These results highlight the value of explicit fragment-level supervision as a more easily obtainable, scalable, and hallucination-less alternative to natural language training.
Qingchuan Zhang, He Cao, Hao Li +6
University of Science and Technology of China · International Digital Economy Academy · Peking University +5
Transformer-based language models for SMILES strings suffer from a locality gap: standard character-level tokenization fragments chemically meaningful motifs, forcing models to repeatedly learn local syntax at the expense of long-range dependencies. To address this without disrupting standard tokenizers, we propose MolGram, which integrates a conditional n-gram memory module into molecular language models. MolGram maps local string patterns to learned embeddings via scalable hash lookups and dynamically injects this regional context into hidden states. Evaluations across three tasks, including unconditional molecule generation, forward reaction prediction, and single-step retrosynthesis, show that MolGram consistently improves performance. Crucially, our analyses demonstrate that MolGram outperforms baselines with 3× more parameters, establishing explicit local pattern memory as a highly efficient inductive bias.
Xinni Zhang, Zijing Liu, He Cao +2
1The Chinese University of Hong Kong · 2International Digital Economy Academy
Large language models possess strong chemical reasoning capabilities, making them effective molecular editors. However, property-relevant information is implicitly entangled across their dense hidden states, providing no explicit handle for property control: a substantial fraction of edits fail to improve or even degrade target properties. To address these issues, we propose SLIM (Sparse Latent Interpretable Molecular editing), a plug-and-play framework that decomposes the editor's hidden states into sparse, property-aligned features via a Sparse Autoencoder with learnable importance gates. Steering in this sparse feature space precisely activates property-relevant dimensions, improving editing success rate without modifying model parameters. The same sparse basis further supports interpretable analysis of editing behavior. Experiments on the MolEditRL benchmark across four model architectures and eight molecular properties show consistent gains over baselines, with improvements of up to 42.4 points.
Mingxu Zhang, Yuhan Li, Lujundong Li +3
The Hong Kong University of Science and Technology (Guangzhou) · Nanjing University of Aeronautics and Astronautics · The 63rd Research Institute, National University of Defense Technology, Nanjing