How to Make Large Language Models Generate 100% Valid Molecules?

Tao Wen, Jing Tang, Alvin Chan, Bryan Hooi, Baolong Bi, Nanyun Peng, Yuansheng Liu, Yiwei Wang

Abstract

Large Language Models (LLMs) have demonstrated significant potential in computational chemistry, yet they frequently struggle to generate syntactically and semantically valid molecular representations. While the SMILES (Simplified Molecular-Input Line-Entry System) format is the industry standard, its fragile syntax leads to high failure rates in LLM outputs. Conversely, while the SELFIES (SELF-referencIng Embedded Strings) representation guarantees validity, LLMs lack the pre-training exposure to use it effectively. This paper introduces SmiSelf, a framework that bridges this gap by using SELFIES as an intermediate correction layer. SmiSelf ensures 100% molecular validity across various LLMs and tasks without compromising structural accuracy or requiring extensive retraining.

Introduction

The integration of Large Language Models (LLMs) into drug discovery and materials science promises to accelerate the design of novel molecules. However, a persistent bottleneck remains: "hallucinations." In the context of chemistry, an LLM might generate a string that looks like a molecule but contains syntax errors (e.g., unclosed rings) or violates physical laws (e.g., pentavalent carbons).

Current molecular representations like SMILES are highly sensitive to minor character changes. While alternative representations like SELFIES were designed to be robust, LLMs are "SMILES-native" due to the prevalence of SMILES in their training data. This paper presents SmiSelf, a post-hoc correction framework that leverages the strengths of both representations to achieve perfect validity in molecular generation.

The Challenge: SMILES vs. SELFIES

SMILES is the most common way to represent molecules as strings. However, its grammar is strict. A single missing parenthesis or an incorrect digit can render the entire string unparseable by chemical software.

SELFIES was developed to solve this by ensuring that every possible string of characters maps to a valid molecular graph. Theoretically, asking an LLM to generate SELFIES should solve the validity problem. However, research shows that LLMs perform significantly worse when prompted in SELFIES. Because LLMs are trained on vast amounts of internet text where SMILES is the dominant standard, they struggle to "think" in SELFIES, leading to poor structural accuracy even if the strings are technically valid.

The SmiSelf Framework

To resolve this, the authors propose SmiSelf, a "cross-chemical language" approach. Rather than forcing the LLM to learn a new representation or asking it to self-correct (which often leads to "semantic drift"), SmiSelf acts as an algorithmic filter.

The SmiSelf pipeline consists of four primary stages:

  1. SMILES Parsing: The LLM's raw SMILES output is parsed into a molecular graph. This step is designed to be "forgiving," attempting to interpret the intended structure even if minor syntax errors exist.
  2. Graph-to-SELFIES: The resulting graph is converted into a SELFIES string.
  3. SELFIES-to-Graph: The SELFIES string is converted back into a graph. During this step, the inherent grammatical rules of SELFIES automatically prune semantic errors, such as atoms exceeding their valence limits.
  4. Graph-to-SMILES: The cleaned graph is converted back into a standard, 100% valid SMILES string for the user.

Experimental Evaluation

The researchers tested SmiSelf using several LLMs, including GPT-3.5, GPT-4, and LLaMA, across two primary benchmarks:

ChEBI-20 Benchmark

On the ChEBI-20 dataset (molecule-caption pairs), SmiSelf achieved 100% validity for all models. Notably, it also improved the Exact Match (EM) rate. For example, GPT-3.5's zero-shot EM rose from 0.019 to 0.020, while validity jumped from approximately 80% to 100%. This indicates that SmiSelf doesn't just fix the strings—it helps the model reach the correct chemical target more reliably.

Class-Specific Generation

The framework was also tested on its ability to generate specific types of monomers (Acrylates, Chain Extenders, and Isocyanates). In few-shot settings, standard prompting for Acrylates resulted in a dismal 23% validity. SmiSelf successfully brought this to 100%. Furthermore, it improved the "Membership" score, ensuring that the generated molecules actually belonged to the requested chemical class.

Discussion and Key Insights

The success of SmiSelf highlights a critical nuance in AI for chemistry: the distinction between syntactic and semantic errors. Syntactic errors are broken string rules, while semantic errors are physical impossibilities. By using SELFIES as a "physical filter," SmiSelf addresses both.

One of the most important findings is that LLM self-correction is often a "trap." When an LLM is told its SMILES string is invalid and asked to fix it, it frequently changes the molecule's identity entirely. SmiSelf’s algorithmic approach preserves the original "intent" of the LLM's output much more effectively than iterative prompting.

Conclusion and Future Work

SmiSelf provides a robust, plug-and-play solution for ensuring molecular validity in LLM outputs. While it is a post-hoc method—meaning it cannot fix a completely nonsensical initial output—it significantly improves the reliability of AI-driven molecular design. Future research may focus on integrating this logic directly into the LLM's decoding process or expanding the framework to support 3D molecular structures.

Summary updated

BibTeX
@misc{wen2025how,
  author = {Tao Wen and Jing Tang and Alvin Chan and Bryan Hooi and Baolong Bi and Nanyun Peng and Yuansheng Liu and Yiwei Wang},
  title = {{How to Make Large Language Models Generate 100\% Valid Molecules?}},
  year = {2025},
  eprint = {2509.23099},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2509.23099},
}
Figure