Growing the Fragment Library by Writing Code

Generative models for molecules face a well-documented ceiling: to guarantee local chemical validity, most of them assemble molecules from a fixed dictionary of predefined fragments — benzene rings, functional groups, peptide motifs — rather than atom by atom. The consequence, shown repeatedly in the literature, is that the model can only recombine what its dictionary already names: models "perform recombination rather than de novo invention" (Bonil Koo et al.), and fixed building blocks restrict generation to known combinations (Xiang Fu et al.). The obvious fix — a bigger dictionary — backfires, because autoregressive decoding over a huge vocabulary combinatorially explodes and degrades both speed and reconstruction fidelity (Hierarchical Generation of Molecular Graphs using Structural Motifs).

This proposal argues that the fragment library bottleneck is not a chemistry problem but an action-space problem, and that the right action space is already available: executable code. Instead of storing fragments as dictionary entries, we let a language model write short RDKit construction programs that synthesize fragments on demand, and we train it with Group Relative Policy Optimization (GRPO) — a critic-free reinforcement learning method that scores each candidate output against the mean and standard deviation of a sampled group of peers, which stabilizes training under sparse rewards (Chen et al.). The interpreter supplies the reward signal, a planning/coding role split supplies the credit assignment, and the library grows as a side effect of optimization. In two sentences: a fragment becomes a program rather than a token, and the vocabulary expands exactly when — and only when — the optimizer hits a wall the current library cannot cross.

Core Claim

Three mechanisms are fused, each inherited from a different direction and each doing a specific job.

Fragments as procedures. The library stores each fragment as a triple: a canonical SMILES, the executable script that constructs it, and its provenance. Construction scripts are built from reaction-like transformations extracted from USPTO-MIT, following the logic of SynCoGen: Synthesizable 3D Molecule Generation via Joint Reaction and Coordinate Modeling, which guarantees synthesizability by construction rather than by post-hoc filtering. This differs from GEAM (Drug Discovery with Dynamic Goal-aware Fragments), which also grows a vocabulary dynamically but does so with genetic crossover and mutation that "frequently produce synthetically intractable or chemically invalid graphs that must be heavily filtered." A code-defined fragment is valid and interpretable by construction, and adding one costs a dictionary insert, not the continuous re-indexing and retraining that makes GEAM's architecture heavy.

Execution-ladder rewards against advantage collapse. The central technical obstacle to GRPO in de novo molecular generation is the learning cliff: when every candidate in a group fails, the group's reward standard deviation is zero and the gradient vanishes (Scaf-GRPO: Scaffolded Group Relative Policy Optimization for Enhancing LLM Reasoning). In multi-property molecular optimization, whole-group failure is the norm, not the exception (Sample Efficiency Matters: A Benchmark for Practical Molecular Optimization). Our answer is not injected hints (Scaf-GRPO's solution, which uses static teacher templates in math domains) but the execution environment itself. The sandbox gives a natural ladder of verifiable outcomes — did the script run, did it produce a sanitized molecule, did intermediate properties move, did the full objective hold — and process-aware intermediate rewards of exactly this kind are known to mitigate advantage collapse in long-horizon reasoning (Chen et al.). Concretely, for a candidate trajectory $o$:

$$r(o) = \alpha_1\,\mathbb{1}[\text{script executes}] + \alpha_2\,\mathbb{1}[\text{molecule sanitizes}] + \alpha_3\,\Delta_{\text{prop}} + \alpha_4\,\mathbb{1}[\text{all constraints met}],$$

where the $\alpha_i$ are fixed weights, $\mathbb{1}[\cdot]$ is an indicator, and $\Delta_{\text{prop}}$ is the normalized improvement on the targeted property over the input molecule. The group-relative advantage $\hat{A}_i$ is then computed from these ladder scores in the standard way. A candidate that crashes the interpreter is still separated from one that runs but breaks valency, so almost no group is truly uniform and the cliff rarely opens.

Role-split credit assignment. A single model asked to do chemical planning and RDKit implementation simultaneously hallucinates API calls and invalid structures (Coder as Editor: Code-driven Interpretable Molecular Editing), and single models fail to self-correct due to confirmation bias (Self-collaboration Code Generation via ChatGPT). We keep the decomposition strictly minimal — a Planner that produces a chemical rationale and a fragment specification, a Coder that writes the script, and a mechanical Verifier sandbox — because adding agents beyond this yields diminishing returns and invites collusion (Adversarial Review: Cooperative Code Review through Structured Disagreement; Studying Coordination and Collusion in Multi-Agent LLM Code Reviews). Following M-GRPO (Multi-Agent Deep Research: Training Multi-Agent Systems with M-GRPO), which treats multi-agent rollouts as groups for relative credit assignment, we train both roles jointly but attribute rewards by role: a syntax or API failure penalizes the Coder's group, an executed-but-chemically-flawed outcome penalizes the Planner's group. This directly targets the error-attribution problem that makes multi-agent failures hard to trace (Aegis: Automated Error Generation and Attribution for Multi-Agent Systems).

Research Program

The system, which we call a procedural fragment library, runs a capped three-iteration refinement loop per candidate: the Planner proposes an edit rationale, the Coder writes the RDKit script, the Verifier executes it in a sandbox and returns tracebacks, sanitization errors, and property readouts, and failed rounds re-prompt both roles with that feedback. Following the rollback design of Fang et al., the loop checkpoints each accepted state and reverts on regression, because uncapped iterative fixing compounds errors. The cap is deliberate: execution feedback plateaus after three to five rounds in general code refinement (Yan et al.), and we treat that plateau as diagnostic signal rather than noise.

That diagnostic is the second research product. We distinguish two plateau causes. A debugging plateau — scripts run but fail chemically — is handled by rollback and the iteration cap. A vocabulary plateau — every group produces valid molecules that simply cannot improve the objective because the required fragment class is absent — triggers library growth: the best-performing construction scripts from the group are generalized into parameterized fragment-building functions, stored as reusable tools in the style of LearnAct (Zhao et al.), which showed agents can expand their own action space by debugging and caching Python functions, and added to the library. This gives a measurable, mechanistic account of when dynamic vocabulary expansion pays off, which GEAM does not provide. Two known risks are monitored throughout: GRPO in multi-turn agent settings can suffer policy collapse through lazy likelihood displacement (On Group Relative Policy Optimization Collapse in Agent Search: The Lazy Likelihood-Displacement), which we track via per-role KL divergence against the reference policy with early stopping; and iterative prompting can amplify flawed behavior (Srivastava et al.), which the deterministic Verifier bounds by rejecting any script that fails sanitization regardless of how it was reached.

Baselines

The naive reference point is random recombination over a static BRICS fragment vocabulary extracted from ZINC250k — the setup GEAM used as its static baseline, which achieved only a ~13% novel hit ratio on the jak2 docking target versus ~43% for dynamic vocabulary updates (Drug Discovery with Dynamic Goal-aware Fragments). Any method that cannot beat static BRICS by a clear margin has no claim to breaking the vocabulary bottleneck.

The strongest existing method on the vocabulary question is GEAM itself, which combines a graph information bottleneck, Soft Actor-Critic assembly, and genetic vocabulary mutation; its reported ~43% novel hit ratio on jak2 (with more negative docking scores than baselines) is the number to beat or match, and we will run it under the same oracle budget and target set rather than re-implementing it loosely. On the code-driven side, the closest competitor is MECo (Coder as Editor: Code-driven Interpretable Molecular Editing), a two-role single-turn system that reached over 98% execution accuracy on USPTO-MIT edits and consistency rates above 90% on ChemCoTBench; running our system with one rollout per prompt, no refinement loop, and a frozen library reduces it to a MECo-like configuration, which doubles as the ablation that isolates the closed loop. On the broader optimization question, the PMO benchmark's own finding is that classical methods dominate under tight budgets — "REINVENT ranked #1 overall across the metrics, followed closely by Graph GA" (Sample Efficiency Matters: A Benchmark for Practical Molecular Optimization); no reported result found on Lacuna for the numeric AUC of REINVENT or Graph GA on PMO, so we will reproduce both under the benchmark's 10,000-oracle-call budget with equal tuning effort. Beating the baseline means, concretely: higher PMO AUC of top-10 average property score than Graph GA on the subset of tasks where the vocabulary-plateau diagnostic fires, a novel hit ratio on the GEAM docking targets at or above ~43% under the same oracle budget, and consistency rate on ChemCoTBench at or above MECo's reported >90% with strictly fewer failed-execution aborts.

Why not just…?

Why not just use a much larger static fragment library? The literature gives three independent reasons this fails: decoding over large vocabularies combinatorially explodes and degrades fidelity (Hierarchical Generation of Molecular Graphs using Structural Motifs); compressing to abstract geometric shapes rather than growing the dictionary is what made MAGNet generalize (MAGNet: Motif-Agnostic Generation of Molecules from Shapes); and empirically, a static BRICS vocabulary reached ~13% novel hits on jak2 where dynamic updates reached ~43% (Drug Discovery with Dynamic Goal-aware Fragments). We will still run this baseline — a 500k-fragment BRICS/Murcco library from ZINC and ChEMBL — because if it matches our hit ratio, the procedural library is unnecessary complexity and the proposal should die.

Why not just generate SELFIES strings directly? SELFIES guarantees valid strings, but MECo found that direct string editing causes non-local corruption and consistency-rate collapse, while an executable intermediate representation raised consistency by 38 to 86 percentage points (Coder as Editor), and the PMO benchmark found SELFIES validity does not universally improve optimization performance (Sample Efficiency Matters). A SELFIES policy is also structurally incapable of the vocabulary-growth mechanism, since its alphabet is fixed by construction.

Why not just use Best-of-N sampling over a frozen code-editing LLM? This is essentially MECo at inference time, and it is a real threat on cheap tasks. But on hard multi-property tasks it hits the learning cliff: unguided sampling persistently yields zero rewards and no learning gradient (Scaf-GRPO — this mechanism is the paper's central finding). Our experiments settle it directly, since Best-of-N over the same base model at the same oracle budget is one of the run configurations.

Why not just use one chain-of-thought frontier model? Single models tasked with planning plus tool use plus code writing degrade from context overload and hallucinate tool calls (A survey on LLM-based multi-agent systems: workflow, infrastructure, and challenges), and self-refinement is undermined by confirmation bias (Self-collaboration Code Generation via ChatGPT). We run this as a baseline too, with the same model and budget, because if a single DeepSeek-R1-class model with a rubric matches the two-agent system, the role split is not earning its coordination cost.

Why not just use Graph GA? This is the most serious objection, and we are honest about it: on PMO's 10,000-call budget, Graph GA beat most modern generative methods (Sample Efficiency Matters), and it does so with trivial engineering. Our response is not to argue but to partition the claim: Graph GA's crossovers and mutations frequently produce synthetically inaccessible structures and offer no rationale for any edit (Sample Efficiency Matters; SynCoGen), so on surrogate-heavy PMO tasks it may well win and we will report that. The decisive comparison is on tasks where the vocabulary boundary binds — the GEAM docking targets with rare-geometry requirements, where Haitao Lin et al. and ZAIXI ZHANG et al. show rigid fragment generators fail — and on synthetic-accessibility and rationale-availability metrics alongside raw score.

Decisive Checks

The proposal stands or falls on three results. First, on the PMO benchmark (23 tasks, AUC of top-10 average versus oracle calls, 10,000-call budget), the system must beat Graph GA specifically on the task subset where the vocabulary-plateau diagnostic fires; matching it everywhere else is acceptable. Second, on the GEAM docking protocol (jak2, parp1, and the other constrained targets), the novel hit ratio must reach or exceed GEAM's reported ~43% under an equal oracle budget while producing higher synthetic-accessibility scores than GA-mutated fragments, and the library must demonstrably contain fragments absent from every static baseline, each with a runnable construction script. Third, the ablation grid must show the mechanism and not just the machinery: removing the execution ladder (sparse final reward only) should reproduce advantage collapse — measurable as the fraction of training steps with zero group variance — removing library growth should cap performance at the static-BRICS level on vocabulary-bound tasks, and collapsing the two roles into one model should raise the error-misattribution rate, measured by penalizing the wrong role on held-out failure traces. If the role-split ablation shows a single model matching the pair, we drop the multi-agent claim and keep the rest; the procedural library and the execution ladder are the load-bearing ideas, and the experiments are designed so that each can fail independently and informatively.

References

  1. Transcriptome-conditioned molecule generation via gene interaction-aware fragment modeling with a GPT-based architecture
  2. Fragment-Based Sequential Translation for Molecular Optimization
  3. Hierarchical Generation of Molecular Graphs using Structural Motifs
  4. Posterior-GRPO: Rewarding Reasoning Processes in Code Generation
  5. SynCoGen: Synthesizable 3D Molecule Generation via Joint Reaction and Coordinate Modeling
  6. Drug Discovery with Dynamic Goal-aware Fragments
  7. Scaf-GRPO: Scaffolded Group Relative Policy Optimization for Enhancing LLM Reasoning
  8. Sample Efficiency Matters: A Benchmark for Practical Molecular Optimization
  9. VIDEOP2R: Video Understanding from Perception to Reasoning
  10. Coder as Editor: Code-driven Interpretable Molecular Editing
  11. Self-Collaboration Code Generation via ChatGPT
  12. Adversarial Review: Structured Disagreement for Grounded Agentic Code Review
  13. Studying Coordination and Collusion in Multi-Agent LLM Code Reviews
  14. Multi-Agent Deep Research: Training Multi-Agent Systems with M-GRPO
  15. Aegis: Automated Error Generation and Attribution for Multi-Agent Systems
  16. MEIC: Re-thinking RTL Debug Automation using LLMs
  17. CodeTransOcean: A Comprehensive Multilingual Benchmark for Code Translation
  18. Empowering Large Language Model Agents through Action Learning
  19. On Group Relative Policy Optimization Collapse in Agent Search: The Lazy Likelihood-Displacement
  20. Do Users Write More Insecure Code with AI Assistants?
  21. MAGNet: Motif-Agnostic Generation of Molecules from Shapes
  22. A survey on LLM-based multi-agent systems: workflow, infrastructure, and challenges
  23. Functional-Group-Based Diffusion for Pocket-Specific Molecule Generation and Elaboration
  24. Molecule Generation For Target Protein Binding with Structural Motifs