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

Retrieval Augmented Code Generation and Summarization

Md. Rizwan Parvez, Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang

Abstract

Automated code generation and summarization are critical tasks in software engineering that often suffer from model hallucinations and poor logic handling in standalone generative models. This paper introduces REDCODER (Retrieval-Augmented Code Generation and Summarization), a framework that integrates a dense retrieval system with a generative Transformer. By mimicking the human developer's workflow of consulting external repositories like GitHub, REDCODER leverages a "search engine" for programming to significantly improve performance. Evaluations on benchmarks like CodeXGLUE and Concode demonstrate that REDCODER outperforms state-of-the-art standalone models, providing more accurate code and higher-quality documentation.

Introduction

Modern software development is rarely done in isolation. Developers frequently leverage platforms like GitHub and StackOverflow to find existing solutions, which they then adapt to their specific needs. However, most AI models for code generation and summarization rely exclusively on their internal parameters learned during pre-training. This often leads to "hallucinations"—generating syntactically correct but logically flawed code—especially when dealing with complex logic or long contexts.

The core challenge is bridging the gap between a model's internal knowledge and the vast amount of high-quality code available in open-source repositories. To address this, researchers developed REDCODER, a framework that provides AI models with a structured way to access and utilize external "memory" during the generation process.

Methodology: The REDCODER Framework

REDCODER operates through a two-stage process: retrieval and generation.

Step 1: Retrieval (SCODE-R)

Instead of relying on traditional keyword-based search (like BM25), REDCODER utilizes a Dense Passage Retriever (DPR). This component employs two separate encoders based on GraphCodeBERT to map both the natural language query and the database entries (code snippets or summaries) into a shared vector space. This semantic mapping allows the system to identify relevant code even when variable names or specific keywords differ from the query.

Step 2: Generation (SCODE-G)

The generation phase uses a sequence-to-sequence Transformer model, specifically PLBART. The input to this model is an "augmented" sequence consisting of the original query concatenated with the top-$k$ retrieved candidates from the first step.

The two-step REDCODER framework: Retrieval of relevant code followed by Generation of the target output.

A significant innovation is REDCODER-EXT, which implements "bimodal" retrieval. When generating code, the system retrieves not only similar code snippets but also their associated summaries. This provides the generator with richer context regarding the intent and functionality of the retrieved examples.

Detail of how retrieved codes and summaries are ranked and concatenated to form the input for the code generator.

Experimental Evaluation

The framework was tested on several industry-standard benchmarks:

  • CodeXGLUE: Evaluated for both code generation and summarization in Java and Python.
  • Concode: A Java dataset where inputs include environment variables and class methods, requiring more complex context handling.
  • Retrieval Databases: The authors constructed massive indices containing approximately 1.4 million functions for Java and Python, sourced from GitHub and the CCSD corpus.

Key Results

REDCODER showed substantial performance gains across all tasks:

  • Code Generation (Concode): The Exact Match (EM) score improved from 18.6 (standalone PLBART) to 23.4.
  • Code Generation (CodeXGLUE): In Java, the BLEU score saw a dramatic increase from 10.10 to 28.98 using the full REDCODER-EXT suite.
  • Code Summarization: In Python, the BLEU-4 score rose from 19.30 to 21.01.

Human evaluations further validated these results. Ph.D. students in a blind study rated REDCODER's outputs as comparable to human-written code in terms of relevance, similarity, and compilability.

Discussion and Insights

One critical finding was that REDCODER does not simply "copy-paste" from the database. In experiments where the target code was intentionally removed from the search index, the model still outperformed baselines. This indicates that the model learns to adapt the logic of retrieved examples to the specific requirements of the query.

Furthermore, the dense retriever (SCODE-R) proved superior to lexical search, suggesting that capturing the semantic "intent" of code is more effective than simple token matching. The bimodal approach (using both code and summaries) also provided a consistent performance boost, acting as a bridge between natural language and implementation.

Conclusion and Future Work

REDCODER demonstrates the power of retrieval-augmented generation for programming tasks. However, limitations remain, such as the 512-token input bottleneck of the PLBART generator, which limits the number of retrieved examples that can be processed. Future research may focus on end-to-end training of the retriever and generator, as well as extending the framework to tasks like code-to-code translation.

Summary updated

BibTeX
@inproceedings{parvez2021retrieval,
  author = {Md. Rizwan Parvez and Wasi Uddin Ahmad and Saikat Chakraborty and Baishakhi Ray and Kai-Wei Chang},
  title = {{Retrieval Augmented Code Generation and Summarization}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.findings-emnlp.232},
}
Figure