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

## Abstract

Abstract We introduce G2T-LLM, a novel approach for molecule generation that uses graph-to-tree text encoding to transform graph-based molecular structures into a hierarchical text format optimized for large language models (LLMs). This encoding converts complex molecular graphs into tree-structured formats, such as JSON and XML, which LLMs are particularly adept at processing due to their extensive pre-training on these types of data. By leveraging the flexibility of LLMs, our approach allows for intuitive interaction using natural language prompts, providing a more accessible interface for molecular design. Through supervised fine-tuning, G2T-LLM generates valid and coherent chemical structures, addressing common challenges like invalid outputs seen in traditional graph-based methods. While LLMs are computationally intensive, they offer superior generalization and adaptability, enabling the generation of diverse molecular structures with minimal task-specific customization. The proposed approach achieved comparable performances with state-of-the-art methods on various benchmark molecular generation datasets, demonstrating its potential as a flexible and innovative tool for AI-driven molecular design.

## 1 Introduction

Molecular generation is a critical task in fields such as drug discovery, material science, and chemistry (Schneider & Fechner, ; Simonovsky & Komodakis, ; Elton et al., ). The ability to design and create novel molecules with specific properties can accelerate the development of new therapies, advanced materials, and innovative chemicals. Traditional approaches to molecular generation, such as rule-based systems (Schneider & Fechner, ; Sastry et al., ) and graph-based (You et al., ; Madhawa et al., ; Shi et al., ) models, have provided foundational tools. However, these methods often face limitations in generating diverse, valid, and chemically coherent molecular structures, restricting their ability to explore the vast chemical space effectively (Vignac et al., ; Jo et al., ).
Recent advancements in deep learning, especially the rise of large language models (LLMs), offer new opportunities for molecular generation (Brahmavar et al., ; Wang et al., ; Yao et al., ). Unlike traditional methods, LLMs are not constrained by domain-specific rules and can generalize from vast amounts of data. This flexibility allows them to generate creative and diverse content, potentially uncovering novel chemical compounds. Prior non-LLM approaches, such as graph-based generative models (You et al., ; Madhawa et al., ; Shi et al., ; Luo et al., ; Vignac et al., ; Jo et al., ), often struggle with limited generalization, rule-based rigidity, or difficulty scaling to more complex chemical structures. In contrast, LLMs can adapt to a wide range of prompts and provide greater flexibility, making them an attractive choice for AI-driven molecular generation.

Despite the promise of LLMs, applying them to molecular generation presents a unique challenge. Molecular structures are typically represented as graphs, with atoms as nodes and bonds as edges. LLMs, however, are trained to understand sequences of tokens (Vaswani, ), particularly in structured text formats such as XML and JSON (Brown, ), and are not inherently designed to process graph-based data. This mismatch creates a barrier when attempting to use LLMs for tasks that require understanding the relational and non-linear properties of molecular structures. LLMs (Luo et al., ; Le et al., ) may struggle to generate chemically valid or meaningful molecules without proper representation.

To overcome this challenge, we propose a novel Graph-to-Tree Text Encoding designed to transform molecular graphs into a format that LLMs can process effectively. Inspired by SMILES but not relying on it, our encoding converts graph-based molecular structures into hierarchical text representations, such as JSON and XML. These formats are naturally suited to LLMs, which excel at interpreting tree-like structures due to their training on similar data. By converting molecular graphs into tree-structured text, we align the data representation with the strengths of LLMs, enabling them to understand and generate molecules more effectively.
With the graph-to-tree text encoding in place, we supervised fine-tuned LLMs to generate valid and coherent chemical structures. This fine-tuning process ensures that the generated molecules adhere to chemical rules and constraints, addressing common challenges such as the generation of invalid or chemically infeasible molecules. The fine-tuning allows LLMs to learn how to translate natural language prompts into meaningful molecular designs, opening new possibilities for human-guided molecule generation.
Our approach has demonstrated comparable performances with state-of-the-art (SOTA) models on several benchmark molecular generation datasets. These results validate the effectiveness of our graph-to-tree encoding in making LLMs capable of generating chemically sound and diverse molecules. Additionally, the performance gains achieved underscore the potential of LLMs as a flexible and innovative tool for molecular generation, particularly when paired with a well-suited encoding.

This work makes the following contributions:

- •
We propose G2T-LLM, a novel approach that transforms graph-based molecular structures into text formats like JSON and XML, optimized for large language models.
- •
We introduce a token constraining technique to guide the LLM’s generation process, ensuring that the output adheres to the expected tree-structured format, which is critical for maintaining molecular coherence.
- •
We develop a supervised fine-tuning method to enable LLMs to generate valid and coherent chemical structures, leveraging graph-to-tree text encoding.
- •
We achieve comparable performances with state-of-the-art models on benchmark molecular generation datasets, demonstrating the effectiveness and potential of our approach for AI-driven molecular design.

## 2 Related Work

Graph Generation.
The graph generation task aims to learn the distribution of graphs. The traditional approaches (Zang & Wang, ; Shi et al., ; Luo et al., ; You et al., ; Madhawa et al., ; Dai et al., ) such as auto-regression, Generative Adversarial Network (GAN), and Variational Autoencoder (VAE) have been explored for this purpose. However, they have faced challenges in modeling the permutation-invariant nature of graph distribution and learning the relationship between edges and nodes, often due to limitations in their model capacity.
Recent advancements in diffusion methods (Niu et al., ; Jo et al., ; Vignac et al., ; Jo et al., ) have significantly improved graph generation. GDSS (Jo et al., ) generates both node features and adjacency matrices simultaneously, resulting in better alignment with graph datasets. DiGress (Vignac et al., ) addresses the challenge of generating graphs with categorical node and edge attributes, which is a difficult task due to the unordered nature and sparsity of graphs. GruM (Jo et al., ) directly learns graph topology, improving connectivity and structure recovery.

Graph to Text for LLM.
The emergence of large language models (LLMs) has driven significant advancements in the natural sciences (Taylor et al., ; Liu et al., ). These models are trained on vast amounts of text data, the most abundant type of data, contributing to their success across many tasks.
Multi-modal methods (Luo et al., ; Le et al., ) have been proposed to incorporate both graph and text information. They typically rely on graph neural networks or transformers to encode graphs. However, these methods often use text, such as SMILES, to represent molecular features. SMILES may not tokenize the molecular structure effectively, limiting the ability to represent the molecule structure accurately. As a result, the graph embeddings may be too weak for intricate molecular structures, limiting performance in molecular generation tasks.

Recently, there have been attempts (Fatemi et al., ) to represent graphs in natural language formats, encoding their structure using descriptive language. However, this naive approach introduces challenges, as such encodings are unlikely to appear in typical text, meaning that LLMs—trained predominantly on conventional text data—may struggle to process them effectively. Using an encoding that aligns with the LLMs’ training data is essential. We propose leveraging tree-structured formats like JSON and XML to encode molecules to address this issue. The JSON format is a widely used and structured data representation commonly found in LLM training. This allows us to capture the complexity of molecular graphs while ensuring compatibility with LLMs.

## 3 G2T-LLM

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

Figure: Figure 1: Illustration of the Graph-to-Tree Text Encoding process described in Section 3.2 and Algorithm 1. This figure shows how the molecular structure of cyclopropene is transformed into a hierarchical tree representation. Each atom and bond is mapped to nodes and edges in the tree, with unique identifiers assigned.
Refer to caption: x1.png

### 3.1 Challenges and Motivations

Molecular graphs pose a challenge for LLMs due to their inherently complex, non-linear structures, where atoms (nodes) and bonds (edges) form intricate connectivity patterns, including rings, branches, and cycles. Traditional LLMs excel at processing sequential data, such as natural language, where information flows in a linear manner. However, molecular graphs do not naturally conform to this format, as their connections often lack a clear, ordered sequence. This mismatch complicates the application of LLMs to molecule-related tasks.

Despite these challenges, LLMs have shown a capacity to handle structured, hierarchical data formats, such as JSON and XML. These formats share some of the complexity of graphs but are still expressed as trees, with clear parent-child relationships between elements. LLMs trained on such data can handle hierarchical structures by processing them as sequences while maintaining the relationships and nested dependencies inherent to these structures. This training has made LLMs particularly adept at handling data that can be decomposed into nested layers, making them better suited for tree-like representations than arbitrary graphs.

To leverage this strength, we propose encoding molecular graphs into a tree structure. This approach is inspired by SMILEs, which are essentially tree representations of molecular graphs, proving that molecular graphs can be effectively serialized as trees while preserving their chemical properties. This encoding acts as a bridge between the graph-based molecular structures and the LLM’s ability to process and generate hierarchical data. The LLM can be trained on these tree-encoded molecules, and it can also output molecules in the same structured format, facilitating the generation of coherent molecular representations.
By aligning graph data with a format that LLMs are well-equipped to handle, this method holds the potential for improving the coherence and plausibility of generated molecular structures.

### 3.2 Graph-to-Tree Text Encoding

To make molecular graphs accessible to LLMs, we introduce a tree-based encoding inspired by the SMILES format. SMILES encodes molecules by performing a depth-first traversal over the molecular graph and representing it as a linear string. In our approach, we extend this traversal to build a hierarchical tree structure, where atoms are represented as nodes and their bonds as edges connecting them. The hierarchical nature of the tree is well-suited for the LLM’s training with tree-like structures.

However, molecular graphs often contain rings and cycles—features that trees cannot naturally represent. To address this, we assign each atom in the molecule a unique identifier (ID). When the traversal encounters a ring closure or cycle, the tree refers back to the atom’s unique ID rather than creating a new node, thereby preserving both the hierarchical structure and chemical validity. This encoding technique ensures that we accurately capture the full molecular graph in a way the LLM can process, while maintaining the integrity of complex molecular features such as rings and branches. Algorithm 1 and Algorithm 2 describe the processes for converting a molecular graph to a tree-structured text representation and for reconstructing the graph from this format, respectively. Figure 1 illustrates the graph-to-tree text encoding.

Figure: Algorithm 1 Convert Molecular Graph to Tree-Structured Text Representation

Figure: Algorithm 2 Convert Tree-Structured Text to Molecular Graph

### 3.3 Token Constraining for Valid Tree-Structure Generation

Despite the advancements in LLMs, there remains a significant challenge in ensuring that the outputs adhere to valid tree-structured formats. LLMs, while capable of generating coherent text, may produce sequences that do not respect the hierarchical relationships required for molecular representation. This can lead to outputs that are structurally invalid, failing to accurately represent the complex relationships inherent in molecular graphs.

To mitigate this issue, we implement a set of constraints that guide the token generation process of the LLM. These constraints filter the tokens allowed at each step, ensuring that generated outputs remain within the bounds of valid tree structures. Specifically, we impose rules that dictate acceptable parent-child relationships, enforce valid connections between atoms, and restrict the formation of non-hierarchical sequences. Additionally, we constrain the types of atoms and bonds that can be generated, ensuring that only valid atom types (e.g., carbon, oxygen) and bond types (e.g., single, double) are used in the output. This approach leverages domain knowledge of molecular structures to create a robust framework for guiding the LLM’s outputs.

The application of token constraining significantly enhances the reliability of the generated tree-structured outputs. By enforcing these constraints, we improve the chances that the LLM produces valid representations of molecular structures that can be effectively used in further analyses or applications. This technique not only aids in ensuring the accuracy of the generated data but also reinforces the overall effectiveness of our graph-to-tree text encoding approach, making it a vital component in achieving coherent and chemically valid molecular generation.

### 3.4 Supervised Fine-Tuning LLMs for Molecular Generation

A key challenge in leveraging large language models for molecular generation is that, without specialized training, they may struggle to produce valid molecular structures, particularly when dealing with complex features such as rings, cycles, and the inherent chemical constraints that govern molecular formation. Supervised fine-tuning addresses this issue by teaching the LLM domain-specific rules and patterns, enabling it to generate valid molecular structures that adhere to chemical principles.

We structure the fine-tuning process as a molecular completion task. The LLM is trained by prompting it with a partial molecular structure, encoded using the graph-to-tree text encoding and tasking it with predicting the remaining atoms and bonds necessary to complete the molecule. For each training example, we provide the LLM with an incomplete molecular graph, and the model is then expected to generate the missing parts based on the information provided. The model’s output is evaluated against the full molecular structure’s text encoding, and the loss is computed based on the accuracy of its predictions. By iterating through this process, the LLM learns to predict the completion of molecular graphs in a way that respects chemical validity, helping the model better handle challenging structural features. Note that token constraining is deliberately omitted during fine-tuning, allowing the LLM to explore and learn more freely before constraints are imposed during inference. Figure 2 illustrates the supervised fine-tuning process of G2T-LLM.

The fine-tuning process is integral to the success of our approach. By casting molecular generation as a completion task and using the proposed graph-to-tree encoding as a bridge between molecular structures and the LLM’s capabilities, we enhance the model’s ability to generate coherent and chemically valid outputs. This fine-tuning approach refines the LLM’s understanding of molecular patterns and constraints, enabling it to produce outputs that are more reliable and scientifically grounded within the realm of molecular design.

Figure: Figure 2: An illustration of the supervised fine-tuning process of G2T-LLM. The process begins by randomly selecting a starting component, exemplified by cyclopropene, which is encoded into a partial tree structure and passed as a prompt to the LLM. The LLM generates the remaining molecular structure, which is compared against the ground truth. A loss is computed and is used to fine-tune the model, iteratively improving its performance in generating valid molecular graphs.
Refer to caption: x2.png

Figure: Figure 3: An illustration of the inference process of G2T-LLM. The process starts by prompting the model with a random molecular component. The model, a fine-tuned LLM (SFT-LLM), generates new molecular structures while applying token constraints to ensure valid outputs. The output is a tree-structured text representing the molecule. It is then decoded back into a molecular graph corresponding to cyclopropene.
Refer to caption: x3.png

### 3.5 Inference Process of G2T-LLM

The molecular generation process begins with selecting a random molecular component, which could be an atom, a bond, or even a larger motif. This component serves as the initial prompt for the fine-tuned LLM. The component is encoded into the graph-to-tree text format, creating a tree-structured representation that the LLM can process.

Once the LLM receives this initial prompt, it is tasked with generating the subsequent components of the molecular structure. At each step, the LLM’s output is constrained by the Token Constraining mechanism, ensuring that only chemical and schema-valid tokens—such as specific atom types and bond types—are generated. These constraints help guide the LLM in maintaining the coherence of the structure, preventing invalid or nonsensical outputs, and ensuring that the generated molecule adheres to the expected chemical rules.
As the LLM iteratively predicts new components, these outputs are progressively combined into an expanding tree-structured text. This generated text represents the molecular graph, with nodes corresponding to atoms and edges corresponding to bonds. Once the generation process is complete, the final tree-structured text is decoded back into a full molecular graph. This graph is then translated into a standard molecular format, fully reconstructing the molecule from the text generated by the LLM. Figure 3 illustrates the inference process of G2T-LLM.

## 4 Experiments

In this section, we conduct comprehensive experiments on two real-world datasets to evaluate the effectiveness of our proposed methods.

### 4.1 Experimental Setup

Datasets and Metrics. We evaluate the quality of molecule generation using two real-world datasets: QM9 (Ramakrishnan et al., ) and ZINC250k (Irwin et al., ). Following the evaluation setting used in (Jo et al., ), we measure model performance across four metrics.
Validity is the proportion of generated molecules that are valid without any valency corrections.
Novelty is the proportion of valid molecules that are not present in the training dataset.
Frechet ChemNet Distance (FCD) (Preuer et al., ) measures the similarity between two molecule sets by comparing the activations of the penultimate layer of the ChemNet model.
Scaffold similarity (Scaf.) evaluates the model’s ability to generate similar substructures.

Baselines. We compare our model with following molecular graph generation methods. MoFlow (Zang & Wang, ) is a one-shot flow-based model that generates entire molecular graphs in a single step. GraphAF (Shi et al., ) and GraphDF(Luo et al., ) are autoregressive flow-based models, generating molecules sequentially. Additionally, we evaluate against the diffusion models. EDP-GNN (Niu et al., ) is a score-based model designed for generating adjacency matrices. GDSS (Jo et al., ) uses a continuous diffusion process for molecule generation, DiGress (Vignac et al., ) employs a discrete diffusion approach, and Grum (Jo et al., ) designed a mixture of endpoint-conditioned diffusion processes.

Although several studies have explored using LLMs for molecular generation, direct comparisons with our approach are not feasible. For instance, LMLF (Brahmavar et al., ), Grammar Prompting (Wang et al., ), and LLM4GraphGen (Yao et al., ) all employ rule-based prompt-engineering techniques that fundamentally differ from our SFT LLM approach. These models rely on predefined rules and heuristics to guide the generation process, which restricts their ability to learn from the underlying data distributions. In contrast, our method leverages a more flexible and adaptive encoding, allowing the LLM to capture the complexities of molecular structures more effectively.

Moreover, the baseline models utilize significantly larger architectures, such as GPT-4, whereas our experiments are conducted with LLaMA3.1-8B. This disparity in model size and complexity further complicates direct comparisons, as the performance capabilities and learned representations of these models can vary widely. Therefore, assessing our results against those achieved by larger, rule-based models may not provide a meaningful evaluation of performance, given the substantial differences in methodologies and model architectures.

Implementation details.
For our G2T-LLM, we conduct experiments using the LLaMA3.1-8B model (Dubey et al., ) as our base LLM, selected for its strong performance in text generation tasks. The model parameters are fine-tuned with torchtune (Ansel et al., ), and we leverage QLoRA (Dettmers et al., ) to accelerate training while reducing memory consumption. The fine-tuning dataset consists of 5,000 molecules, and the model is trained with a batch size of 8, using the AdamW optimizer (Loshchilov, ) with a weight decay of 0.01 and a learning rate of 3e-4. The learning rate is adjusted by a cosine schedule with 100 warmup steps, and cross-entropy loss is employed for the loss computation. All model computations are performed with the bf16 data type. Fine-tuning is carried out on an NVIDIA A100 SXM4 80GB, and inference is done on NVIDIA GeForce RTX 3090 and 4090 GPUs. The implementation is done in PyTorch (Paszke et al., ).

### 4.2 Experimental Results

**Table 1: Generation results on the QM9 and ZINC250k datasets. We report the mean of 3 different runs. The best results are highlighted in bold. The second-best results are highlighted in underline. We provide the results of uniqueness, and NSPDK in Appendix A.**
| Datasets | QM9 | ZINC250K |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Methods | Valid$\uparrow$ | Novelty$\uparrow$ | FCD$\downarrow$ | Scaf$\uparrow$ | Valid$\uparrow$ | Novelty$\uparrow$ | FCD$\downarrow$ | Scaf$\uparrow$ |
| MoFlow | 91.36 | 94.72 | 4.467 | 0.1447 | 63.11 | 100.00 | 20.931 | 0.0133 |
| GraphAF | 74.43 | 86.59 | 5.625 | 0.3046 | 68.47 | 99.99 | 16.023 | 0.0672 |
| GraphDF | 93.88 | 98.54 | 10.928 | 0.0978 | 90.61 | 100.00 | 33.546 | 0.0000 |
| EDP-GNN | 47.52 | 86.58 | 2.680 | 0.3270 | 82.97 | 100.00 | 16.737 | 0.0000 |
| GDSS | 95.72 | 86.27 | 2.900 | 0.6983 | 97.01 | 100.00 | 14.656 | 0.0467 |
| DiGress | 98.19 | 25.58 | 0.095 | 0.9353 | 94.99 | 99.99 | 3.482 | 0.4163 |
| Grum | 99.69 | 24.15 | 0.108 | 0.9449 | 98.65 | 99.98 | 2.257 | 0.5299 |
| Ours | 99.47 | 88.29 | 0.815 | 0.9112 | 98.03 | 100.00 | 2.445 | 0.6062 |

Figure: Figure 4: Visualization of the generated molecules with Tanimoto similarity scores based on Morgan fingerprints. The best results are highlighted in bold.
Refer to caption: x4.png

Table 1 presents performance comparisons on both the QM9 and ZINC250k datasets against baseline models. Our approach consistently achieves top-two validity scores across both datasets, demonstrating its effectiveness in enabling the LLM to capture the underlying chemical rules essential for accurate molecule generation. For novelty, our method attains a perfect score of 100% on the ZINC250k dataset and 88% on QM9, highlighting its ability to consistently generate novel molecular structures.
In terms of FCD and Scaf metrics—critical indicators of a model’s ability to explore and replicate chemical space—our method delivers competitive performance compared to other baselines. While DiGress and Grum show strong FCD and Scaf scores on the QM9 dataset, their novelty scores fall significantly short (below 40%), suggesting potential overfitting to the training data rather than true generalization of molecular distributions. In contrast, our method not only maintains high novelty rates but also achieves strong performance on FCD and Scaf metrics.
On the ZINC250k dataset, our approach attains the highest Scaf score and the second-best FCD score, further demonstrating its superior ability to generalize and innovate within chemical spaces. This robust performance underscores our model’s advanced understanding and application of molecular distributions, making it a powerful tool for innovative molecular design in computational chemistry.

### 4.3 Visualization Results of Generated Molecules

In Fig. 4, we follow the experimental setup outlined in (Jo et al., ), using Tanimoto similarity based on Morgan fingerprints to evaluate the generated molecular graphs. For consistency and comparability, we select the same molecules as (Jo et al., ). Additionally, we perform experiments on molecular graphs generated by Grum (Jo et al., ). Across most cases, our method demonstrates superior performance compared to previous state-of-the-art diffusion-based approaches, showcasing its effectiveness and robustness in molecular graph generation.

### 4.4 Ablation Study: Impact of Tree-Structured Text Encoding

**Table 2: Study of the impact of tree-structured text encoding on the ZINC250K dataset.**
| Methods | Valid$\uparrow$ | FCD$\downarrow$ | Scaf$\uparrow$ | Novelty$\uparrow$ |
| --- | --- | --- | --- | --- |
| Talk like a graph | 59.20 | 19.8114 | 0.1317 | 100 |
| Ours | 98.60 | 5.6906 | 0.1522 | 100 |

To evaluate how our proposed graph-to-tree text encoding improves the LLM’s ability to learn graph structures compared to the previous graph-to-text methods such as Talk Like a Graph (Fatemi et al., ), we conducted experiments on the challenging Zinc250K dataset (Irwin et al., ), which contains larger molecules. Talk Like a Graph encodes graph structures by converting them into natural language, where each node’s connections and attributes are described in sentence form.
For the fine-tuning process, we randomly selected 5,000 molecules from the training set and generated 1,000 molecules for performance comparison. As shown in Table 2, our method significantly outperforms the previous approach across all metrics, demonstrating that encoding molecular structures in JSON format enables LLMs to more effectively learn and replicate complex molecular structures.

### 4.5 Ablation Study: Impact of supervised Fine-Tuning LLM

**Table 3: Comparison of LLM performance with and without SFT on the ZINC250k dataset.**
| Methods | Valid$\uparrow$ | Unique$\uparrow$ | Novelty$\uparrow$ |
| --- | --- | --- | --- |
| w/o SFT | 70.80 | 61.12 | 100.00 |
| w/ SFT | 98.60 | 98.98 | 100.00 |

In this study, we aim to evaluate the impact of supervised fine-tuning on LLM performance. Specifically, we generate 1,000 molecules using the same prompt to compare the performance of the LLM before and after fine-tuning. This direct comparison allows us to assess how fine-tuning enhances the model’s ability to accurately generate molecular structures. We conduct this experiment using the ZINC250k dataset, and the results are presented in Table 3. The results reveal that without fine-tuning, the LLM produces molecules with only 70.8% validity and 61.12% uniqueness, indicating that the model, in its initial state, struggles to fully comprehend and accurately replicate the text representation of molecular structures. However, after fine-tuning, there is a significant improvement, with validity and uniqueness increasing to 99.6% and 99.79%, respectively. These results highlight the effectiveness of fine-tuning in substantially improving the model’s performance, demonstrating its critical role in enabling the LLM to better understand and generate precise molecular structures.

### 4.6 Ablation Study: Impact of size of the Fine-Tuning dataset

**Table 4: Comparison of LLM performance with different size of fine-tuning datasets**
| Methods | Valid$\uparrow$ | Novelty$\uparrow$ | FCD $\downarrow$ | Scaf $\uparrow$ |
| --- | --- | --- | --- | --- |
| 1k (10 epoch) | 98.50 | 90.38 | 1.226 | 0.6933 |
| 5k (10 epoch) | 98.70 | 86.53 | 1.219 | 0.7779 |
| 10k (10 epoch) | 98.50 | 73.89 | 1.146 | 0.7980 |

In this section, we investigate the impact of dataset size on the performance of a LLM during fine-tuning. Our experiments use the QM9 dataset with three distinct dataset sizes for fine-tuning: 1,000, 5,000, and 10,000 molecules. Each model is trained over 10 epochs. This setup enables a systematic evaluation of how variations in fine-tuning data size affect the model’s learning efficacy and its ability to generalize. Table 4 presents the results of these experiments.
The results indicate an improvement in the FCD and Scaf scores as the dataset size increases. This improvement likely stems from the LLM’s exposure to a larger array of data points, which enhances its understanding of the chemical distribution within the dataset. Conversely, we observe a decrease in novelty scores with larger datasets. This reduction may be attributed to the relatively small and structurally simple nature of the QM9 dataset, which comprises only four types of atoms and molecules not exceeding nine atoms. As the model encounters more data, it increasingly reproduces similar outputs, reflecting the limited diversity in the dataset.

### 4.7 Ablation Study: Impact of Token Constraining

**Table 5: Comparison results of using token constraining (TC) on molecular generation on the ZINC250k dataset.**
|  | w/o TC | w/ TC |
| --- | --- | --- |
| Validity (%)$\uparrow$ | 41.60 | 98.60 |

In this section, we examine the impact of token constraining on molecular generation, as introduced in Section 3.3. Token constraining is implemented to guide the LLM toward generating valid molecular structures by restricting its output to adhere to chemical rules. To evaluate the effectiveness of this approach, we perform an experimental comparison using the ZINC250k dataset. Specifically, we generate 1,000 molecules to compare the validity of the output with and without token constraining. The results, presented in Table 5, clearly demonstrate the efficacy of token constraining in improving the validity of generated molecules. Without token constraining, the validity of the generated molecules is only 41.6%. However, when token constraining is applied, validity dramatically increases to 98.6%. This significant improvement underscores the critical role of token constraining in guiding the LLM to produce valid molecular structures, ensuring closer adherence to the fundamental rules of chemical structure and leading to a higher rate of valid outputs.

## 5 Conclusion

In this work, we introduced G2T-LLM, a novel approach for molecular generation that leverages LLMs to generate valid molecular structures through a novel graph-to-tree text encoding. By converting molecular graphs into hierarchical representations inspired by SMILES but adapted for LLMs, we bridge the gap between non-linear molecular structures and sequential data processing. This encoding allows the LLM to understand the molecular structure better and produce coherent chemical outputs.
Our method addresses the challenges of generating valid molecular structures by introducing token constraints during the generation process, ensuring that the outputs respect some chemical and structural rules. Through supervised fine-tuning, we further align the LLM with molecular generation tasks, improving its ability to produce chemically valid molecules based on the learned data patterns from benchmark datasets like Zinc250K and QM9.
Our results demonstrate the effectiveness of G2T-LLM, achieving state-of-the-art performance on benchmark datasets. This work highlights the potential of utilizing LLMs in molecular design, opening up new avenues for AI-driven discoveries in chemistry. The combination of hierarchical encoding, token constraining, and fine-tuning proves to be a powerful strategy for tackling the complexities of molecular generation. Future work will focus on refining these techniques to enhance efficiency and explore further applications in drug discovery and material science.

## Appendix A Additional experiments results

Here are additional experiment results on QM9 and ZINC250k datasets. The Neighborhood Subgraph Pairwise Distance Kernel (NSPDK) Maximum Mean Discrepancy (MMD) (Costa & Grave, ) evaluates the difference between generated and test molecules, accounting for both node and edge features.
Uniqueness refers to the percentage of valid molecules that are distinct from each other. Validity, FCD, Novelty, and Scaf have been introduced before.

**Table 6: Generation results on the QM9 dataset. We report the mean of 3 different runs. The best results are highlighted in bold. The second-best results are highlighted in underline.**
| Methods | Valid (%)$\uparrow$ | FCD $\downarrow$ | NSPDK $\downarrow$ | Scaf $\uparrow$ | Unique (%)$\uparrow$ | Novelty (%)$\uparrow$ |
| --- | --- | --- | --- | --- | --- | --- |
| MoFlow | 91.36 | 4.467 | 0.017 | 0.1447 | 98.65 | 94.72 |
| GraphAF | 74.43 | 5.625 | 0.021 | 0.3046 | 88.64 | 86.59 |
| GraphDF | 93.88 | 10.928 | 0.064 | 0.0978 | 98.58 | 98.54 |
| EDP-GNN | 47.52 | 2.680 | 0.005 | 0.3270 | 99.25 | 86.58 |
| GDSS | 95.72 | 2.900 | 0.003 | 0.6983 | 98.46 | 86.27 |
| DiGress | 98.19 | 0.095 | 0.0003 | 0.9353 | 96.67 | 25.58 |
| Grum | 99.69 | 0.108 | 0.0002 | 0.9449 | 96.90 | 24.15 |
| Ours | 99.47 | 0.815 | 0.002 | 0.9112 | 89.57 | 88.29 |

**Table 7: Generation results on the ZINC250k dataset. We report the mean of 3 different runs. The best results are highlighted in bold. The second-best results are highlighted in underline.**
| Methods | Valid (%)$\uparrow$ | FCD $\downarrow$ | NSPDK $\downarrow$ | Scaf $\uparrow$ | Unique (%)$\uparrow$ | Novelty (%)$\uparrow$ |
| --- | --- | --- | --- | --- | --- | --- |
| MoFlow | 63.11 | 20.931 | 0.046 | 0.0133 | 99.99 | 100.00 |
| GraphAF | 68.47 | 16.023 | 0.044 | 0.0672 | 98.64 | 99.99 |
| GraphDF | 90.61 | 33.546 | 0.177 | 0.0000 | 99.63 | 100.00 |
| EDP-GNN | 82.97 | 16.737 | 0.049 | 0.0000 | 99.79 | 100.00 |
| GDSS | 97.01 | 14.656 | 0.019 | 0.0467 | 99.64 | 100.00 |
| DiGress | 94.99 | 3.482 | 0.0021 | 0.4163 | 99.97 | 99.99 |
| Grum | 98.65 | 2.257 | 0.0015 | 0.5299 | 99.97 | 99.98 |
| Ours | 98.03 | 2.445 | 0.0049 | 0.6062 | 94.69 | 100.00 |

## Peer Reviews

confidence: 4
contribution: 2
initial_rating: 3
presentation: 2
questions: See weaknesses.
soundness: 2
strengths: The paper proposes an interesting method for molecule generation and validates its effectiveness. The paper is clearly written, and the experiments are comprehensive.
summary: The paper proposes an interesting method for molecule generation, which first represents the molecular graph with structured data in JSON format, and then uses a Large Language Model (LLM) to learn the patterns of structured molecules for molecule generation. To generate valid JSON-formatted molecules, the authors limit the token space generated by the LLM. The authors conducted experiments on the QM9 and ZINC250k datasets, demonstrating that their proposed model can generate valid and novel molecules.
weaknesses: 1. The conversion of molecular graphs into JSON is interesting, but the motivation and necessity are somewhat far-fetched. Molecules can not only be represented by graphs but also by serialized representations, such as IUPAC, SMILES, InChl. These are widely recognized molecular serialization representation methods in the biological field. These representation methods can also be recognized by LLM, and they are superior to JSON in terms of readability, convenience, and complexity. The authors propose a new way of representing molecules and should elaborate on where this method is superior to traditional methods. 

2. The authors state that graph-based methods often face limitations in generating diverse, valid, and chemically coherent molecular structures, restricting their ability to explore the vast chemical space effectively. However, from the experimental results, the method proposed by the authors does not have an advantage over graph-based methods. For example, on QM9, GraphDF's validity is 93.88, novelty is 98.54; the proposed method's validity is 99.48, novelty is 88.29; although validity has increased by about 6%, novelty has lost about 10%. In fact, it is easy for a graph-based method to generate 100% valid and high novelty molecules [1].

3. The authors state that LLMs may struggle to generate chemically valid or meaningful molecules without proper representation. In fact, in the work of using LLM for molecule generation, using SMILES, validity can reach over 99%, and novelty can reach over 90% depending on the dataset [2].

4. Further explanation is needed on how to restrict the generated tokens. There is a lack of description of the specific implementation details, such as how token screening is conducted, which tokens are considered valid, and which are not.

5. The ablation experiments demonstrate that the validity of the generated molecules heavily relies on token constraining. Without this, the validity of the generated molecules drops to only 41.60%. This result also confirms that compared to using SMILES or other molecular serialization representation methods, JSON does not offer any advantages, but rather increases the model's complexity. In contrast, the work referenced in [2] did not use a token constraining method, yet the validity of the generated molecules exceeded 99%. 

[1] Geng, Zijie, et al. "De novo molecular generation via connection-aware motif mining." arXiv preprint arXiv:2302.01129 (2023).

[2] Zhou, Peng, et al. "Instruction Multi-Constraint Molecular Generation Using a Teacher-Student Large Language Model." arXiv preprint arXiv:2403.13244 (2024).

---

confidence: 4
contribution: 2
initial_rating: 3
presentation: 3
questions: See weaknesses.
soundness: 2
strengths: 1. Transforming graphs as json format is interesting and the authors introduce a token constraining technique to guide the generation process.
summary: This paper proposes G2T-LLM, which transforms the molecules as json format for LLM learning. The authors additionally propose a token constraining technique to generate the valid molecules. G2T-LLM achieves comparable performances with state-of-the-art models.
weaknesses: 1. The overall performance is actually not strong compared with the baselines.

2. The baselines or ablation study compared other textual methods to represent molecules, such as SMILES and SMARTS.

3. The json format compared with SIMLES is less straightforward and more time-consuming to generate. Besides, the json format ignore some inductive bias in molecules, such as valence.

---

confidence: 4
contribution: 1
initial_rating: 3
presentation: 1
questions: Please see the *Weaknesses* section for my main concerns.
soundness: 1
strengths: - Ablation studies on the impact of the proposed text encoding and supervised LLM fine-tuning were conducted.
summary: Through this paper, the authors propose G2T-LLM, a method for molecule generation that uses graph-to-tree text encoding to transform graph-based molecular structures into a hierarchical text format optimized for LLMs. The proposed G2T-LLM achieved comparable performances with SOTA methods on unconditional molecule generation tasks.
weaknesses: I will combine the *Weaknesses* section and the *Questions* section. My concerns are as follows:
- The Introduction and Related Work sections are not well organized. In Introduction, only rule-based and graph-based methods were mentioned as previous approaches. In Related Work, only graph generation and graph-to-text methods were introduced. However, sequence-based methods that represents molecules as SMILES [1] or SELFIES [2] are also a very important and big branch in molecular generation. The authors need to list the previous methods well and state the limitations of each method in detail.
- Overall, I think the figures and tables are not carefully drawn and do not help the reader understand. Figure 1, 2, and 3 are drawn with poor abstraction. The raw codes are just pasted, and the figures not readable at a glance. The authors could have used shapes and diagrams to better explain what is happening in each process. Algorithms 1 and 2 are also written with poor abstraction, and the variables and functions used are poorly explained. They cannot be called well-written pseudocode.
- Another main weakness of this paper is that the experimental setup is not sound and extensive enough. Previous papers like GDSS [3], DiGress [4], and GruM [5] are generic graph generation methods and therefore include generic graph generation experiments. On the other hand, recent papers focusing on molecule generation include goal-directed molecule generation benchmarks as well as unconditional molecule generation, with the latter being the main experiment [6, 7]. This is because unconditional molecule generation is less practical and less important for real-world drug discovery problems. To claim that this paper proposes a methodology that is specific to molecule generation, I strongly suggest to include the goal-directed molecule generation experiment as the main experiment. Moreover, sequence-based molecular generation models are missing as baselines.
- The authors described that QM9 is a real-world dataset on Line 344, which is incorrect: most of the molecules in QM9 are very small (those are organic molecules with up to 9 heavy atoms) and do not exist in the real world.
- What are the specific differences between G2T-LLM and SFT-LLM? There is no mention of SFT-LLM in the abstract or introduction, and the only mention of it is in the caption of Figure 3. It seems that the terms are being used interchangeably without a strict definition.
- The authors did not provide the codebase to reproduce the results.

---

**References:**

[1] Weininger, SMILES: a chemical language and information system, Journal of chemical information and computer sciences 28.1 (1988): 31-36.

[2] Krenn et al., Self-referencing embedded strings (SELFIES): a 100% robust molecular string representation, Machine Learning: Science and Technology 1.4 (2020): 045024.

[3] Jo et al., Score-based generative modeling of graphs via the system of stochastic differential equations, ICML, 2022.

[4] Vignac et al., DiGress: discrete denoising diffusion for graph generation, ICLR, 2023.

[5] Jo et al., Graph generation with diffusion mixture, ICML, 2024.

[6] Wang et al., Retrieval-based controllable molecule generation, ICLR, 2023.

[7] Lee et al., Drug discovery with dynamic goal-aware fragments, ICML, 2024.

---

**Typos:**
- Line 357 & Table 1 & Figure 4, Grum -> GruM
- Line 362, SFT LLM -> SFT-LLM
- Line 461 & Line 534, Zinc250k -> ZINC250k

---

confidence: 5
contribution: 2
initial_rating: 3
presentation: 3
questions: - There seems to be duplicated information in the representation; for instance, a double bond is represented twice for the edges (0,1) and (1,0). Why is there no technique, such as pruning, applied? Since the molecular graph is undirected, this could simplify the representation.
- Why not train the tree-to-graph model directly instead of randomly selecting the starting component? What are the advantages of this approach? The random selection of starting points seems questionable, as any molecule containing the substructure could be considered a valid output, making it difficult to define a unique loss function.
soundness: 2
strengths: - The authors propose a novel approach to converting molecular data using XML and JSON, an area that has not been extensively explored before.
- The paper presents various analyses and ablation studies that support the results.
summary: This paper introduces G2T-LLM, a method that integrates hierarchical encoding, token constraining, and supervised fine-tuning for the generation of small molecules. Hierarchical encoding involves converting graph-based molecular structures into hierarchical text formats, such as JSON and XML. Additionally, token constraining is employed to prevent the generation of invalid structures, while supervised fine-tuning focuses on generating hierarchical text based on the molecular substructure. This approach effectively produces valid and coherent molecules, demonstrating competitive performance against baseline methods.
weaknesses: - While molecular data conversion is indeed less explored, there have been a few works that have utilized XML or JSON for graph data applications in large language models (LLMs) [1]. Additionally, representing graphs in a tree format (e.g., nodes connected to each other) is fundamentally similar to the proposed hierarchical encoding [2]. Therefore, the novelty of this approach is somewhat limited.
- There is a lack of discussion regarding related works. The proposed method appears to be closely related to HGGT [3], which also presents a sequential tree-based representation. Incorporating this comparison in the experiments would strengthen the paper significantly.
- The paper does not adequately explain the advantages of the proposed representation compared to SMILES. The statement “SMILES may not tokenize the molecular structure effectively” (line 103) needs clarification. Essentially, both SMILES and the proposed hierarchical representation serve similar purposes, although SMILES may require additional steps to traverse nodes when encountering obstacles. Furthermore, SMILES can accurately represent detailed structures, including rings, aromatic rings, and branches, making it more informative than the proposed method. Including SMILES generation results in the experiments (e.g., by prompting for SMILES or using supervised fine-tuning for SMILES generation) could partially address this issue, but further explanation is required.
- The motivation for using JSON format or tree representation is not well-supported by relevant references. The paper claims that these formats are widely used in LLM training and that LLMs have demonstrated the capacity to handle hierarchical data formats, yet no supporting references are provided.
- The algorithm lacks sufficient detail. For example, the token constraining algorithm does not specify which invalid node and bond types are filtered out, how to enforce a valid JSON format, or how this algorithm integrates with the decoding scheme of the LLM. Since the authors used Llama, enforcing constraints at each step could be challenging. Additionally, the process for randomly selecting the starting component is not clearly explained.
- The results do not appear to show a significant improvement compared to the baselines.

[1]  Kerui Zhu, et al.,Investigating Instruction Tuning Large Language Models on Graphs, COLM 2024.

[2] Ruosong Ye, et al., Langage is all a graph needs. EACL 2024.

[3] Yunhui Jang, et al., Graph generation with K2-trees. ICLR 2024.