Can docstring reformulation with an LLM improve code generation?

Nicola Dainese, Alexander Ilin, Pekka MarttinenStudent Research Workshop @ EACL 2024

TL;DR

This paper explores whether improving function docstrings using LLMs can boost code generation performance. Surprisingly, they found that current open-source LLMs are remarkably robust to docstring quality, showing little improvement even with reformulated or "oracle" docstrings. This suggests that while docstrings are crucial, small improvements might not be enough to significantly enhance LLM code generation capabilities.

What Problem Does This Paper Address?

The paper tackles the challenge of improving code generation, specifically the task of function completion, where an LLM needs to write the body of a function given its signature and a docstring. While much research focuses on improving the LLMs themselves (training, fine-tuning, prompting), this work investigates a different angle: optimizing the input to the LLM. Specifically, they question whether improving the docstring – the natural language description of a function's purpose and usage – can lead to better code generation. This is important because docstrings are a key part of the input for code generation models, and understanding their impact and how to optimize them could offer a complementary path to better code generation.

Methods

The core idea is to use a separate LLM (the "reformulator") to rewrite the original docstrings. They propose two main methods for this docstring reformulation:

  1. Supervised Fine-Tuning (SFT): The reformulator LLM is fine-tuned on pairs of original docstrings and their "best" reformulated versions. The "best" reformulation is determined by generating multiple reformulations and selecting the one that leads to the highest code generation pass rate (pass@1) when evaluated by a "coder" LLM.
  2. Optimization by PROmpting (OPRO): This method keeps the reformulator LLM fixed and instead optimizes the instructions given to the reformulator. It uses a process similar to reinforcement learning, where instructions are iteratively refined based on the performance of the generated docstrings.

Both methods aim to produce docstrings that, when fed to a coder LLM, result in more accurate code completions.

Datasets

The primary dataset used is HumanEval, a widely-used benchmark for evaluating code generation. To specifically test the impact of docstring quality, the authors created four new variants of HumanEval by introducing realistic faults into the docstrings:

  • Misspelling: Typos in important words.
  • Ambiguity: Removal of examples and hints.
  • Distractor: Insertion of irrelevant sentences.
  • Bad formatting: Removal of type hints, blank lines, and example markers.

They also created "oracle" docstrings (hints or full solutions) to establish an upper bound on potential improvements.

Key Insights and Ideas

  • Docstrings are Crucial, but LLMs are Robust: Removing docstrings entirely drastically hurts code generation performance, confirming their importance. However, current LLMs are surprisingly resilient to imperfect docstrings.
  • Small Improvements Don't Always Translate: Even when docstrings are "improved" by LLMs or have minor errors corrected, the performance gains for many open-source LLMs are negligible.
  • "Oracle" Docstrings Show Potential: When provided with extremely high-quality docstrings (like "oracle hints" or even the ground truth solution), performance dramatically increases, highlighting the potential of docstring optimization. This suggests the limitation might be in how LLMs leverage small improvements, not in the concept itself.
  • Model-Agnosticism is Tricky: While the goal was to find improvements beneficial to all LLMs, the results show mixed effects, with some models even performing worse with reformulated docstrings.

Key Results

  • SFT and OPRO Methods: Both proposed methods showed only limited improvements in code generation pass@1 scores across various LLMs and modified HumanEval datasets. For instance, on the original HumanEval, the average pass@1 score for SFT was 35.1% (reformulated) vs. 34.9% (original), and for OPRO it was 34.5% vs. 34.9%.
  • Impact of Faulty Docstrings: Introducing faults like misspellings or ambiguity generally led to a slight decrease in performance, but not drastically for most models.
  • Oracle Docstrings: Performance jumped significantly with oracle docstrings, with an average pass@1 of 70.4% compared to 34.9% for original docstrings. This indicates a large gap between current capabilities and potential.
  • Model Robustness: Most tested LLMs showed little to no significant performance change when using reformulated docstrings compared to their original or even worsened versions.

Limitations and Future Work

  • Exploration Challenge: The methods struggle to explore the vast space of possible docstring reformulations to find the truly optimal ones.
  • Noisy Learning Signal: The pass@1 metric, used as feedback, is noisy due to the stochastic nature of LLM code generation, making stable optimization difficult.
  • Overfitting Concerns: While not observed empirically, there's a theoretical risk of reformulations overfitting to specific coder models.
  • Future Directions: The authors suggest exploring more efficient search strategies for promising reformulations, investigating RL-based methods like RLHF and DPO for optimization, and focusing on cross-dataset generalization. They also note that the quality of the instruction optimizer LLM is critical, as a poor one can lead to hallucinated information and degraded performance.

Summary updated

BibTeX
@inproceedings{dainese2024can,
  author = {Nicola Dainese and Alexander Ilin and Pekka Marttinen},
  title = {{Can docstring reformulation with an LLM improve code generation?}},
  booktitle = {Student Research Workshop @ EACL},
  year = {2024},
  url = {https://openreview.net/forum?id=Jh03a56MdB},
}
Figure