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

Incorporating External Knowledge through Pre-training for Natural Language to Code Generation

Frank F. Xu, Zhengbao Jiang, Pengcheng Yin, Bogdan Vasilescu, Graham Neubig

Introduction

Generating executable code from natural language (NL) intents—such as "choose a random file from a directory"—is a fundamental challenge in semantic parsing. While modern AI models excel in restricted domain-specific languages, open-domain generation for general-purpose languages like Python remains difficult. The primary bottleneck is the scarcity of high-quality, human-annotated datasets. Furthermore, models often lack "API literacy," struggling to correctly utilize library functions and their arguments despite maintaining syntactic correctness. This research proposes that AI models should leverage the same external resources human developers use: StackOverflow and official API documentation.

Methodology

The researchers introduce a two-stage training pipeline consisting of large-scale pre-training on noisy external data followed by fine-tuning on high-quality, human-curated data.

Overview of the proposed training pipeline incorporating external knowledge resources.

External Knowledge Integration

Two primary sources of external knowledge are utilized:

  1. Mined Pairs (StackOverflow): 100,000 pairs of NL questions and code snippets were automatically mined. While these represent real-world usage, they are often noisy.
  2. API Documentation: Approximately 13,000 pairs were parsed from the Python 3.7 standard library documentation. These provide clean, authoritative examples but suffer from a distributional shift; documentation covers all functions uniformly, whereas developers focus on a popular subset.

Retrieval-based Re-sampling

To address the distributional shift in API documentation, the authors implemented a retrieval-based re-sampling strategy. Using the BM25 algorithm, they identified documentation segments that most closely aligned with real-world queries from StackOverflow and human-annotated datasets. By re-sampling the documentation to favor these high-utility API calls—while using temperature smoothing to retain exposure to rarer functions—the model learns to prioritize the most relevant knowledge.

Results and Analysis

The proposed approach was evaluated on the CoNaLa benchmark using the TranX transition-based model as a baseline.

BLEU score performance across different data strategies and methods.

The results demonstrate significant improvements:

  • Performance Gains: Integrating StackOverflow data and re-sampled API documentation raised the BLEU score from a baseline of 30.11 to 32.26.
  • Fine-grained Accuracy: API call accuracy increased from 31.5% to 36.8%, and variable name accuracy improved from 41.2% to 43.0%.
  • Qualitative Improvements: The model showed enhanced "argument placement" logic (e.g., correctly identifying parameters in functions like open()) and a reduced tendency to "hallucinate" non-existent APIs.

Crucially, these data-centric improvements were found to be orthogonal to architectural enhancements like hypothesis reranking, suggesting that this training strategy can provide cumulative benefits when combined with other model improvements.

Conclusion

By aligning "dry" official documentation with the distribution of real-world developer queries, this research successfully bridges the gap between theoretical API knowledge and practical application. Future work will explore zero-shot scenarios for unseen libraries and the use of execution-based evaluation metrics to further refine code generation capabilities.

Summary updated

BibTeX
@inproceedings{xu2020incorporating,
  author = {Frank F. Xu and Zhengbao Jiang and Pengcheng Yin and Bogdan Vasilescu and Graham Neubig},
  title = {{Incorporating External Knowledge through Pre-training for Natural Language to Code Generation}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2020},
  doi = {10.18653/v1/2020.acl-main.538},
}
Figure