Abstract
Molecular optimization is a central task in drug discovery that requires precise structural reasoning and domain knowledge. While large language models (LLMs) have shown promise in generating high-level editing intentions in natural language, they often struggle to faithfully execute these modifications—particularly when operating on non-intuitive representations like SMILES. We introduce MECo, a framework that bridges reasoning and execution by translating editing actions into executable code. MECo reformulates molecular optimization for LLMs as a cascaded framework: generating human-interpretable editing intentions from a molecule and property goal, followed by translating those intentions into executable structural edits via code generation. Our approach achieves over 98% accuracy in reproducing held-out realistic edits derived from chemical reactions and target-specific compound pairs. On downstream optimization benchmarks spanning physicochemical properties and target activities, MECo substantially improves consistency by 38-86 percentage points to 90%+ and achieves higher success rates over SMILES-based baselines while preserving structural similarity. By aligning intention with execution, MECo enables consistent, controllable and interpretable molecular design, laying the foundation for high-fidelity feedback loops and collaborative human-AI workflows in drug discovery.
Introduction
Molecular optimization—the process of iteratively modifying a molecule to enhance specific properties such as solubility or binding affinity—is fundamental to drug discovery. Recent attempts to leverage Large Language Models (LLMs) for this task typically involve prompting them to generate new SMILES strings directly. However, this approach suffers from significant drawbacks. SMILES strings are brittle and non-intuitive; minor structural changes can result in radically different strings, leading LLMs to produce chemically invalid "gibberish." Furthermore, direct generation acts as a "black box," where the rationale behind specific edits is obscured. Most critically, there is often a misalignment between the model's stated reasoning (e.g., "adding a hydroxyl group") and the actual SMILES output, undermining trust in human-AI collaborative workflows.
Methods: The MECo Framework
To address these challenges, we introduce MECo (Molecular Editing via Code generation). MECo decouples chemical reasoning from structural execution by treating molecular optimization as a programming task. The framework operates in a two-stage cascade:
- Intention Generation: A reasoning-centric LLM (such as DeepSeek-R1) analyzes the input molecule and the desired property goal to produce a natural language "intention" and rationale.
- Action Execution: A specialized "Coder LLM" (a fine-tuned Qwen2.5-Coder) translates these natural language instructions into a Python script using the RDKit library. This script performs precise graph-level modifications.
By framing editing as code generation, MECo leverages the inherent logical proficiency of LLMs in programming to bypass the complexities of SMILES manipulation. This approach ensures that the final molecule strictly adheres to the AI's stated reasoning, providing a verifiable and "debuggable" record of the transformation.
Datasets and Evaluation
The Coder LLM was fine-tuned on a synthetic dataset of 50,000 "molecule-edit-code" triples derived from the ZINC database using programmatic moiety replacement. To evaluate the framework's ability to generalize to real-world scenarios, the authors tested it on two "realistic" datasets:
- Reaction-derived edits: Extracted from the USPTO-MIT reaction dataset.
- Target-based edits: Derived from matched molecular pairs in ChEMBL35.
Downstream performance was further assessed using ChemCoTBench, covering six optimization tasks: three physicochemical properties (logP, solubility, QED) and three target activities (DRD2, JNK3, GSK3β).
Key Results
MECo demonstrates near-perfect fidelity, achieving over 98% execution accuracy on realistic edit benchmarks. This significantly outperforms direct SMILES generation models, which frequently fail to execute requested edits correctly. A key highlight is the massive gain in "structure-intention consistency"—the alignment between the stated plan and the final molecule—which improved by 38 to 86 percentage points, reaching over 90% across most tasks. On the ChemCoTBench, MECo achieved higher success rates than state-of-the-art baselines while maintaining superior structural similarity to the starting molecule, indicating more precise and conservative editing strategies.
Conclusion
MECo represents a paradigm shift in molecular optimization by aligning intention with execution through code. By using code as a universal interface, the framework bypasses the brittleness of text-based molecular representations and provides an interpretable record of chemical changes. While the system's success currently depends on the quality of the initial reasoning model, it lays the groundwork for high-fidelity feedback loops in drug discovery, enabling more effective collaboration between human experts and AI systems.
Summary updated