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

Grounding Data Science Code Generation with Input-Output Specifications

Yeming Wen, Pengcheng Yin, Kensen Shi, Henryk Michalewski, Swarat Chaudhuri, Alex PolozovarXiv 2024 · DOI

TL;DR

This paper introduces GIFT4CODE, a novel method for fine-tuning Large Language Models (LLMs) to generate data science code that accurately adheres to both natural language prompts and explicit input-output (I/O) specifications. By leveraging synthetic data derived from LLM-generated code execution, GIFT4CODE significantly improves the LLM's ability to produce correct and usable code for complex data science tasks.

What Problem Does This Paper Address?

Large Language Models (LLMs) have shown impressive capabilities in generating code from natural language (NL) descriptions. However, in real-world scenarios, especially in data science, NL prompts are often too ambiguous to fully capture the programmer's intent. This ambiguity becomes particularly problematic when dealing with complex data structures like Pandas DataFrames or PyTorch Tensors, where precise input-output (I/O) specifications are crucial for correctness. Existing LLMs struggle to reconcile these I/O specifications with the NL prompt, leading to code that might be syntactically correct but semantically flawed or non-compliant with user requirements. This necessitates manual debugging and correction by developers, diminishing the utility of LLM-generated code.

Methods

The core of the proposed solution is GIFT4CODE (Grounding Instruction Fine-Tuning for Code). This approach focuses on instruction fine-tuning LLMs to better understand and utilize I/O specifications. The key innovation lies in the generation of synthetic training data:

  1. Synthetic Data Generation:
    • Programmatic Contexts: The process starts by mining real-world code repositories (e.g., Jupyter notebooks) to extract programmatic contexts, such as DataFrame initializations.
  2. NL Intent Generation: A "generalist" LLM (like PALM2) is used to generate natural language questions (intents) based on these contexts. This is done using few-shot demonstrations to ensure diversity and relevance.
  3. Code Solution Generation: The target code LLM (the one being improved) is then prompted to generate multiple candidate code solutions for each NL intent, given the programmatic context.
  4. Execution-Derived I/O Specifications: This is the crucial step. The generated code solutions are executed. From these executions, I/O specifications are derived. These specifications can range from simple variable type descriptions to concrete input-output examples, or even natural language summaries of salient variables. These derived specifications are then appended to the original NL intents.
  5. Instruction Fine-Tuning: The code LLM is then fine-tuned on this synthetic dataset, where each example consists of a programmatic context, an NL intent augmented with I/O specifications, and the corresponding correct code solution. This fine-tuning process teaches the LLM to ground its code generation not just in the NL prompt but also in the concrete I/O constraints.

Datasets

The paper evaluates GIFT4CODE on two challenging data science benchmarks:

  • ARCADE (Yin et al., 2022): A benchmark for natural language to code generation in interactive data science notebooks, focusing on Pandas operations. The dataset was augmented with programmatic contexts scraped from public Jupyter notebooks.
  • DS-1000 (Lai et al., 2022): A benchmark of data science problems sourced from Stack Overflow, featuring a wider variety of I/O types (NumPy, SciPy, PyTorch, TensorFlow).

Key Insights and Ideas

  • Execution is Key for Grounding: The most significant insight is that using execution results to derive I/O specifications is a powerful way to create high-quality synthetic training data. This moves beyond simply generating text-based examples and grounds the LLM's learning in the actual behavior of the code.
  • I/O Specifications Reduce Ambiguity: For complex data science tasks, NL prompts alone are insufficient. Explicit I/O specifications, even when derived synthetically, are essential for guiding LLMs towards correct solutions.
  • Different Levels of Specification Matter: The paper explores various types of I/O specifications (type descriptions, concrete examples, NL summaries), showing that more detailed specifications generally lead to better performance, especially in challenging "no-context" scenarios.
  • Synthetic Data Can Outperform Real Data (in some aspects): While the paper uses existing benchmarks, the core contribution is the generation of synthetic data. This approach bypasses the need for extensive manual annotation of real-world I/O-specification-augmented code.
  • Execution Rate vs. Correctness: The paper highlights that a high execution rate for generated code doesn't guarantee correctness. Models can produce executable code that still fails to meet semantic requirements, underscoring the need for I/O specifications.

Key Results

  • Significant Improvements: GIFT4CODE consistently outperforms baseline models and few-shot prompting methods across both ARCADE and DS-1000 benchmarks.
  • ARCADE: With I/O Summary specifications, GIFT4CODE achieved a pass@20 of 55.47, a substantial improvement over the base LLM's 46.94.
  • DS-1000: Similar gains were observed on DS-1000, with I/O Summary yielding the best results.
  • Qualitative Analysis: Examples show that GIFT4CODE successfully generates code that adheres to complex I/O specifications, avoiding errors like KeyErrors or syntax errors that plague other methods.
  • No Context Advantage: The benefits of I/O specifications are particularly pronounced in "no-context" scenarios, where the LLM has less information to rely on.

Limitations and Future Work

  • Generalist LLM Dependency: The generation of synthetic data relies on a capable "generalist" LLM (like PALM2). The quality of the synthetic data is thus dependent on the capabilities of this LLM.
  • Synthetic Data Quality: While execution-derived feedback improves quality, the initial generation of code solutions by the LLM can still contain errors or be suboptimal. The paper mentions filtering for executability and API diversity, but further refinements might be possible.
  • Computational Cost: Generating synthetic data, especially with execution, can be computationally intensive.
  • Future Work: The authors suggest that the GIFT4CODE methodology is generalizable to other domains requiring specifications for precise task descriptions. Further exploration into more sophisticated I/O specification types and their impact on LLM performance is also a natural next step.

Summary updated

BibTeX
@misc{wen2024grounding,
  author = {Yeming Wen and Pengcheng Yin and Kensen Shi and Henryk Michalewski and Swarat Chaudhuri and Alex Polozov},
  title = {{Grounding Data Science Code Generation with Input-Output Specifications}},
  year = {2024},
  eprint = {2402.08073},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.08073},
}
Figure