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

GraphCoder: Enhancing Repository-Level Code Completion via Code Context Graph-based Retrieval and Language Model

Wei Liu, Ailun Yu, Daoguang Zan, Bo Shen, Wei Zhang, Haiyan Zhao, Zhi Jin, Qianxiang WangarXiv 2024 · DOI

Introduction

Large Language Models (LLMs) like GPT-4 and StarCoder have revolutionized general-purpose programming. However, they frequently struggle when applied to specific, private repositories. These models often lack "repository-specific knowledge," such as internal APIs, unique coding conventions, and complex cross-file dependencies that were not part of their public training data.

To bridge this gap, current solutions employ Retrieval-Augmented Generation (RAG), which searches a repository for relevant code snippets to provide context for the LLM. Most existing RAG tools treat code as natural language, relying on sequence-based similarity. This approach fails to account for the structured, logical nature of code, where a variable definition 50 lines away may be more relevant than the line immediately preceding the cursor. GraphCoder addresses this by moving beyond simple text-based retrieval to a structural, graph-based approach.

The GraphCoder Framework

GraphCoder represents a repository as a Code Context Graph (CCG) rather than a collection of text files. This multi-graph structure captures three fundamental types of dependencies:

  1. Control Flow (CFG): The order of execution.
  2. Data Dependence (DDG): The relationship between variable definitions and their usage.
  3. Control Dependence (CDG): The logic determining whether specific statements execute (e.g., if conditions).
The GraphCoder framework overview, illustrating the pipeline from repository construction to retrieval and final generation.

The framework operates in three main stages. First, it constructs the CCG for every statement in the repository. Second, it uses "CCG Slicing" to extract manageable subgraphs of relevant neighbors around a target statement. Finally, it employs a coarse-to-fine retrieval process. A fast BM25 search initially filters for textual similarity, followed by a fine-grained re-ranking using Decay-with-distance Subgraph Edit Distance (SED). This algorithm prioritizes candidates that are logically and structurally closer to the query.

Experimental Methodology

The researchers evaluated GraphCoder using RepoEval-Updated, a benchmark designed to mitigate "data leakage." By using repositories created in 2023, the authors ensured that the evaluated LLMs had not encountered the code during their initial training. The dataset spans 8,000 tasks across Python and Java, ranging from simple line-level completions to complex API-level tasks.

Results and Key Insights

GraphCoder was tested against several baselines, including Vanilla RAG and RepoCoder, across five different LLMs. The results demonstrate that structural alignment is a superior proxy for relevance in code completion compared to text similarity.

  • Accuracy Gains: GraphCoder achieved the highest Exact Match (EM) scores across all tests, providing an average boost of +6.06% in code match and +6.23% in identifier match compared to vanilla RAG.
  • Unique Successes: GraphCoder successfully completed a significant number of tasks that all other retrieval methods failed to solve, highlighting its ability to find functionally similar snippets even when variable names differ.
Venn diagrams illustrating that GraphCoder correctly completes many tasks that other RAG methods miss, demonstrating its unique retrieval capabilities.
  • Efficiency: By indexing statements rather than lines or windows, GraphCoder reduced the number of database entries by 79.5%. This statement-level indexing filters out noise like comments and blank lines, making the system faster and more storage-efficient than sequence-based competitors.
  • Structural Importance: Ablation studies identified the Control Flow Graph (CFG) as the most critical component of the CCG; removing it significantly degraded the model's logical understanding.

Conclusion

GraphCoder demonstrates that treating code as a structured graph rather than a sequence of text significantly improves the performance of RAG for repository-level code completion. While building the initial graph index requires more overhead than text indexing, the resulting gains in accuracy and retrieval efficiency make it a powerful tool for working within private codebases. Future work will focus on expanding language support beyond Python and Java and optimizing performance for repositories with low code duplication.

Summary updated

BibTeX
@misc{liu2024graphcoder,
  author = {Wei Liu and Ailun Yu and Daoguang Zan and Bo Shen and Wei Zhang and Haiyan Zhao and Zhi Jin and Qianxiang Wang},
  title = {{GraphCoder: Enhancing Repository-Level Code Completion via Code Context Graph-based Retrieval and Language Model}},
  year = {2024},
  eprint = {2406.07003},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.07003},
}
Figure