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

Python Code Generation by Asking Clarification Questions

Haau-Sing Li, Mohsen Mesgar, André F. T. Martins, Iryna Gurevych

Introduction

Current text-to-code models often struggle with under-specified natural language descriptions (NLDs). When a user provides a vague prompt like "Do a logistic regression," a model must guess whether to include grid searches, model serialization, or specific hyperparameters. This ambiguity often results in code that is syntactically valid but fails to meet the user's actual intent. To address this, we introduce a framework that enables models to behave like human pair-programmers by identifying missing information and asking clarification questions (CQs).

Figure 1: An example of the clarification dialogue. The model identifies missing operations (GridSearchCV and joblib.dump) and asks the user for confirmation before generating the final code.

Methodology: Identifying Missing Operations

The core of our approach is a pipeline designed to detect "key operations"—API calls essential to the code's logic—that are absent from the initial user prompt.

  1. Key Operation Identification: We represent code as a data flow graph using the GraphGen4Code toolkit to isolate core API calls.
  2. Schema Alignment: We extract "schemas" (comprising verbs, key-phrases, and relations) from both the code documentation and the user's NLD. Using Sentence-BERT, we calculate similarity scores between these schemas. If the similarity falls below a threshold, the operation is flagged as missing.
  3. Synthetic CQ Generation: For each missing operation, the system generates a question using predefined templates, offering either multiple-choice or yes/no options.
Figure 3: The pipeline for identifying missing key operations by aligning NLD schemas with code operation schemas.

The CodeClarQA Dataset

To train and evaluate this framework, we developed CodeClarQA, a dataset derived from the notebookCDG collection of Jupyter notebooks. It consists of approximately 19,368 NLD-code samples focused on data science libraries such as pandas and scikit-learn. The dataset includes synthetic clarification questions and answers, validated through human annotation to ensure the accuracy of "missingness" labels.

Experimental Results

Our evaluation demonstrates that interactivity significantly boosts model performance. When provided with "oracle" (perfect) clarifications, models like CodeT5 saw their BLEU scores increase from 27.03 to 39.13. Similarly, PLBART achieved a notable jump in Exact Match (EM) rates.

In an end-to-end setting, the system employs a three-stage pipeline:

  • Clarification Need Predictor: Decides if a prompt requires clarification.
  • CQ Ranker: Selects the most relevant questions from a candidate pool.
  • Code Generator: Produces the final code based on the prompt and the resulting Q&A.

Conclusion and Future Work

While the current framework effectively handles missing API calls, challenges remain in addressing missing arguments and complex logic. Future work will focus on expanding the system to handle multi-turn conversations and broader programming domains beyond data science. By moving from static generation to interactive dialogue, we can create more robust and reliable AI coding assistants.

Summary updated

BibTeX
@inproceedings{li2023python,
  author = {Haau-Sing Li and Mohsen Mesgar and André F. T. Martins and Iryna Gurevych},
  title = {{Python Code Generation by Asking Clarification Questions}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-long.799},
}
Figure