G2T-LLM: Graph-to-Tree Text Encoding for Molecule Generation with Fine-Tuned Large Language Models

Zhaoning Yu, Xiangyang Xu, Hongyang Gao · DOI

Introduction

The discovery of novel, chemically valid molecules is a cornerstone of modern drug development and materials science. Traditionally, this task has relied on specialized graph-based models or linear text representations like SMILES (Simplified Molecular Input Line Entry System). However, both approaches face significant hurdles. Graph models, while structurally accurate, often struggle with scalability and generalization across diverse chemical spaces. Conversely, Large Language Models (LLMs), despite their exceptional reasoning and generalization capabilities, are fundamentally designed for sequential text. This creates a representational mismatch: the non-linear, cyclic nature of molecular graphs—comprising rings, branches, and complex connectivity—is often lost when forced into a one-dimensional string, leading to "hallucinated" structures that are chemically impossible.

G2T-LLM addresses this gap by introducing a Graph-to-Tree (G2T) encoding framework. By transforming molecular graphs into hierarchical, tree-structured text formats like JSON, the researchers enable LLMs to leverage their pre-trained understanding of structured data to generate complex, valid, and novel molecular architectures.

Methodology: Bridging Graphs and Trees

The core innovation of G2T-LLM lies in its three-pronged approach: hierarchical encoding, supervised fine-tuning, and strict inference-time constraints.

1. Graph-to-Tree (G2T) Encoding

Rather than relying on natural language descriptions or flat strings, G2T-LLM converts molecular graphs into a nested JSON format. This choice is strategic: modern LLMs are pre-trained on vast repositories of code and structured data, making them inherently proficient at parsing parent-child relationships and nested hierarchies. To represent cycles and rings—structures that are traditionally non-hierarchical—the encoding assigns unique identifiers to atoms. When a bond refers back to a previously defined ID, the model interprets it as a ring closure, effectively mapping a graph onto a tree structure without losing topological information.

The Graph-to-Tree (G2T) encoding process, transforming a cyclopropene graph into a structured JSON tree.

2. Supervised Fine-Tuning (SFT)

The researchers utilized a LLaMA 3.1-8B model as the backbone for G2T-LLM. The model underwent supervised fine-tuning on a "molecular completion" task. In this setup, the model is presented with a partial molecular structure in JSON format and tasked with predicting the subsequent atoms and bonds. This process allows the LLM to internalize the specific "grammar" of chemical structures and the hierarchical conventions of the G2T format.

3. Token Constraining and Guardrails

To prevent the generation of chemically nonsensical outputs, the framework implements a dynamic token-constraining system during inference. This system acts as a chemical "guardrail," restricting the model's output vocabulary at each step. It ensures that the generated text strictly adheres to the JSON schema and, more importantly, respects the fundamental laws of chemistry, such as atomic valency (e.g., preventing a carbon atom from forming more than four bonds).

Experimental Evaluation

G2T-LLM was evaluated against two benchmark datasets:

  • QM9: A dataset containing small molecules with up to nine heavy atoms, serving as a test for fundamental chemical rule adherence.
  • ZINC250k: A dataset of 250,000 drug-like molecules, used to evaluate the model's ability to handle complex, medically relevant structures.

Key Results

The model achieved state-of-the-art (SOTA) performance, rivaling or exceeding specialized diffusion and flow-based models:

  • Chemical Validity: G2T-LLM reached a validity score of 99.47% on QM9 and 98.03% on ZINC250k.
  • Exceptional Novelty: On the ZINC250k dataset, the model achieved a perfect 100% novelty score. This indicates that every valid molecule generated was a unique discovery not present in the training data, demonstrating that the model learned the underlying logic of chemistry rather than merely memorizing patterns.
  • Structural Diversity: The model showed a high capacity for replicating the structural diversity found in real-world chemicals, outperforming several specialized models like DiGress in maintaining chemical coherence and scaffold similarity.

Discussion and Insights

The success of G2T-LLM provides several critical insights into the intersection of LLMs and scientific data:

  1. Structured Data as a "Native Tongue": The comparison between G2T-LLM and prose-based graph descriptions (like "Talk Like a Graph") reveals that structured formats like JSON are significantly more effective for encoding relational complexity.
  2. The Necessity of Constraints: Without token constraints, validity on the ZINC dataset dropped from 98.6% to 41.6%. This underscores that while LLMs are powerful generators, they require rigorous, domain-specific constraints to be reliable in scientific applications.
  3. Generalization Power: The high novelty scores suggest that the hierarchical tree representation allows the LLM to generalize its understanding of molecular fragments and their valid combinations, effectively "dreaming" of new chemical spaces.

Limitations and Future Outlook

Despite its performance, G2T-LLM faces challenges related to computational efficiency. Running an 8-billion parameter model is significantly more resource-intensive than using smaller, specialized graph neural networks. Additionally, the token-by-token generation process is inherently slower than "one-shot" generation methods.

Future research will likely focus on optimizing inference speeds and extending the framework to conditional generation. This would allow researchers to prompt the model for specific properties, such as "design a non-toxic molecule that binds to a specific protein target," moving G2T-LLM closer to a fully functional tool for automated drug discovery.

Summary updated

BibTeX
@misc{yu2024g,
  author = {Zhaoning Yu and Xiangyang Xu and Hongyang Gao},
  title = {{G2T-LLM: Graph-to-Tree Text Encoding for Molecule Generation with Fine-Tuned Large Language Models}},
  year = {2024},
  eprint = {2410.02198},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.02198},
}
Figure