Understanding and Mitigating Library-Related Issues in LLM-Generated Code
Yacine Majdoub, Rinad Hamid, Eya Ben Charrada, Ahmad Abdellatif, Haifa Touati
Abstract
Software practitioners increasingly rely on Large Language Models (LLMs) to generate code that integrates external libraries. However, LLMs often produce incorrect library usage, such as invalid imports, outdated API calls, and hallucinated dependencies, leading to compilation or runtime failures that reduce the reliability of AI-assisted software development. In this paper, we propose an agentic approach to mitigate libraryrelated errors in LLM-generated code. More specifically, we first conduct an exploratory study to characterize the library-related issues produced by LLMs. Our analysis of 100 LLM-generated code files reveals that 84% of generated files contain at least one library-related error, with recurring patterns including incorrect import paths, missing imports, hallucinated libraries, deprecated library usage, and unused imports. Based on these findings, we design an agentic approach that integrates task analysis, documentation grounding, code generation, and automated validation to improve library usage during code synthesis. We evaluate our approach on 300 code generation tasks derived from realworld implementations of rapidly evolving Python frameworks, including LangChain and AutoGen, across five LLMs: GPT-5, DeepSeek-V3, Qwen3, Mistral, and Llama 3. The results show that our approach consistently improves code generation quality across all evaluated models, reducing library-related errors by 38.1% - 54.6% and increasing code correctness by up to 16%.
Create a lesson
Related papers
PreMaQ: Predicting Maintainability-Related Quality of LLM-Generated Code Before Generation
Shared title terminologycs.SETaiki Wakamatsu, Yuta Ishimoto, Masanari Kondo et al.
Understanding and Mitigating Representation Drift of Code Models under Software Distribution Shifts
Shared title terminologycs.SEYiqing Shi, Zhifei Chen, Yangyang Zhao et al.
AI-Generated Code Issues: A Taxonomy from Repository Histories, AI Conversations, and Developer Interviews
Shared title terminologycs.SERosalia Tufano, Nargiz Humbatova, Daniele Bifolco et al.
Newer and Bigger, but Safer? A Longitudinal Study of the Functionality-Security Gap in LLM-Generated Code
Shared title terminologycs.SEThiago Santos de Moura, Fynn Matuschek, Flavio Toffalini et al.
Code Detectors Have a Half-Life: Obsolescence and Metric Illusions in LLM-Generated Code Detection
Shared title terminologycs.SEAlberick Euraste Djire
"My Code Runs Fine, I Don't": Understanding How Software Professionals Work Through Chronic Pain
Shared title terminologycs.SEMairieli Wessel, Kiev Gama, Joukje Oosterman et al.