Table of Contents
Fetching ...

Do Not Treat Code as Natural Language: Implications for Repository-Level Code Generation and Beyond

Minh Le-Anh, Huyen Nguyen, Khanh An Tran, Nam Le Hai, Linh Ngo Van, Nghi D. Q. Bui, Bach Le

TL;DR

This paper tackles the challenge that CodeLLMs underperform in repository-level code generation due to fragmentation and misalignment of context when treated as natural language. It introduces Hydra, a framework that uses structure-aware indexing to represent repositories as hierarchical units (functions, classes, variables), a dependency-aware retriever (DAR) to fetch true dependencies, and a hybrid Hydra retriever that combines DAR with BM25 for usage examples. Empirical results on DevEval and RepoExec show Hydra achieving state-of-the-art performance across open- and closed-source models, with smaller models approaching or surpassing larger baselines by providing high-quality, dependency-centered context. Hydra also demonstrates improved executable correctness, reduced hallucinations, and favorable latency by constraining retrieval to dependency-localized context, highlighting the practical value of moving beyond NLP-inspired chunking toward dependency-centric retrieval for repository-level code generation.

Abstract

Large language models for code (CodeLLMs) have demonstrated remarkable success in standalone code completion and generation, sometimes even surpassing human performance, yet their effectiveness diminishes in repository-level settings where cross-file dependencies and structural context are essential. Existing Retrieval-Augmented Generation (RAG) approaches often borrow strategies from NLP, relying on chunking-based indexing and similarity-based retrieval. Chunking results in the loss of coherence between code units and overlooks structural relationships, while similarity-driven methods frequently miss functionally relevant dependencies such as helper functions, classes, or global variables. To address these limitations, we present Hydra, a repository-level code generation framework that treats code as structured code rather than natural language. Our approach introduces (i) a structure-aware indexing strategy that represents repositories as hierarchical trees of functions, classes, and variables, preserving code structure and dependencies, (ii) a lightweight dependency-aware retriever (DAR) that explicitly identifies and retrieves the true dependencies required by a target function, and (iii) a hybrid retrieval mechanism that combines DAR with similarity-based retrieval to provide both essential building blocks and practical usage examples. Extensive experiments on the challenging DevEval and RepoExec benchmarks, both requiring function implementation from real-world repositories with complex large repository context, show that Hydra achieves state-of-the-art performance across open- and closed-source CodeLLMs. Notably, our method establishes a new state of the art in repository-level code generation, surpassing strongest baseline by over 5% in Pass@1 and even enabling smaller models to match or exceed the performance of much larger ones that rely on existing retrievers.

Do Not Treat Code as Natural Language: Implications for Repository-Level Code Generation and Beyond

TL;DR

This paper tackles the challenge that CodeLLMs underperform in repository-level code generation due to fragmentation and misalignment of context when treated as natural language. It introduces Hydra, a framework that uses structure-aware indexing to represent repositories as hierarchical units (functions, classes, variables), a dependency-aware retriever (DAR) to fetch true dependencies, and a hybrid Hydra retriever that combines DAR with BM25 for usage examples. Empirical results on DevEval and RepoExec show Hydra achieving state-of-the-art performance across open- and closed-source models, with smaller models approaching or surpassing larger baselines by providing high-quality, dependency-centered context. Hydra also demonstrates improved executable correctness, reduced hallucinations, and favorable latency by constraining retrieval to dependency-localized context, highlighting the practical value of moving beyond NLP-inspired chunking toward dependency-centric retrieval for repository-level code generation.

Abstract

Large language models for code (CodeLLMs) have demonstrated remarkable success in standalone code completion and generation, sometimes even surpassing human performance, yet their effectiveness diminishes in repository-level settings where cross-file dependencies and structural context are essential. Existing Retrieval-Augmented Generation (RAG) approaches often borrow strategies from NLP, relying on chunking-based indexing and similarity-based retrieval. Chunking results in the loss of coherence between code units and overlooks structural relationships, while similarity-driven methods frequently miss functionally relevant dependencies such as helper functions, classes, or global variables. To address these limitations, we present Hydra, a repository-level code generation framework that treats code as structured code rather than natural language. Our approach introduces (i) a structure-aware indexing strategy that represents repositories as hierarchical trees of functions, classes, and variables, preserving code structure and dependencies, (ii) a lightweight dependency-aware retriever (DAR) that explicitly identifies and retrieves the true dependencies required by a target function, and (iii) a hybrid retrieval mechanism that combines DAR with similarity-based retrieval to provide both essential building blocks and practical usage examples. Extensive experiments on the challenging DevEval and RepoExec benchmarks, both requiring function implementation from real-world repositories with complex large repository context, show that Hydra achieves state-of-the-art performance across open- and closed-source CodeLLMs. Notably, our method establishes a new state of the art in repository-level code generation, surpassing strongest baseline by over 5% in Pass@1 and even enabling smaller models to match or exceed the performance of much larger ones that rely on existing retrievers.
Paper Structure (33 sections, 1 equation, 6 figures, 6 tables, 1 algorithm)

This paper contains 33 sections, 1 equation, 6 figures, 6 tables, 1 algorithm.

Figures (6)

  • Figure 1: Overview of our workflow. The approach integrates structure-aware indexing, dependency-aware retrieval, and hybrid context construction to support repository-level code generation.
  • Figure 2: Comparison of chunk-based indexing and structure-aware indexing. Our approach preserves full functions, classes, and variables as nodes with structural links, avoiding the fragmentation and noise of traditional chunking.
  • Figure 3: Example of retrieved results from DAR and BM25 for a query (incomplete function under generation) in the RepoExec dataset.
  • Figure 4: Example of contextual fragmentation under chunk-based retrieval. The retriever provides partial context (isdir, listdir) but misses the ground-truth dependency exists. As a result, the model hallucinates an isfile method, leading to an AttributeError.
  • Figure 5: Case study comparing the generated code by Hydra and text-based retrieval on task_id 18 (RepoExec).
  • ...and 1 more figures