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

Enhancing LLM-based Fault Localization with a Functionality-Aware Retrieval-Augmented Generation Framework

Xinyu Shi, Zhenhao Li, An Ran Chen · DOI

Abstract

Fault localization (FL) is a critical but time-consuming task in software debugging, aiming to identify faulty code elements. While recent advances in large language models (LLMs) have shown promise for FL, they often struggle with complex systems due to the lack of project-specific knowledge and the difficulty of navigating large projects. To address these limitations, we propose FaR-Loc, a novel framework that enhances method-level FL by integrating LLMs with retrieval-augmented generation (RAG). FaR-Loc consists of three key components: LLM Functionality Extraction, Semantic Dense Retrieval, and LLM Re-ranking. First, given a failed test and its associated stack trace, the LLM Functionality Extraction module generates a concise natural language description that captures the failing behavior. Next, the Semantic Dense Retrieval component leverages a pre-trained code-understanding encoder to embed both the functionality description (natural language) and the covered methods (code) into a shared semantic space, enabling the retrieval of methods with similar functional behavior. Finally, the LLM Re-ranking module reorders the retrieved methods based on their contextual relevance. Our experiments on the widely used Defects4J benchmark show that FaR-Loc outperforms state-of-the-art LLM-based baselines SoapFL and AutoFL, by 14.6% and 9.1% in Top-1 accuracy, by 19.2% and 22.1% in Top-5 accuracy, respectively. It also surpasses all learning-based and spectrum-based baselines across all Top-N metrics without requiring re-training. Furthermore, we find that pre-trained code embedding models that incorporate code structure, such as UniXcoder, can significantly improve fault localization performance by up to 49.0% in Top-1 accuracy. Finally, we conduct a case study to illustrate the effectiveness of FaR-Loc and to provide insights for its practical application.

Introduction

Pinpointing the exact method or line of code responsible for a software bug—known as fault localization (FL)—remains a significant bottleneck in the development lifecycle. While traditional spectrum-based (SBFL) and learning-based (LBFL) techniques have been widely adopted, they often suffer from limited generalizability and high labeling costs. Recent explorations into Large Language Models (LLMs) for FL show promise, yet these models frequently struggle with the "needle in a haystack" problem: navigating massive codebases within constrained context windows. Without specific project knowledge, LLMs are prone to hallucinations and cascading errors.

To address these challenges, we introduce FaR-Loc (Functionality-Aware fault localization Framework via Retrieval-Augmented LLMs). FaR-Loc leverages the Retrieval-Augmented Generation (RAG) paradigm to provide LLMs with precise, semantically relevant context, focusing on failing functionality rather than raw, noisy debugging data.

The FaR-Loc Framework

FaR-Loc operates through a specialized three-stage pipeline designed to bridge the gap between natural language bug descriptions and source code logic.

The FaR-Loc architecture and its three-stage pipeline.
  1. LLM Functionality Extraction: Instead of querying the codebase with raw stack traces, which are often cluttered with helper methods, FaR-Loc uses an LLM to synthesize the failing test code and stack trace into a concise natural language description of the intended but failing functionality.
  2. Semantic Dense Retrieval: Using a pre-trained cross-modal encoder (such as UniXcoder), the framework embeds both the functionality query and the project's methods into a shared vector space. It retrieves the top-$k$ methods that are semantically most similar to the described failure.
  3. LLM Re-ranking: A high-reasoning LLM evaluates the retrieved candidates alongside the functionality query. By reasoning over this focused subset of code, the model produces a final, prioritized list of suspicious methods.

Experimental Evaluation

We evaluated FaR-Loc on the Defects4J benchmark, a standard collection of real-world Java bugs. Our primary evaluation utilized Defects4J v1.2.0 (381 bugs), while v2.0.0 (214 bugs) was used to assess generalizability.

Performance comparison on the Defects4J benchmark.

As shown in the results, FaR-Loc achieves state-of-the-art accuracy. On Defects4J v1.2.0, it correctly identified 228 faults at Top-1, representing a 14.6% improvement over SoapFL and a 9.1% improvement over AutoFL. In Top-5 metrics, the gains were even more pronounced, with improvements of 19.2% and 22.1% respectively. Notably, FaR-Loc outperformed all traditional learning-based models (like GRACE) and spectrum-based methods (Ochiai) without requiring any project-specific training.

Key Insights

  • Semantic Retrieval vs. Raw Traces: Translating structural stack traces into semantic "functionality queries" significantly reduces noise, allowing retrieval models to find relevant logic more effectively.
  • Structural Awareness in Embeddings: The choice of embedding model is pivotal. Models like UniXcoder, which incorporate Abstract Syntax Trees (ASTs), outperformed text-only code models by up to 49.0% in Top-1 accuracy, highlighting the importance of code structure in semantic search.
  • Efficiency and Cost: By narrowing the context window through retrieval before applying heavy reasoning, FaR-Loc is remarkably cost-effective. It averages just $0.019 per bug, making it approximately 65-70% cheaper than multi-agent navigation approaches.

Conclusion

FaR-Loc demonstrates that a functionality-aware RAG approach can significantly enhance the precision and efficiency of LLM-based fault localization. By decomposing the task into extraction, retrieval, and re-ranking, the framework avoids the pitfalls of large context windows and provides a scalable solution for complex software systems. Future work will explore extending FaR-Loc to other programming languages and integrating ensemble retrieval strategies to further improve robustness.

Summary updated

BibTeX
@misc{shi2025enhancing,
  author = {Xinyu Shi and Zhenhao Li and An Ran Chen},
  title = {{Enhancing LLM-based Fault Localization with a Functionality-Aware Retrieval-Augmented Generation Framework}},
  year = {2025},
  eprint = {2509.20552},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2509.20552},
}
Figure