Abstract
Chain-of-thought (CoT) has emerged as a groundbreaking tool in NLP, notably for its efficacy in complex reasoning tasks, such as mathematical proofs. However, its application in code generation faces a distinct challenge, i.e., although the code generated with CoT reasoning is logically correct, it faces the problem of syntax error (e.g., invalid syntax error report) during code execution, which causes the CoT result's pass@1 in HumanEval even lower than the zero-shot result. In this paper, we present Code Chain-of-Thought (CodeCoT) that integrates CoT with a self-examination process for code generation. CodeCoT begins with the LLMs using CoT for initial code development to ensure the generated code follows the correct logic flow. Then, CodeCoT will generate test cases to validate whether the code has syntax errors during the execution. CodeCoT then employs a self-examination phase, in which the generated code is executed against these test cases in the local environment. If the local environment raises error information (e.g., invalid syntax error), CodeCoT will iteratively refine the code based on the feedback information. Within this loop, CodeCoT can make sure their generated codes not only follow the logic flow of the code description, but the syntax error will also be addressed with the self-examination process. Our evaluation results reveal that CodeCoT improves the effectiveness of code generation. For example, CodeCoT increases pass@1 from 75.6% to 79.3% for the HumanEval dataset.
Introduction
Chain-of-Thought (CoT) prompting has revolutionized how Large Language Models (LLMs) handle complex reasoning by encouraging step-by-step thinking. While highly successful in mathematics and logic, its application to code generation reveals a significant bottleneck: the "right but unrunnable" problem. Models often produce logically sound algorithms that fail to execute due to minor syntax errors—mismatched brackets, indentation issues, or incorrect API calls. Surprisingly, standard CoT can sometimes underperform compared to zero-shot prompting because the narrative reasoning process introduces more opportunities for formatting mistakes. CodeCoT addresses this by bridging the gap between natural language reasoning and strict executable syntax.
Methodology: The CodeCoT Framework
CodeCoT introduces a closed-loop system that integrates reasoning with execution-based verification. The framework operates in four distinct stages:
- CoT Prompting: The model is provided with a task description and structured examples to guide its logical flow.
- Code Generation: The LLM generates an initial code draft based on the CoT logic.
- Test Case Generation: Simultaneously, the model generates a set of test cases (inputs and expected outputs) to validate its own code.
- Self-Examination: This is the core innovation. The code is executed against the generated test cases in a local environment. If an error occurs (e.g.,
SyntaxError), the feedback is returned to the model. The model then iteratively refines the code over several steps (typically up to 5) until it passes or the limit is reached.
Experimental Setup
The effectiveness of CodeCoT was evaluated across four major Python benchmarks:
- HumanEval: 164 hand-written programming problems.
- MBPP (Mostly Basic Python Problems): Approximately 500 entry-level tasks.
- HumanEval-ET & MBPP-ET: Enhanced versions of the standard benchmarks with more rigorous test suites to ensure true correctness.
Results and Analysis
CodeCoT consistently outperforms baseline ChatGPT and other advanced prompting strategies like ReAct, Reflexion, and ToT.
Key findings include:
- Accuracy Gains: On HumanEval, CodeCoT improved pass@1 accuracy from 75.6% to 79.3%. On MBPP, the improvement was even more dramatic, jumping from 69.8% to 89.5%.
- Robustness: The framework excels on "Enhanced Test" (ET) datasets, achieving 69.5% on HumanEval-ET, significantly higher than the 56.1% baseline.
- Syntax Error Reduction: Iteration is key. Moving from 1 to 5 steps of self-examination reduced syntax error rates on HumanEval from 27% to just 2%.
- Model Synergy: Interestingly, the CoT prompt improves the quality of the generated test cases themselves, creating a virtuous cycle for the self-examination phase.
Discussion and Limitations
While CodeCoT sets a new standard for reliable code generation, it comes with trade-offs. The iterative nature increases computational costs and latency due to multiple LLM calls. Furthermore, the system's success is tied to the model's ability to generate accurate test cases; "hallucinated" test cases could lead the model to "fix" correct code into an incorrect state. Future research should focus on extending this framework to other programming languages and optimizing the efficiency of the self-correction loop.
Conclusion
CodeCoT demonstrates that integrating execution feedback into the reasoning process is essential for high-quality code generation. By treating the compiler as a collaborator, LLMs can overcome the syntax hurdles that previously limited the utility of Chain-of-Thought reasoning in software development.
Summary updated