Large language models (LLMs) have achieved impressive performance on code generation. However, for complex programming tasks, generating the correct solution in one go becomes challenging, thus some prior works have designed program repair approaches to improve code generation performance. In this work, we propose , which teaches a large language model to debug its predicted program via few-shot demonstrations. In particular, we demonstrate that  can teach the large language model to perform rubber duck debugging; i.e., without any feedback on the code correctness or error messages, the model is able to identify its mistakes by explaining the generated code in natural language.  achieves the state-of-the-art performance on several code generation benchmarks, including the Spider dataset for text-to-SQL generation, TransCoder for C++-to-Python translation, and MBPP for text-to-Python generation. On the Spider benchmark where there are no unit tests to verify the correctness of predictions,  with code explanation consistently improves the baseline by $2-3\%$, and improves the prediction accuracy on problems of the hardest label by $9\%$. On TransCoder and MBPP where unit tests are available,  improves the baseline accuracy by up to $12\%$. Meanwhile, by leveraging feedback messages and reusing failed predictions,  notably improves sample efficiency, and can match or outperform baseline models that generate more than 10$\times$ candidate programs.

§ INTRODUCTION

Code generation has been a long-standing challenge with a variety of applications, such as code synthesis from natural languages [63, 8, 2, 32], programming by examples [14, 5, 11], and code translation [44, 10]. In particular, recent large language models have demonstrated a significant leap in improvement over prior deep neural networks [8, 13, 38, 67, 57]. However, for many programming tasks, generating correct code with a single attempt is challenging. Inspired by observations that correct code is much more likely to be predicted when multiple programs are sampled from the model [8, 13, 32], one line of work has designed reranking techniques to select the best candidate from multiple samples, which typically requires tens of samples to start with [49, 65, 37, 32].

Intuitively, even for human programmers, there is no guarantee that the code written on the first try is always accurate. Instead of completely discarding the incorrect code, humans typically look into the code and investigate execution results, then make changes to resolve the implementation errors. Therefore, prior works propose deep learning techniques to repair the predicted code, which demonstrate notable performance gains on various coding tasks [20, 53, 17, 6]. However, these approaches require additional training for the code repair model. While some recent works show that large language models have potential for generating feedback messages to critique and refine their outputs for some natural language and reasoning domains [50, 35, 28, 36, 3], prior works suggest that such large language models are not yet capable of correcting code when lacking external feedback, such as unit tests or human instructions [6].

In this work, we propose , where we teach the large language model to debug its own predicted code via few-shot prompting. Without any additional model training,  instructs the model to execute the code, then generate a feedback message based on the code and its execution result. Different from prior works on utilizing human feedback for code repair, where the feedback message explains the code errors and how to fix them [6, 2],  teaches the model to identify the implementation errors via code explanation. This debugging process is reminiscent of rubber duck debugging for human programmers, where explaining the code line-by-line in natural language to a rubber duck significantly boosts debugging efficiency without expert guidance [24]. Figure <ref> illustrates the full procedure of .

We evaluate  on  in the GPT-3 model family [8].  achieves the state-of-the-art performance on different types of code generation tasks, including text-to-SQL generation, code translation and text-to-Python generation. On the Spider benchmark [63] for text-to-SQL generation where there are no unit tests in the problem description, with code explanation,  consistently improves the baseline by $2-3\%$ with different numbers of initial programs, and improves the prediction accuracy on the most complicated SQL queries by $9\%$. On both TransCoder for code translation [44] and MBPP for text-to-Python generation [2], utilizing unit tests along with code explanation boosts the accuracy by up to $12\%$, and code explanation alone without debugging also consistently improves the code translation performance by $2-3\%$. Meanwhile,  improves sample efficiency, and can match or outperform baseline models that sample more than 10$\times$ predictions. Our work indicates that besides improving their ability to generate code from scratch, teaching large language models to perform  without human guidance is another promising path to enhance coding capability and reduce the sampling cost required to accomplish challenging tasks.

for iterative debugging using a large language model. At each debugging step, the model first generates new code, then the code is executed and the model explains the code. The code explanation along with the execution results constitute the feedback message, which is then sent back to the model to perform more debugging steps. When unit tests are not available, the feedback can be purely based on code explanation.

§ PROMPTING FOR CODE GENERATION

In this section, we discuss the background on prompting for code generation using large language models. We first introduce few-shot prompting, then discuss how to select the final prediction from multiple samples based on code execution.

Few-shot prompting. Few-shot prompting aims to instruct the language model to solve a task with several input-output demonstrations [4]. Taking text-to-SQL generation as an example, the few-shot prompt prepends the question of interest with a list of (question, SQL) pairs, so that when the model is asked to predict subsequent tokens of the given prompt, it will follow the prompt format to generate the SQL query. Figure <ref> presents one exemplar for the prompt. Besides input-output demonstrations, we can optionally add an instruction in the prompt to provide a high-level task description [41, 46, 51]. For example, in the first two steps of our  prompt shown in Figure <ref>, both prompts start with instructions that ask the model to generate explanations. We provide the full few-shot prompts in the appendix.

Execution-based code selection. Prior works demonstrate that decoding multiple samples can significantly improve the performance of large language models [54, 49]. In particular, for code generation tasks, we can utilize code execution to select the final prediction [11, 32, 49, 65, 37]. One line of work selects the final prediction using the majority vote of execution results [11, 32, 49], while other works design reranking schemes to improve the performance [65, 37, 61, 64]. In this work, when there are multiple predictions, we follow the first line of work to select the predicted code with the most frequent execution result among those that do not encounter execution errors, then apply  to the code.

Some code generation tasks are accompanied with unit tests to specify the program execution behavior [8, 2, 32, 22]. Specifically, the unit tests are a set of input-output pairs $\{(i_k, o_k)\}_{k=1}^K$, and a program $P$ passes unit tests when $P(i_k)=o_k,\forall k\in \{1, ..., K\}$. When unit tests are presented in the problem description, we filter out programs that do not pass the unit tests before performing the execution-based majority vote.

§  FRAMEWORK

Figure <ref> illustrates our  framework for iterative debugging, where we utilize the pretrained large language model without finetuning. Given the problem description, the model first predicts candidate programs, then it infers the program correctness and produces feedback messages for subsequent debugging steps. The debugging process terminates when the feedback message states that the prediction is correct, or a maximum allowed number of debugging turns is reached.

Existing works demonstrate that language models can be trained to understand human feedback for code and make corrections based on the instructions [2, 38, 6]. However, it is unclear whether language models can debug by themselves without human assistance. In the following, we discuss different types of feedback messages that can be automatically acquired and generated with code execution and few-shot prompting.

§.§  with Simple Feedback

The simplest form of automatic feedback is a sentence that just indicates the code correctness without more detailed information. For instance, in text-to-SQL generation, the few-shot prompt provides the feedback message “The SQL prediction above is correct!” for all correct SQL queries, and “The SQL prediction above is wrong. Please fix the SQL.” for wrong predictions.

§.§  with Unit Tests (UT)

For code generation tasks where the problem description includes unit tests, besides utilizing code execution to check code correctness, we can also present the execution results in the feedback message, which provides richer information for debugging. Figure <ref> presents a sample unit test feedback message for code translation. Intuitively, inspecting runtime error messages and execution results of failed unit tests also helps human programmers debug more effectively. In our experiments, we will demonstrate that leveraging unit tests considerably improves the debugging performance.

§.§  via Code Explanation (Expl.)

Despite recent promising progress showing that large language models can generate critiques to avoid harmful model outputs [18, 3] and improve their performance on some natural language and reasoning tasks [50, 28, 47], prior work has yet to show the effectiveness of model-generated feedback on code generation [6]. On the other hand, large language models have been shown to be capable of describing their generated problem solutions in both text [55, 29, 68] and code [19, 9] formats.

Inspired by these observations, instead of teaching the large language model to predict error messages, we propose to teach the model to self-debug via explaining the generated code. This debugging process is reminiscent of rubber duck debugging, where a programmer debugs code by explaining it line-by-line to a rubber duck [24]. By describing the code implementation and comparing it to the problem description, human programmers are usually able to identify the bugs without extra guidance. Empirically, we observe that a large language model can also benefit from rubber duck debugging, especially when unit tests are not available.

§ APPLICATIONS

In our evaluation, we showcase applications of  to different code generation domains: text-to-SQL generation, code translation, and text-to-Python generation. First, text-to-SQL generation represents the scenario where no unit tests are available, where  enables the model to identify errors and justify the code correctness by explaining the predicted code. In code translation, we demonstrate how to utilize the execution of unit tests to provide feedback messages. Finally, the text-to-Python domain requires the model to infer the prediction correctness when only a subset of unit tests are presented in the problem description.

§.§ Text-to-SQL Generation

The goal of text-to-SQL tasks is to generate the corresponding SQL query given a question and the database information, and Figure <ref> presents an exemplar in our prompt for generating the SQL query. Following prior work evaluating large language models for text-to-SQL generation [43, 49, 37], we evaluate  on the development set of the Spider benchmark [63]. Since the unit tests are not available, it becomes more challenging for the model to infer the correctness of the predicted SQL queries. As will be shown in Section <ref>, it is insufficient for accurately predicting SQL correctness when the few-shot prompt only contains simple feedback.

Figure <ref> demonstrates an exemplar in our  prompt for text-to-SQL generation, and we present the full few-shot prompts for each step in Appendix <ref>. At a high level, the debugging process contains 3 steps. First, we prompt the model to summarize the question and infer the return type required by the question; i.e., the number of table columns of the corresponding SQL query. In the second step, we execute the SQL query and add the returned table to the model input for code explanation. The generated SQL explanation includes a detailed description of each clause, the number of columns included in the returned table, and the high-level meaning of the full SQL query. When the returned table has more than 2 rows, only the first 2 rows are included in the prompt. Finally, the model compares the inferred SQL explanation and question description, and then predicts the correctness of the current SQL query. The  process terminates when the SQL query is considered correct in step 3, or when it reaches the maximum number of debugging turns.

An example of  prompting for text-to-SQL generation. The model is required to predict the code, explanation and feedback. Database information is omitted in the figure for clarity, and we present the full prompts in Appendix <ref>.

§.§ Code Translation

Next, we apply  to code translation, where the goal is to translate code in one programming language into another language. In our experiments, we use the TransCoder dataset [44], which includes a test set of parallel functions in different programming languages along with unit tests. Following [13], we evaluate  on the task of translating C++ code to Python code, and we utilize the same test set with 560 problems and 10 unit tests per problem. The problem description includes both the C++ code and all unit tests, as shown in Figure <ref>.

Given the availability of unit tests, we apply  only when the predicted Python code does not pass all the unit tests, and thus the model does not need to predict the feedback messages. Figure <ref> provides sample  prompts with unit test feedback and code explanation. We iteratively apply  until the predicted Python code passes all unit tests, or the model reaches a maximum number of debugging turns.

Examples of  prompts for code translation. Left-aligned blocks are model predictions, and right-aligned blocks contain the input C++ code and feedback messages based on code execution. We present the full prompts in Appendix <ref>.

§.§ Text-to-Python Generation

In addition, we evaluate  in the setting where a subset of unit tests are presented in the problem description, which is a common setup in coding assignments and competitions [8, 2, 32]. Specifically, we perform experiments on the test set of MBPP [2], which contains 500 Python problems with text descriptions, where each problem has 3 unit tests. We follow prior work [49, 37] in including the first unit test in the prompt as part of the problem description, and keeping the remaining 2 unit tests hidden for full evaluation. Similar to code translation, we can also utilize the unit test execution results in the feedback message, but the main difference is that the model still needs to infer the code correctness even if the predicted Python code passes the given unit test. We utilize the same prompt as in [37] to obtain the initial Python code, and we present the full prompts for  in Appendix <ref>.

§ EXPERIMENTS

We use  [8] for all our experiments with , and we denote it as  throughout this section. For initial code generation, when starting from one program, we perform greedy decoding with temperature $\tau=0$. When sampling multiple programs for a problem, we set temperature $\tau=0.7$, then we perform execution-based selection described in Section <ref>. All experiments for  use greedy decoding to generate code explanations, feedback messages and new programs. We set the maximum number of debugging turns to be 10, and we will show that empirically the successful debugging processes mostly end within 3 turns. We present the full few-shot prompts for experiments in the appendix.

We evaluate  against two types of baselines as follows.

Models trained for the given task. The Spider benchmark contains a training set of over 10K samples, and the state-of-the-art models are mostly finetuned on this training set. We compare  to Graphix-T5 [31], T5-3B + Syn data [66] and T5-3B + N-best Reranking [64], which are adapted from the T5 architecture [42] and specially trained for text-to-SQL generation. Although LEVER [37] also utilizes  to generate candidate SQL queries, they train a verifier to select the final prediction based on execution, and thus also requires extra training. For both TransCoder and MBPP benchmarks, the state-of-the-art results are all accomplished by large language models for code, thus we mainly compare to such models.

Prompting-based approaches. We compare  against recent approaches that also only perform prompting without any additional training. In particular, Both MBR-Exec [49] and Coder-Reviewer [65] first generate multiple candidate programs by prompting the pretrained large language model. Afterward, MBR-Exec [49] selects the program with the most common execution output, while Coder-Reviewer [65] selects the program by utilizing both the likelihood of the predicted code given the problem description (Coder score) and the likelihood of the problem description given the predicted code (Reviewer score).

Accuracy on the Spider development set for text-to-SQL generation [63].

Spider (Dev)

2lw/ training

Graphix-T5 [31]     81.0

T5-3B + Syn data [66]     81.4

T5-3B + N-best Reranking [64]     80.6

LEVER [37]     81.9

2lPrompting only w/o debugging

Rajkumar et al. [43]     67.0

Coder-Reviewer [65]     74.5

MBR-Exec [49]     75.2

2lThis work

Codex     81.3

Simple     81.3

+ Expl.     84.1

Accuracy on the TransCoder dataset [44], where we evaluate on the C++ to Python translation task as in [13].  All approaches w/o debugging use greedy decoding, and  methods utilize Codex predictions for debugging.

TransCoder (pass@1)

2lw/o debugging

TransCoder [44]     44.5

PaLM [13]     51.8

PaLM-Coder [13]     55.1

2lThis work

Codex     80.4

Simple     89.3

UT     91.6

UT + Expl.     92.5

Accuracy on the MBPP dataset for text-to-Python generation [2].

Greedy decoding     $n$ samples

2lPrior work

MBR-Exec [49]     -     63.0 ($n=25$)

Reviewer [65]     -    66.9 ($n=25$)

LEVER [37]     -    68.9 ($n=100$)

2lThis work

Codex     61.4     72.2 ($n=10$)

Simple     68.2     73.6

UT     69.4     75.2

UT + Expl.     69.8     75.6

§.§ Main Results

We present the main results in Tables <ref>, <ref> and <ref>, where we compare  to the state-of-the-art approaches on the Spider, TransCoder and MBPP benchmarks. We demonstrate that  achieves the state-of-the-art on all 3 benchmarks, and significantly boosts the performance compared to the baseline that does not perform debugging.

On the Spider benchmark where unit tests are not available, simple feedback alone does not improve the performance, because the model is not able to distinguish between correct and wrong SQL queries from only a few exemplars without explanation, and does not produce meaningful changes to the initial predicted SQL queries. Note that on TransCoder and MBPP benchmarks where at least one unit test is available for , simple feedback utilizes the execution result to infer the code correctness even if the execution information is not presented in the feedback message to the model, thus simple feedback still improves the model prediction for both applications. For all tasks, the model benefits from richer feedback signals for . In particular, code explanation enables the model to self-debug without unit tests.

§.§ Ablation studies

§.§.§ Text-to-SQL Generation

Ablation studies on the Spider development set. (a) Accuracies with different numbers of initial samples. (b) Breakdown accuracies on problems with different hardness levels.

On the Spider benchmark, we observe that our Codex result before  already matches the state-of-the-art performance, which is due to our better prompt design for SQL generation. In particular, we find that specifying primary and foreign key relationships between different tables improves the generation performance. Still,  further increases the prediction accuracy over the strong baseline and outperforms prior approaches without specialized training.

Figure <ref> demonstrates the effectiveness of  when applied to different numbers of initial samples, where  notably improves the sample efficiency. In particular, applying  to predictions generated with greedy decoding matches the baseline accuracy using 16 samples, and  from 8 samples outperforms the baseline accuracy using 32 samples. Note that typically one debugging turn is sufficient, and the accuracy improvement after one turn is within $0.1\%$. These results highlight the benefit of leveraging the large language model to explain the code for debugging.

In Figure <ref>, we further show the breakdown accuracies on problems with different difficulty levels, where the difficulty of each problem is annotated in the Spider benchmark based on the complexity of the ground truth SQL queries. We observe that the improvement achieved by  is more significant on hard problems. In particular, on extra hard problems,  increases the accuracy by $9\%$. Figure <ref> presents an example where  fixes the predicted SQL query for an extra hard problem.

§.§.§ Code Translation

For generating initial Python translation, we apply the same few-shot prompt for TransCoder as [13], which consists of 3 exemplars (Appendix <ref>). From Figure <ref>, we again observe that the major improvement comes from the first debugging turn. Specifically, a single debugging turn with the full feedback improves over the greedy decoding accuracy by around $12\%$. Compared to Figure <ref>, applying  to greedy decoding outperforms the baseline accuracy with 5 samples, and is close to the baseline accuracy with 10 samples.

Meanwhile, incorporating both unit test execution and code explanation improves the debugging performance, and we present some examples in Figures <ref> and <ref>. In addition, we demonstrate that leveraging code explanation alone without  also provides a consistent performance gain of $2-3\%$ for different numbers of samples, as shown in Figure <ref>.

Ablation studies on TransCoder. (a) The accuracy of  prompts with different numbers of debugging turns. (b) The accuracy with different numbers of initial samples. Note that Codex+Expl. only prompts the model to generate explanations for the input C++ code, and does not perform .

§.§.§ Text-to-Python Generation

For initial Python code generation on MBPP, we apply the same 3-shot prompt as [37]. With greedy decoding,  increases the accuracy by over $8\%$, and leveraging both unit test execution and code explanation again consistently improves the performance.

We observe that the improvement using code explanation is less significant on MBPP. First, due to the availability of unit tests, it is much easier to recognize wrong predictions from unit test feedback. Second, due to the context length limit, the number of exemplars in the prompt for  is smaller when adding code explanation in the feedback message. Specifically, prompts with both simple and unit test feedback messages include 6 exemplars, while the prompt with both the unit test execution and code explanation has 3 exemplars. Third, we find that code explanation plays a smaller role when the initial predicted code is far from the correct solution, in which case the model simply generates a new program and does not rely on code snippets in the initial prediction. See Figure <ref> for an example where the prediction after  is very different from the initial code.

§ RELATED WORK

Language models for code. Recent years have witnessed rapid progress in deep neural networks for code generation [14, 11, 63, 44]. While models designed and trained for specialized domains have achieved impressive performance in various applications such as text-to-code generation [31, 52, 48, 15, 26] and code translation [10, 44, 45], latest work on large language models demonstrate that a single pretrained model can achieve the state-of-the-art performance across a wide variety of coding tasks without specialized finetuning [8, 13, 38, 67, 57, 1, 40].

Despite showing the remarkable ability to follow natural language instructions, large language models still exhibit limited understanding of code execution [2, 32]. Specifically, even when the unit tests are provided in the problem description, the generated programs may still violate them [32, 49, 37]. Therefore, several approaches have been proposed to leverage code execution to choose the final prediction from multiple candidates, such as utilizing or training a language model for reranking [65, 37], and performing selection based on the consensus on unit test execution outputs among samples [11, 12, 45, 49, 32, 7]. In this work, our main focus is to utilize and explain code execution for , which improves the sample efficiency compared to utilizing execution solely for initial code generation.

Prompting techniques. Several prompting methods have been proposed to unlock latent abilities of large language models [55, 39, 29, 68, 27, 69, 19, 9]. Rather than prompting a model to directly produce the desired result, these methods have the model first produce useful intermediate outputs. For example, chain-of-thought prompting asks the model to produce intermediate reasoning steps in natural language, which can be accomplished either with few-shot exemplars [55] or in a zero-shot manner [29]. Meanwhile, several prompting techniques explicitly direct the model to break down a problem into subproblems [68, 27]. For example, decomposed prompting [27] delegates subproblems to other LLM instances that are prompted to specialize in specific tasks. Our prompting format of code explanation is relevant in spirit to chain-of-thought prompting, as the line-by-line code explanation in natural language facilitates analysis of the code that is useful for the debugging task. On the other hand,  also decomposes the debugging process into several stages, and triggers separate prompts for code explanation and feedback generation.

Code repair. Program repair is an area of research concerned with fixing bugs in code, where several neural network models have been developed to handle different types of bugs [21, 60, 20, 53]. While some methods train repair models that only take the code as input [21, 60], other approaches incorporate additional information such as execution traces [20, 53] and compiler errors [59]. Our  also uses additional information to aid in code repair, including execution results and self-generated code explanations. In contrast to prior work on training a separate model for code repair,  utilizes pretrained large language models for code, and teaches the model to debug via few-shot prompting.

Training with feedback. Training with feedback to improve the outputs of large language models, both in terms of correctness and alignment with human preferences, is an active research direction nowadays [70, 30, 18, 3]. One popular technique is reinforcement learning from human feedback (RLHF) [70, 41], and RLHF-trained models have demonstrated the ability to avoid harmful outputs when instructed to do so in the prompt [18]. Constitutional AI [3] introduces another path toward training harmless models, where they use the pretrained model itself to create automated feedback for both supervised learning and RLHF: for the former, a set of principles are used to guide a language model in creating revisions of its own responses that it is then trained on, and for the latter the same principles are used to prompt a separate model for the feedback needed for RLHF.

Another line of work trains the language model to refine the initial model outputs based on external feedback on prediction quality [56, 33], which improves the performance on several natural language and reasoning tasks. For code generation, a number of works have trained models to perform code optimization [34], interact with users for multi-turn code generation [62, 58, 38], and fix generated programs based on human feedback [25, 16, 6]. On the other hand,  enables the model to generate feedback messages on its own at test time, and does not require extra training.

Prompting with feedback. Recent works have shown the great promise of RLHF-trained models to generate critiques with prompting, which reduces harmful model outputs [3, 18] and improves the performance on some reasoning tasks [50, 35, 28, 36]. Reflexion [50] prompts an agent powered with a large language model to reflect on its actions under certain situations (such as when its actions become repetitive) and stores these reflections in its memory, leading to reduced hallucination and more effective plans. Self-Refine [35] shows that having a language model iteratively generate feedback and revisions of its outputs can greatly improve the final output quality on several tasks, such as text generation and math reasoning. RCI [28] demonstrates a related method combined with state and agent grounding that allows a large language model to accomplish computer control tasks in the MiniWoB++ benchmark [23], and improves the zero-shot performance on several math and commonsense reasoning benchmarks. Finally, DERA [36] simulates a dialogue between two agents to improve the model's predictions in the clinical domain, with one agent providing feedback about important elements of the problem, and the other using the feedback to produce a final output. In this work, we focus on code generation applications, and  demonstrates the effectiveness of both self-generated feedback and unit test feedback acquired by code execution. In particular,  teaches the large language model to notice prediction errors via code explanation in the style of rubber duck debugging, which does not require the feedback message to explicitly explain the implementation errors and how to fix them.

§ CONCLUSION

In this work, we presented , which enables a large language model to debug code generated by itself. In particular, we demonstrate that  empowers the model to perform rubber duck debugging, so that the model can identify and fix the bugs without human instructions.  achieves the state-of-the-art performance across several code generation domains, and notably improves sample efficiency. On text-to-SQL generation where there is no unit test specified for the task, leveraging code explanation for  consistently improves the baseline by $2-3\%$, and provides a performance gain of $9\%$ on the hardest problems. For code translation and text-to-Python generation tasks where unit tests are available,  significantly increases the baseline accuracy by up to $12\%$.

Our work highlights the promise of improving the coding performance of large language models by teaching them to iteratively debug their own predictions, instead of requiring the model to generate the correct code from scratch.  instructs the model to understand the code, identify the errors, and follow the error messages to fix the bugs. We consider improving the model's ability to conduct all these steps as important future work. In particular, we hypothesize that better code explanation ability leads to better debugging performance, and one direction is to instruct the model to better describe the high-level semantic meaning of code along with the implementation details in its explanation. Another direction is to include additional debugging information in the model feedback, such as a description of potential bugs. Our preliminary results suggest that model-generated feedback messages on semantic errors do not provide additional benefits on top of line-by-line code explanation, and future work can explore techniques to predict more informative error messages.

§ ACKNOWLEDGEMENT

We would like to thank Jacob Austin, Quoc V. Le, Charles Sutton, Hanjun Dai, and Olivier Bousquet for helpful discussion and feedback.

[1]
B. Athiwaratkun, S. K. Gouda, Z. Wang, X. Li, Y. Tian, M. Tan, W. U. Ahmad,
S. Wang, Q. Sun, M. Shang, S. K. Gonugondla, H. Ding, V. Kumar, N. Fulton,
A. Farahani, S. Jain, R. Giaquinto, H. Qian, M. K. Ramanathan, R. Nallapati,
B. Ray, P. Bhatia, S. Sengupta, D. Roth, and B. Xiang.
Multi-lingual evaluation of code generation models.
In The Eleventh International Conference on Learning
Representations, 2023.

[2]
J. Austin, A. Odena, M. Nye, M. Bosma, H. Michalewski, D. Dohan, E. Jiang,
C. Cai, M. Terry, Q. Le, et al.
Program synthesis with large language models.
arXiv preprint arXiv:2108.07732, 2021.

[3]
Y. Bai, S. Kadavath, S. Kundu, A. Askell, J. Kernion, A. Jones, A. Chen,
A. Goldie, A. Mirhoseini, C. McKinnon, et al.
Constitutional ai: Harmlessness from ai feedback.
arXiv preprint arXiv:2212.08073, 2022.

[4]
T. Brown, B. Mann, N. Ryder, M. Subbiah, J. D. Kaplan, P. Dhariwal,
A. Neelakantan, P. Shyam, G. Sastry, A. Askell, et al.
Language models are few-shot learners.
Advances in neural information processing systems,
33:1877–1901, 2020.

[5]
R. Bunel, M. Hausknecht, J. Devlin, R. Singh, and P. Kohli.
Leveraging grammar and reinforcement learning for neural program
In International Conference on Learning Representations, 2018.

[6]
A. Chen, J. Scheurer, T. Korbak, J. A. Campos, J. S. Chan, S. R. Bowman,
K. Cho, and E. Perez.
Improving code generation by training with natural language feedback.
arXiv preprint arXiv:2303.16749, 2023.

[7]
B. Chen, F. Zhang, A. Nguyen, D. Zan, Z. Lin, J.-G. Lou, and W. Chen.
Codet: Code generation with generated tests.
In The Eleventh International Conference on Learning
Representations, 2023.

[8]
M. Chen, J. Tworek, H. Jun, Q. Yuan, H. P. d. O. Pinto, J. Kaplan, H. Edwards,
Y. Burda, N. Joseph, G. Brockman, et al.
Evaluating large language models trained on code.
arXiv preprint arXiv:2107.03374, 2021.

[9]
W. Chen, X. Ma, X. Wang, and W. W. Cohen.
Program of thoughts prompting: Disentangling computation from
reasoning for numerical reasoning tasks.
arXiv preprint arXiv:2211.12588, 2022.

[10]
X. Chen, C. Liu, and D. Song.
Tree-to-tree neural networks for program translation.
Advances in neural information processing systems, 31, 2018.

[11]
X. Chen, C. Liu, and D. Song.
Execution-guided neural program synthesis.
In International Conference on Learning Representations, 2019.

[12]
X. Chen, D. Song, and Y. Tian.
Latent execution for neural program synthesis beyond domain-specific
Advances in Neural Information Processing Systems,
34:22196–22208, 2021.

[13]
A. Chowdhery, S. Narang, J. Devlin, M. Bosma, G. Mishra, A. Roberts, P. Barham,
H. W. Chung, C. Sutton, S. Gehrmann, et al.
Palm: Scaling language modeling with pathways.
arXiv preprint arXiv:2204.02311, 2022.

[14]
J. Devlin, J. Uesato, S. Bhupatiraju, R. Singh, A.-r. Mohamed, and P. Kohli.
Robustfill: Neural program learning under noisy i/o.
In International conference on machine learning, pages
990–998. PMLR, 2017.

[15]
L. Dong and M. Lapata.
Language to logical form with neural attention.
In Proceedings of the 54th Annual Meeting of the Association for
Computational Linguistics (Volume 1: Long Papers), 2016.

[16]
A. Elgohary, S. Hosseini, and A. Hassan Awadallah.
Speak to your parser: Interactive text-to-SQL with natural language
In Proceedings of the 58th Annual Meeting of the Association for
Computational Linguistics, 2020.

[17]
C. Fu, H. Chen, H. Liu, X. Chen, Y. Tian, F. Koushanfar, and J. Zhao.
Coda: An end-to-end neural program decompiler.
In NeurIPS, 2019.

[18]
D. Ganguli, A. Askell, N. Schiefer, T. Liao, K. Lukošiūtė,
A. Chen, A. Goldie, A. Mirhoseini, C. Olsson, D. Hernandez, et al.
The capacity for moral self-correction in large language models.
arXiv preprint arXiv:2302.07459, 2023.

[19]
L. Gao, A. Madaan, S. Zhou, U. Alon, P. Liu, Y. Yang, J. Callan, and G. Neubig.
Pal: Program-aided language models.
arXiv preprint arXiv:2211.10435, 2022.

[20]
K. Gupta, P. E. Christensen, X. Chen, and D. Song.
Synthesize, execute and debug: Learning to repair for neural program
Advances in Neural Information Processing Systems,
33:17685–17695, 2020.

[21]
R. Gupta, S. Pal, A. Kanade, and S. Shevade.
Deepfix: Fixing common c language errors by deep learning.
In Proceedings of the aaai conference on artificial
intelligence, 2017.

[22]
D. Hendrycks, S. Basart, S. Kadavath, M. Mazeika, A. Arora, E. Guo, C. Burns,
S. Puranik, H. He, D. Song, and J. Steinhardt.
Measuring coding challenge competence with APPS.
In Thirty-fifth Conference on Neural Information Processing
Systems Datasets and Benchmarks Track (Round 2), 2021.

[23]
P. C. Humphreys, D. Raposo, T. Pohlen, G. Thornton, R. Chhaparia, A. Muldal,
J. Abramson, P. Georgiev, A. Santoro, and T. Lillicrap.
A data-driven approach for learning to control computers.
In International Conference on Machine Learning, pages
9466–9482. PMLR, 2022.

[24]
A. Hunt and D. Thomas.
The pragmatic programmer: from journeyman to master, 2000.

[25]
S. Iyer, I. Konstas, A. Cheung, J. Krishnamurthy, and L. Zettlemoyer.
Learning a neural semantic parser from user feedback.
In Proceedings of the 55th Annual Meeting of the Association for
Computational Linguistics (Volume 1: Long Papers), 2017.

[26]
S. Iyer, I. Konstas, A. Cheung, and L. Zettlemoyer.
Mapping language to code in programmatic context.
In Proceedings of the 2018 Conference on Empirical Methods in
Natural Language Processing, 2018.

[27]
T. Khot, H. Trivedi, M. Finlayson, Y. Fu, K. Richardson, P. Clark, and
A. Sabharwal.
Decomposed prompting: A modular approach for solving complex tasks.
arXiv preprint arXiv:2210.02406, 2022.

[28]
G. Kim, P. Baldi, and S. McAleer.
Language models can solve computer tasks.
arXiv preprint arXiv:2303.17491, 2023.

[29]
T. Kojima, S. S. Gu, M. Reid, Y. Matsuo, and Y. Iwasawa.
Large language models are zero-shot reasoners.
In A. H. Oh, A. Agarwal, D. Belgrave, and K. Cho, editors, Advances in Neural Information Processing Systems, 2022.

[30]
T. Korbak, K. Shi, A. Chen, R. Bhalerao, C. L. Buckley, J. Phang, S. R. Bowman,
and E. Perez.
Pretraining language models with human preferences.
arXiv preprint arXiv:2302.08582, 2023.

[31]
J. Li, B. Hui, R. Cheng, B. Qin, C. Ma, N. Huo, F. Huang, W. Du, L. Si, and
Y. Li.
Graphix-t5: Mixing pre-trained transformers with graph-aware layers
for text-to-sql parsing.
arXiv preprint arXiv:2301.07507, 2023.

[32]
Y. Li, D. Choi, J. Chung, N. Kushman, J. Schrittwieser, R. Leblond, T. Eccles,
J. Keeling, F. Gimeno, A. Dal Lago, et al.
Competition-level code generation with alphacode.
Science, 378(6624):1092–1097, 2022.

[33]
H. Liu, C. Sferrazza, and P. Abbeel.
Chain of hindsight aligns language models with feedback.
arXiv preprint arXiv:2302.02676, 2023.

[34]
A. Madaan, A. Shypula, U. Alon, M. Hashemi, P. Ranganathan, Y. Yang, G. Neubig,
and A. Yazdanbakhsh.
Learning performance-improving code edits.
arXiv preprint arXiv:2302.07867, 2023.

[35]
A. Madaan, N. Tandon, P. Gupta, S. Hallinan, L. Gao, S. Wiegreffe, U. Alon,
N. Dziri, S. Prabhumoye, Y. Yang, et al.
Self-refine: Iterative refinement with self-feedback.
arXiv preprint arXiv:2303.17651, 2023.

[36]
V. Nair, E. Schumacher, G. Tso, and A. Kannan.
Dera: Enhancing large language model completions with dialog-enabled
resolving agents.
arXiv preprint arXiv:2303.17071, 2023.

[37]
A. Ni, S. Iyer, D. Radev, V. Stoyanov, W.-t. Yih, S. I. Wang, and X. V. Lin.
Lever: Learning to verify language-to-code generation with execution.
arXiv preprint arXiv:2302.08468, 2023.

[38]
E. Nijkamp, B. Pang, H. Hayashi, L. Tu, H. Wang, Y. Zhou, S. Savarese, and
C. Xiong.
Codegen: An open large language model for code with multi-turn
program synthesis.
In The Eleventh International Conference on Learning
Representations, 2023.

[39]
M. Nye, A. J. Andreassen, G. Gur-Ari, H. Michalewski, J. Austin, D. Bieber,
D. Dohan, A. Lewkowycz, M. Bosma, D. Luan, et al.
Show your work: Scratchpads for intermediate computation with
language models.
arXiv preprint arXiv:2112.00114, 2021.

[40]
G. Orlanski, K. Xiao, X. Garcia, J. Hui, J. Howland, J. Malmaud, J. Austin,
R. Singh, and M. Catasta.
Measuring the impact of programming language distribution.
arXiv preprint arXiv:2302.01973, 2023.

[41]
L. Ouyang, J. Wu, X. Jiang, D. Almeida, C. Wainwright, P. Mishkin, C. Zhang,
S. Agarwal, K. Slama, A. Gray, J. Schulman, J. Hilton, F. Kelton, L. Miller,
M. Simens, A. Askell, P. Welinder, P. Christiano, J. Leike, and R. Lowe.
Training language models to follow instructions with human feedback.
In A. H. Oh, A. Agarwal, D. Belgrave, and K. Cho, editors, Advances in Neural Information Processing Systems, 2022.

[42]
C. Raffel, N. Shazeer, A. Roberts, K. Lee, S. Narang, M. Matena, Y. Zhou,
W. Li, and P. J. Liu.
Exploring the limits of transfer learning with a unified text-to-text
The Journal of Machine Learning Research, 21(1):5485–5551,

[43]
N. Rajkumar, R. Li, and D. Bahdanau.
Evaluating the text-to-sql capabilities of large language models.
arXiv preprint arXiv:2204.00498, 2022.

[44]
B. Roziere, M.-A. Lachaux, L. Chanussot, and G. Lample.
Unsupervised translation of programming languages.
Advances in Neural Information Processing Systems,
33:20601–20611, 2020.

[45]
B. Roziere, J. Zhang, F. Charton, M. Harman, G. Synnaeve, and G. Lample.
Leveraging automated unit tests for unsupervised code translation.
In International Conference on Learning Representations, 2022.

[46]
V. Sanh, A. Webson, C. Raffel, S. Bach, L. Sutawika, Z. Alyafeai, A. Chaffin,
A. Stiegler, A. Raja, M. Dey, M. S. Bari, C. Xu, U. Thakker, S. S. Sharma,
E. Szczechla, T. Kim, G. Chhablani, N. Nayak, D. Datta, J. Chang, M. T.-J.
Jiang, H. Wang, M. Manica, S. Shen, Z. X. Yong, H. Pandey, R. Bawden,
T. Wang, T. Neeraj, J. Rozen, A. Sharma, A. Santilli, T. Fevry, J. A. Fries,
R. Teehan, T. L. Scao, S. Biderman, L. Gao, T. Wolf, and A. M. Rush.
Multitask prompted training enables zero-shot task generalization.
In International Conference on Learning Representations, 2022.

[47]
W. Saunders, C. Yeh, J. Wu, S. Bills, L. Ouyang, J. Ward, and J. Leike.
Self-critiquing models for assisting human evaluators.
arXiv preprint arXiv:2206.05802, 2022.

[48]
T. Scholak, N. Schucher, and D. Bahdanau.
PICARD: Parsing incrementally for constrained auto-regressive
decoding from language models.
In Proceedings of the 2021 Conference on Empirical Methods in
Natural Language Processing, 2021.

[49]
F. Shi, D. Fried, M. Ghazvininejad, L. Zettlemoyer, and S. I. Wang.
Natural language to code translation with execution.
In Proceedings of the 2022 Conference on Empirical Methods in
Natural Language Processing, 2022.

[50]
N. Shinn, B. Labash, and A. Gopinath.
Reflexion: an autonomous agent with dynamic memory and
arXiv preprint arXiv:2303.11366, 2023.

[51]
M. Suzgun, N. Scales, N. Schärli, S. Gehrmann, Y. Tay, H. W. Chung,
A. Chowdhery, Q. V. Le, E. H. Chi, D. Zhou, and J. Wei.
Challenging big-bench tasks and whether chain-of-thought can solve
arXiv preprint arXiv:2210.09261, 2022.

[52]
B. Wang, R. Shin, X. Liu, O. Polozov, and M. Richardson.
RAT-SQL: Relation-aware schema encoding and linking for
text-to-SQL parsers.
In Proceedings of the 58th Annual Meeting of the Association for
Computational Linguistics, 2020.

[53]
K. Wang, R. Singh, and Z. Su.
Dynamic neural program embedding for program repair.
In International Conference on Learning Representations, 2018.

[54]
X. Wang, J. Wei, D. Schuurmans, Q. V. Le, E. H. Chi, S. Narang, A. Chowdhery,
and D. Zhou.
Self-consistency improves chain of thought reasoning in language
In The Eleventh International Conference on Learning
Representations, 2023.

[55]
J. Wei, X. Wang, D. Schuurmans, M. Bosma, E. Chi, Q. Le, and D. Zhou.
Chain of thought prompting elicits reasoning in large language
In NeurIPS, 2022.

[56]
S. Welleck, X. Lu, P. West, F. Brahman, T. Shen, D. Khashabi, and Y. Choi.
Generating sequences by learning to self-correct.
In The Eleventh International Conference on Learning
Representations, 2023.

[57]
F. F. Xu, U. Alon, G. Neubig, and V. J. Hellendoorn.
A systematic evaluation of large language models of code.
In Proceedings of the 6th ACM SIGPLAN International Symposium on
Machine Programming, pages 1–10, 2022.

[58]
Z. Yao, Y. Su, H. Sun, and W.-t. Yih.
Model-based interactive semantic parsing: A unified framework and a
text-to-SQL case study.
In Proceedings of the 2019 Conference on Empirical Methods in
Natural Language Processing and the 9th International Joint Conference on
Natural Language Processing (EMNLP-IJCNLP), 2019.

[59]
M. Yasunaga and P. Liang.
Graph-based, self-supervised program repair from diagnostic feedback.
In International Conference on Machine Learning, pages
10799–10808. PMLR, 2020.

[60]
M. Yasunaga and P. Liang.
Break-it-fix-it: Unsupervised learning for program repair.
In International Conference on Machine Learning, pages
11941–11952. PMLR, 2021.

[61]
P. Yin and G. Neubig.
Reranking for neural semantic parsing.
In Proceedings of the 57th Annual Meeting of the Association for
Computational Linguistics, 2019.

[62]
T. Yu, R. Zhang, H. Er, S. Li, E. Xue, B. Pang, X. V. Lin, Y. C. Tan, T. Shi,
Z. Li, Y. Jiang, M. Yasunaga, S. Shim, T. Chen, A. Fabbri, Z. Li, L. Chen,
Y. Zhang, S. Dixit, V. Zhang, C. Xiong, R. Socher, W. Lasecki, and D. Radev.
CoSQL: A conversational text-to-SQL challenge towards
cross-domain natural language interfaces to databases.
In Proceedings of the 2019 Conference on Empirical Methods in
Natural Language Processing and the 9th International Joint Conference on
Natural Language Processing (EMNLP-IJCNLP), 2019.

[63]
T. Yu, R. Zhang, K. Yang, M. Yasunaga, D. Wang, Z. Li, J. Ma, I. Li, Q. Yao,
S. Roman, Z. Zhang, and D. Radev.
Spider: A large-scale human-labeled dataset for complex and
cross-domain semantic parsing and text-to-SQL task.
In Proceedings of the 2018 Conference on Empirical Methods in
Natural Language Processing, 2018.

[64]
L. Zeng, S. H. K. Parthasarathi, and D. Hakkani-Tur.
N-best hypotheses reranking for text-to-sql systems.
arXiv preprint arXiv:2210.10668, 2022.

[65]
T. Zhang, T. Yu, T. B. Hashimoto, M. Lewis, W.-t. Yih, D. Fried, and S. I.
Coder reviewer reranking for code generation.
arXiv preprint arXiv:2211.16490, 2022.

[66]
Y. Zhao, J. Jiang, Y. Hu, W. Lan, H. Zhu, A. Chauhan, A. H. Li, L. Pan,
J. Wang, C.-W. Hang, S. Zhang, M. Dong, J. Lilien, P. Ng, Z. Wang,
V. Castelli, and B. Xiang.
Importance of synthesizing high-quality data for text-to-SQL
In NeurIPS 2022 Workshop on Synthetic Data for Empowering ML
Research, 2022.

[67]
Q. Zheng, X. Xia, X. Zou, Y. Dong, S. Wang, Y. Xue, Z. Wang, L. Shen, A. Wang,
Y. Li, T. Su, Z. Yang, and J. Tang.
Codegeex: A pre-trained model for code generation with multilingual
evaluations on humaneval-x, 2023.

[68]
D. Zhou, N. Schärli, L. Hou, J. Wei, N. Scales, X. Wang, D. Schuurmans,
C. Cui, O. Bousquet, Q. V. Le, and E. H. Chi.
Least-to-most prompting enables complex reasoning in large language
In The Eleventh International Conference on Learning
Representations, 2023.

[69]
H. Zhou, A. Nova, H. Larochelle, A. Courville, B. Neyshabur, and H. Sedghi.
Teaching algorithmic reasoning via in-context learning.
arXiv preprint arXiv:2211.09066, 2022.

[70]
D. M. Ziegler, N. Stiennon, J. Wu, T. B. Brown, A. Radford, D. Amodei,
P. Christiano, and G. Irving.
Fine-tuning language models from human preferences.
arXiv preprint arXiv:1909.08593, 2019.

§ TEXT-TO-SQL GENERATION

§.§ Baseline Prompt (5-shot)

§.§ Simple Feedback Prompt (9-shot)

§.§ Explanation Feedback Prompt (9-shot)

§.§ Prompt for Question Explanation

§.§ Prompt for SQL Explanation

§ CODE TRANSLATION

§.§ Baseline Prompt (3-shot)

The prompt for generating the initial Python translation is taken from [13]. We set temperature $\tau=0.8$ as in [13].

§.§ Baseline + Expl. Prompt (3-shot)

§.§ Simple Feedback Prompt (2-shot)

§.§ Unit Test Feedback Prompt (2-shot)

§.§ Unit Test + Expl. Feedback Prompt (2-shot)

§ TEXT-TO-PYTHON GENERATION

§.§ Baseline Prompt (3-shot)

The prompt for generating initial Python code is taken from [37].

§.§ Simple Feedback Prompt (6-shot)

§.§ Unit Test Feedback Prompt (6-shot)

§.§ Unit Test + Expl. Feedback Prompt (3-shot)