mlsanityData to 7 Oct 2026

Towards Mitigating API Hallucination in Code Generated by LLMs with Hierarchical Dependency Aware

arXiv 2505.05057 ↗First posted 8 May 2025cs.SEcited by 6

On the map: Retrieval-augmented generation → RAG for code

Abstract (arXiv)

Application Programming Interfaces (APIs) are crucial in modern software development. Large Language Models (LLMs) assist in automated code generation but often struggle with API hallucination, including invoking non-existent APIs and misusing existing ones in practical development scenarios. Existing studies resort to Retrieval-Augmented Generation (RAG) methods for mitigating the hallucination issue, but tend to fail since they generally ignore the structural dependencies in practical projects and do not indeed validate whether the generated APIs are available or not. To address these limitations, we propose MARIN, a framework for mitigating API hallucination in code generated by LLMs with hierarchical dependency aware. MARIN consists of two phases: Hierarchical Dependency Mining, which analyzes local and global dependencies of the current function, aiming to supplement comprehensive project context in LLMs input, and Dependency Constrained Decoding, which utilizes mined dependencies to adaptively constrain the generation process, aiming to ensure the generated APIs align with the projects specifications. To facilitate the evaluation of the degree of API hallucination, we introduce a new benchmark APIHulBench and two new metrics including Micro Hallucination Number (MiHN) and Macro Hallucination Rate (MaHR). Experiments on six state-of-the-art LLMs demonstrate that MARIN effectively reduces API hallucinations, achieving an average decrease of 67.52% in MiHN and 73.56% in MaHR compared to the RAG approach. Applied to Huaweis internal projects and two proprietary LLMs, MARIN achieves average decreases of 57.33% in MiHN and 59.41% in MaHR.

Key facts. Cited by 6 papers in the map (data through 7 Oct 2026). Placed in RAG for code, one of 15 directions in Retrieval-augmented generation. Builds on 16 of its 39 references that are in the map. First posted on arXiv 8 May 2025.

Builds on

16 of 39 references.

References · 16

  1. 2023Llama 2: Open Foundation and Fine-Tuned Chat Modelscited by 12,865
  2. 2023Efficient Memory Management for Large Language Model Serving with PagedAttentioncited by 5,767
  3. 2022Survey of Hallucination in Natural Language Generationcited by 2,056
  4. 2023WizardCoder: Empowering Code Large Language Models with Evol-Instructcited by 728
  5. 2022InCoder: A Generative Model for Code Infilling and Synthesiscited by 492
  6. 2023Magicoder: Empowering Code Generation with OSS-Instructcited by 251
  7. 2022Grounded Copilot: How Programmers Interact with Code-Generating Modelscited by 216
  8. 2024Beyond Functional Correctness: Exploring Hallucinations in LLM-Generated Codecited by 106
  9. 2024LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigationcited by 92
  10. 2024A New Era in LLM Security: Exploring Security Concerns in Real-World LLM-based Systemscited by 74
  11. 2023PanGu-Coder2: Boosting Large Language Models for Code with Ranking Feedbackcited by 69
  12. 2021Revisiting, Benchmarking and Exploring API Recommendation: How Far Are We?cited by 21
4 more
  1. 2024On Mitigating Code LLM Hallucinations with API Documentationcited by 21
  2. 2024ComplexCodeEval: A Benchmark for Evaluating Large Code Models on More Complex Codecited by 18
  3. 2021Toward Less Hidden Cost of Code Completion with Acceptance and Ranking Modelscited by 4
  4. 2022API Usage Recommendation via Multi-View Heterogeneous Graph Representation Learningcited by 1

Built on by

Cited by year (2026 to 7 Oct)

Citing papers per year: 2025: 1; 2026: 5.

6 papers.

Similar, not cited

Close in wording, no citation either way

  1. 2024De-Hallucinator: Mitigating LLM Hallucinations in Code Generation Tasks via Iterative Grounding
  2. 2024A Comprehensive Framework for Evaluating API-oriented Code Generation in Large Language Models
  3. 2026When LLMs Lag Behind: Knowledge Conflicts from Evolving APIs in Code Generation
  4. 2026Learning from Execution: Self-Evolving Memory for Private-Library Code Generation
  5. 2026Mitigating Errors in LLM-Generated Web API Invocations via Retrieval-Augmented Generation and Constrained Decoding
  6. 2026Diagnosing Knowledge Gaps in LLM Tool Use: An Agentic Benchmark for Novel API Acquisition
  7. 2026Evaluating Inference-Time Defenses Against Package Hallucination in LLM-Generated Code
  8. 2026Do Not Treat Code as Natural Language: Implications for Repository-Level Code Generation and Beyond