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

RAP-Gen: Retrieval-Augmented Patch Generation with CodeT5 for Automatic Program Repair

Weishi Wang, Yue Wang, Shafiq Joty, Steven C. H. Hoi · DOI

Abstract

Automatic Program Repair (APR) has evolved from heuristic-based systems to sophisticated Deep Learning (DL) models. However, standard parametric models often struggle to generalize across the vast search space of potential bugs. This paper introduces RAP-Gen (Retrieval-Augmented Patch Generation), a framework that combines the generative power of CodeT5 with a hybrid retriever. By providing the model with a "cheat sheet" of similar, previously fixed bugs, RAP-Gen achieves state-of-the-art performance across multiple programming languages and benchmarks.

Introduction

The "holy grail" of software engineering is the fully automated identification and repair of software vulnerabilities. While recent shifts toward Deep Learning have improved results, these models are typically parametric—meaning their knowledge is entirely contained within their trained weights. This creates a bottleneck when encountering rare or complex bug patterns.

RAP-Gen is built on the "redundancy assumption": the observation that many software bugs are not unique and have likely been resolved in similar forms elsewhere in a codebase or across open-source history. By shifting from a purely generative approach to a retrieval-augmented one, RAP-Gen allows the model to explicitly search a database of past fixes to guide its reasoning.

The RAP-Gen Framework

RAP-Gen utilizes a two-stage pipeline designed to optimize both the search for relevant context and the generation of the final patch.

1. Hybrid Patch Retriever

To identify the most relevant fix patterns, the system employs a dual-search strategy:

  • BM25 (Lexical Search): A keyword-based approach that identifies similar syntax and variable names.
  • Dense Passage Retriever (DPR): A semantic search mechanism using a CodeT5 encoder trained via contrastive learning. This allows the system to find code that is functionally similar even when the naming conventions differ.

2. CodeT5 Patch Generator

Once the most relevant bug-fix pair is retrieved, it is concatenated with the current buggy code into a single prompt. This context is fed into CodeT5, an encoder-decoder model pretrained on millions of lines of code. By observing a "before and after" example of a similar repair, the generator can distill the underlying logic—such as specific error-handling patterns or conditional checks—and apply it to the new problem.

Experimental Evaluation

The researchers evaluated RAP-Gen across three major benchmarks to test its versatility and accuracy:

  • TFix (JavaScript): A dataset of over 100,000 instances based on ESLint errors. RAP-Gen improved exact match accuracy from 49.70% to 54.15%, repairing 478 more bugs than previous state-of-the-art models.
  • Code Refinement (Java): A dataset of GitHub bug-fixing commits. RAP-Gen showed a significant jump in exact match results, particularly in the "small" subset (increasing from 21.61 to 24.80).
  • Defects4J (Java): An industry-standard benchmark of real-world bugs. RAP-Gen repaired 125 bugs—15 more than previous models—under conditions of perfect fault localization.

Key Insights

Efficiency Over Scale

One of the study's most significant findings is that a smaller model (CodeT5-base, 220M parameters) equipped with retrieval capabilities can outperform much larger models (T5-large, 770M parameters) that lack them. This suggests that providing a model with "external memory" is a more efficient path to performance than simply increasing parameter count.

Language Agnosticism

Unlike many traditional APR tools that rely on complex Abstract Syntax Trees (ASTs) or specific compiler features, RAP-Gen operates on raw source code. This makes it highly flexible and easily deployable across different programming languages without requiring specialized language-specific engineering.

Limitations and Future Directions

While RAP-Gen sets a new standard for APR, several challenges remain:

  • Codebase Quality: The system's efficacy depends on the quality of the retrieval database. Unique bugs without similar precedents may result in the retriever providing "noise" rather than helpful context.
  • Patch Complexity: The current framework focuses on localized, single-point fixes. Future iterations will need to address "multi-hunk" patches that require changes across multiple locations in a file.
  • Latency: The addition of a dense retrieval step introduces computational overhead. Optimizing this loop is essential for future integration into real-time Integrated Development Environments (IDEs).

Conclusion

RAP-Gen demonstrates that Retrieval-Augmented Generation is a powerful paradigm for code repair. By bridging the gap between retrieval-based search and generative modeling, the framework provides a scalable, efficient, and language-agnostic solution for the future of automated software maintenance.

Summary updated

BibTeX
@inproceedings{wang2023rap,
  author = {Weishi Wang and Yue Wang and Shafiq Joty and Steven C. H. Hoi},
  title = {{RAP-Gen: Retrieval-Augmented Patch Generation with CodeT5 for Automatic Program Repair}},
  booktitle = {ESEC/SIGSOFT FSE},
  year = {2023},
  doi = {10.1145/3611643.3616256},
}
Figure