Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries
summary
The gist
The paper, "Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries," presents a systematic study on how user-level prompt variations influence library
In short
The episode discusses the paper "Library Hallucinations in LLM-Generated Code," which identifies that AI models often fabricate entire, seemingly valid code elements rather than just making typos. These hallucinations are presented as systemic failures triggered by specific human interaction patterns, such as natural phrasing or misspellings. The hosts conclude that developers must use tools like LIBHALLUBENCH and maintain skepticism toward AI output.
Key concepts
- Library Hallucinations
- This refers to the specific failure mode where LLMs confidently invent non-existent libraries or elements of code. Instead of finding the correct tool, the model fabricates a plausible but fake alternative, such as recommending 'math_quantum' instead of 'numpy.'
- Systemic Failures
- The paper frames these issues not as random bugs but as predictable failures tied to how users interact with AI. It maps triggers—ranging from user language to direct input mistakes—to show where the model's inherent blind spots are.
- LIBHALLUBENCH
- This is a proposed framework designed to systematically measure and test the effects of hallucinations. It provides a clear, standardized method for evaluating whether an LLM has generated genuinely good code or merely highly plausible fabrication.
Terminology used across episodes
This episode discusses
- Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries · Paper Radio
- Dated Data: Tracing Knowledge Cutoffs in Large Language Models
- DeepSeek-V3 Technical Report
- De-Hallucinator: Mitigating LLM Hallucinations in Code Generation Tasks via Iterative Grounding
- Using digital traces to analyze software work: skills, careers and programming languages
- Towards Mitigating API Hallucination in Code Generated by LLMs with Hierarchical Dependency Aware
- Is ChatGPT a Good Software Librarian? An Exploratory Study on the Use of ChatGPT for Software Library Recommendations
- GitHub Typo Corpus: A Large-Scale Multilingual Dataset of Misspellings and Grammatical Errors
- Hallucination by Code Generation LLMs: Taxonomy, Benchmarks, Mitigation, and Challenges
- A Survey on Hallucination in Large Language Models: Principles, Taxonomy, Challenges, and Open Questions
- Self-Reflection Makes Large Language Models Safer, Less Biased, and Ideologically Neutral
- Qwen2.5-Coder Technical Report
- On Mitigating Code LLM Hallucinations with API Documentation
- A Survey on Large Language Models for Code Generation
- A Survey on Large Language Model Hallucination via a Creativity Perspective
- The Dynamics of Innovation in Open Source Software Ecosystems
- Importing Phantoms: Measuring LLM Package Hallucination Vulnerabilities
- Gorilla: Large Language Model Connected with Massive APIs
- Which Prompting Technique Should I Use? An Empirical Investigation of Prompting Techniques for Software Engineering Tasks
- Check Your Facts and Try Again: Improving Large Language Models with External Knowledge and Automated Feedback
- Misspellings in Natural Language Processing: A survey
The paper
Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries · Read on arXiv
Lukas Twist, Jie M. Zhang, Mark Harman, Helen Yannakoudakis
King’s College London, University College London, UK · University College London, UK (Note: The affiliations are listed together in the source document)
Large language models (LLMs) now play a central role in code generation, yet they continue to hallucinate, frequently inventing non-existent libraries. Such library hallucinations are not just benign errors: they can mislead developers, break builds, and expose systems to supply chain threats such as slopsquatting. Despite growing awareness of these risks, there is limited understanding of how library hallucinations manifest under realistic usage conditions. To fill this gap, we present the first systematic study of how user-level prompt variations influence library hallucinations in LLM-generated code. Across seven diverse LLMs, we analyse library name hallucinations (invalid imports) and library member hallucinations (invalid calls from valid libraries), examining the effects of realistic developer language and controlled user mistakes, including misspellings and fabricated libraries or members. Our findings expose systemic vulnerabilities: one-character misspellings trigger hallucinations in up to 26% of tasks; fabricated library names are accepted in up to 99%; and time-based prompts induce hallucinations in up to 85%. Grounded in the highest-risk prompts identified in our study, we introduce LibHalluBench, a benchmark that enables a systematic and reproducible evaluation of these library hallucinations. Our findings underscore the fragility of LLMs to natural prompt variation and highlight the urgent need for safeguards against library-related hallucinations and their downstream risks.
Transcript
Introduction to the show: ident: AI Radio. Generated commentary on the latest Artificial Intelligence papers.
Tom: Next we'll be talking about the paper "Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries".
Jane: The paper was written by Lukas Twist, Jie M. Zhang, Mark Harman and Helen Yannakoudakis from King’s College London, University College London, UK and University College London, UK (Note: The affiliations are listed together in the source document).
Tom: Stay tuned as we take you through the paper and discuss its implications.
Title: Jane: The core finding of "Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries" is that these hallucinations aren't just random typos, Tom.
Tom: They are specific failures where the LLMs fabricate entire elements of the code that look perfectly valid, which is terrifying because they don't always trigger a crash immediately after hallucinating.
Lu: It’s fascinating how these models are confidently inventing non-existent libraries when they fail to find what we actually need.
Meng: And that confidence is the dangerous part; if an AI recommends "math quantum" instead of "numpy," the developer might take it at face value, which isn't good for practical development.
Lalam: I see this as a failure of trust in my own generated suggestions, Lu and Meng both point to the fact that we are treating these LLM outputs like ground truth when we shouldn't be.
Tom: This is why the paper’ so important because it frames it not just as "bugs," but as systemic failures based on how users interact with AI.
Jane: We are moving past simple syntax errors and looking at the authors, who are pointing to a specific failure mode related to library selection.
Summary: Tom: The summary of "Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries" is that there is a significant gap in understanding how prompt variations trigger these issues.
Jane: It goes beyond just stating that the problem exists; it’ shows *how* we can make the AI hallucinate, which is far more actionable for us.
Lu: I love the data on how these LLMs react to language—it suggests that our natural way of asking questions is actually making them more prone to error.
Meng: The finding that a one-character misspelling triggers hallucinations in up to twenty-six percent of tasks is a massive red flag for practical deployment.
Lalam: And the fact that fake library names are accepted in up to ninety-nine percent of tasks tells me that simply asking for an alternative solution often leads to fabrication.
Tom: So, the paper is essentially mapping the triggers—from user language to direct user mistakes—to show us where the "blind spots" are.
Jane: We're seeing that these failures aren't uniform across all LLMs, but they are widespread enough to be a major systemic issue for developers.
Improvements: Tom: The paper suggests several ways we can improve our approach to code generation, and it’s not just about adding more words to the prompt.
Jane: It highlights that simple tricks like adding an adjective-based description rarely causes issues, which is a relief for us users.
Lu: But the authors show that time-based prompts are highly effective triggers, with up to eighty-five percent hallucination rates when asking for libraries from "two thousand twenty-five."
Meng: That's a huge practical insight; if I ask my AI to be up-to-date, it might just make me more likely to accept a fake library.
Lalam: The idea of anti-sycophancy behaviour is really important here; instead of just obeying the user's typo, the model should refuse to use it.
Tom: And they are proposing L IB H ALLU B ENCH as a way to systematically measure these effects, which is a massive step forward for standardized testing.
Jane: It’s providing us with a clear framework to evaluate whether we are getting good code or just highly plausible hallucinations.
Conclusion: Tom: As we wrap up our discussion of "Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries," the core message is that these failures are systemic and tied to real human interaction patterns.
Jane: It's a powerful reminder that the more natural or less precise our prompts are, the more likely we are to receive fabricated code.
Lu: I think this paper really suggests that our future work needs to be less about "fixing" the output and more about building safer systems around it.
Meng: From my perspective, we need robust tools like those in L IB H ALLU B ENCH and a strong sense of skepticism toward any LLM-suggested library.
Lalam: I hope that this work helps us shift the cultural expectation from trusting AI output to using AI as a highly effective but non-authoritative assistant.
Tom: We've covered how user mistakes, like misspellings and fabricated names, are major triggers for hallucination across the board.
Jane: And by pointing out these vulnerabilities, we’ hope developers use this research to guide their next steps in the software development life cycle.
More episodes
- 2610.15391-DC-Flow: Target-Conditioned Cyclic Peptide Design via Delayed-Coupling Flow Matching
- 2610.15379-LiveAnon: Improving Privacy and Utility in Streaming Speaker Anonymization with Codec Language Models
- 2610.15097-Anchors Enhance Controllability and Reasoning of Diffusion Language Models
- 2610.15398-GA-SVD: Generation-aware Singular Value Decomposition for LLM Compression
- 2610.15106-RELAGRAM: Recursive Latent Graph Model for the Capacitated Vehicle Routing Problem
- 2610.15159-Beyond Adjacent Layers: Graph-Guided Layer Fusion for Compressing Large Language Models
- 2610.15185-DeltaNet-PC: Test-Time Memory Without Backpropagation Through Time
- 2610.15246-ObsSpec: Accelerating Agentic Reinforcement Learning with Observation Speculation
- 2610.15388-Approximation in Contrastive Representation Learning
- 2610.15427-Riemannian Graph Tokenizer for Structural Knowledge Transfer