TL;DR
Existing code RAG frameworks fail because they treat code like natural language text, using naive chunking and surface-level similarity matching. This paper introduces Hydra, a framework that treats code as a structured graph of Abstract Syntax Tree (AST) components and call-graph dependencies. Hydra achieves state-of-the-art performance across open- and closed-source CodeLLMs, boosting Pass@1 by over 5% and enabling a 1.5B parameter model to outperform a 7B model using standard retrievers.
What Problem Does This Paper Address?
While Large Language Models for code (CodeLLMs) excel at standalone function completion, their performance plummets when tasked with repository-level generation. In real-world software engineering, writing a function requires understanding cross-file dependencies, global variables, and class structures spread across an entire codebase.
To provide this context, current Retrieval-Augmented Generation (RAG) systems borrow directly from Natural Language Processing (NLP). They split code files into arbitrary, fixed-size text chunks (e.g., 20-line blocks) and retrieve context using text similarity algorithms like BM25 or embedding-based cosine search.
The authors identify two fatal flaws in this approach:
- Contextual Fragmentation: Fixed-size chunking breaks logical boundaries, splitting functions or classes in half and stripping away their semantic coherence.
- Similarity Blindness: Crucial dependencies (like a helper function or a global configuration variable) often share very little lexical or surface similarity with the target function prompt, causing standard retrievers to miss them entirely.
Methods
The authors propose Hydra, a repository-level code generation framework designed to treat code as structured data rather than flat text.
Hydra consists of three main components:
- Structure-Aware Indexing: Instead of splitting text arbitrarily, Hydra uses an AST parser to break the repository down into its natural logical units: Function nodes, Class nodes, and Variable nodes. It preserves the complete implementation within each node and maps the relationships (such as imports and scopes) as edges in a graph.
- Dependency-Aware Retriever (DAR): Hydra trains a lightweight encoder (based on UniXCoder) to explicitly predict whether a specific node in the codebase is a direct dependency required by the target function. Interestingly, the authors found that standard contrastive learning failed to converge because code dependencies are highly query-dependent. Instead, they framed this as a pairwise classification task, training the model to output a binary probability of relevance for a given query-candidate pair.
- Hybrid Retrieval Strategy: DAR ensures the generator receives the exact structural building blocks (definitions) it needs. To complement this, Hydra runs a parallel BM25 search to retrieve textually similar snippets, which provide the LLM with practical usage examples of how those dependencies are invoked in practice.
Datasets
- Training Dataset: The authors curated a high-quality dataset by mining 2,864 Python repositories from GitHub (filtering for at least 30 forks, 10 stars, and 5 files). This yielded approximately 564,740 data samples for fine-tuning the DAR module.
- Evaluation Benchmarks: Hydra was evaluated on two rigorous, execution-based repository-level benchmarks:
- RepoExec: 355 problems focused on executability, functional correctness, and dependency utilization.
- DevEval: A manually annotated benchmark containing 1,825 test samples from 117 real-world Python repositories across 10 popular domains.
Key Insights and Ideas
- Code is a Graph, Not a String: Naive text chunking forces the model to hallucinate. If a retriever returns a partial class definition that omits a critical method, the LLM will invent a plausible but non-existent method name, leading to immediate runtime errors (like
AttributeError). - The Failure of Contrastive Learning for Code: In standard code search, contrastive learning works well. But for dependency retrieval, it creates an ambiguous optimization landscape. A function
Func_Amight be a vital dependency forQuery_1(positive) but completely irrelevant toQuery_2(negative), causing conflicting gradient updates that destabilize contrastive training. Pairwise classification circumvents this. - The "What" vs. the "How": A perfect code generator needs two types of context: the structural definitions of its dependencies (the "what") and concrete examples of how those dependencies are used elsewhere in the codebase (the "how"). Combining DAR and BM25 satisfies both needs.
Key Results
Hydra was tested using open-source backbones (Qwen2.5-Coder 1.5B and 7B) and closed-source models (GPT-4.1 mini).
- State-of-the-Art Performance: Hydra consistently outperformed top baselines (RepoCoder, RepoFormer, and RLCoder). On the DevEval benchmark using GPT-4.1 mini, Hydra achieved a 31.91% Pass@1, outperforming the closest baseline by over 5%.
- Bridging the Model Size Gap: Remarkably, a small 1.5B Qwen model equipped with Hydra outperformed a 7B Qwen model using standard retrievers or no retrieval on both benchmarks.
- Fewer Hallucinations: Error analysis showed a massive drop in structural failures like
NameErrorandAttributeError, proving that Hydra successfully delivers the exact missing symbols the LLM needs. - High Efficiency: Because Hydra restricts its search space to the current file and its imported modules, its computational complexity is $O(c)$ (where $c$ is the size of local dependencies) rather than $O(n)$ (the size of the entire repository). This yields a median retrieval latency of just 220–270 milliseconds, making it highly practical for real-time IDE integration.
Limitations and Future Work
- Language Scope: The current implementation and evaluation are strictly limited to Python. Generalizing the AST parsing and dependency tracking to multi-language repositories remains an open task.
- Granularity of Generation: The framework focuses exclusively on function-level code generation. Extending structure-aware RAG to higher-level tasks, such as generating entire classes or modules from scratch, is left for future work.
- Retriever Backbone: The authors only evaluated UniXCoder as the underlying encoder for the DAR module; exploring newer, code-specific transformer architectures could yield further improvements.
Summary updated