NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Self-Planning Code Generation with Large Language Models

Xue Jiang, Yihong Dong, Lecheng Wang, Zheng Fang, Qiwei Shang, Ge Li, Zhi Jin, Wenpin Jiao · DOI

TL;DR

This paper introduces "self-planning" code generation, a novel approach that leverages Large Language Models (LLMs) to first decompose complex coding intents into manageable steps (planning) and then generate code based on these steps. This method significantly outperforms direct code generation and even Chain-of-Thought approaches, leading to more correct, readable, and robust code.

What Problem Does This Paper Address?

Large Language Models (LLMs) have shown impressive capabilities in generating code from natural language descriptions. However, they often struggle with complex intents that require a structured, multi-step problem-solving approach. Humans naturally employ planning to break down complex problems into smaller, manageable sub-problems before writing code. This paper aims to imbue LLMs with this planning capability to improve their code generation performance, especially for intricate tasks. The challenge is to enable LLMs to generate these plans autonomously without extensive manual labeling or training.

Methods

The core innovation is the "self-planning" approach, which operates in two phases:

  1. Planning Phase: The LLM is prompted with a few examples of intents and their corresponding plans. It then generates a concise, step-by-step plan for the given intent. This leverages the LLM's inherent reasoning and decomposition abilities.
  2. Implementation Phase: The LLM receives both the original intent and the generated plan as input. It then generates the code step-by-step, guided by the plan.

This approach is inspired by Chain-of-Thought (CoT) prompting but adapted for code generation, termed "Code CoT" by the authors. The key is that the LLM generates the plan itself, hence "self-planning."

Datasets

The authors evaluated their approach on several public benchmarks:

  • MBPP-sanitized: A subset of MBPP with 427 Python programming problems.
  • HumanEval: A benchmark of 164 handwritten Python programming problems from OpenAI.
  • MBPP-ET and HumanEval-ET: Expanded versions of the above with more test cases.
  • HumanEval-X: A multilingual benchmark containing 820 human-crafted samples in Java, JavaScript, and Go, designed to test cross-lingual capabilities.

Key Insights and Ideas

  • Planning is Crucial for Complexity: For complex coding tasks, a planning phase is not just beneficial but imperative. LLMs can benefit from breaking down problems into sub-problems.
  • Self-Planning Emerges with Scale: The ability for LLMs to self-plan appears to be an emergent property that becomes more pronounced in larger models (e.g., 175B parameters).
  • Planning Enhances Code Quality: Beyond just correctness (Pass@1), self-planning significantly improves code readability and robustness, as confirmed by human evaluations.
  • Code CoT vs. Self-Planning: While Code CoT (using LLM-generated comments as intermediate steps) shows improvement over direct generation, self-planning (generating explicit plans) offers a more substantial advantage.
  • Multilingual Generalization: The self-planning approach shows promise across different programming languages, though customizing plans for specific languages could further enhance performance.

Key Results

  • Performance Improvement: Self-planning achieved up to a 25.4% relative improvement in Pass@1 over direct code generation and up to 11.9% over Code CoT.
  • Human Evaluation: Self-planning was rated as the most readable approach, with correctness and robustness on par with ground-truth planning.
  • Subproblem Decomposition: Self-planning generated significantly more sub-functions (37) compared to Direct (1) and Code CoT (9) on HumanEval, indicating better problem decomposition.
  • Complexity Handling: The self-planning approach showed particularly significant performance gains on higher-difficulty problems.

Limitations and Future Work

  • Prompt Crafting Sensitivity: The performance of self-planning is sensitive to the quality and selection of prompt examples. Future work could explore automated prompt generation techniques.
  • Generalizability: While tested on multiple languages, further research into language-specific plan customization or more abstract, language-agnostic planning could improve cross-lingual effectiveness.
  • Beyond Code Generation: The authors suggest that LLMs could be extended beyond code writing to assist in earlier stages of software development, such as requirements analysis, by leveraging planning and abstraction methodologies.
  • Data Efficiency: The self-planning approach is data-efficient, requiring only a few examples, which is a significant advantage over methods needing large labeled datasets.

Summary updated

BibTeX
@article{jiang2024self,
  author = {Xue Jiang and Yihong Dong and Lecheng Wang and Zheng Fang and Qiwei Shang and Ge Li and Zhi Jin and Wenpin Jiao},
  title = {{Self-Planning Code Generation with Large Language Models}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2024},
  doi = {10.1145/3672456},
}
Figure