Library Hallucinations in LLMs: Risk Analysis Grounded in Developer Queries

Lukas Twist, Jie M. Zhang, Mark Harman, Helen Yannakoudakis · 2025

Read Library Hallucinations in LLMs: Risk Analysis Grounded in Developer Queries by Lukas Twist, Jie M. Zhang, Mark Harman, Helen Yannakoudakis (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2509-22202.txt.

Introduction

Language models that write code often invent libraries that do not exist, and Lukas Twist, Mark Harman, Helen Yannakoudakis, and Jie M. Zhang measure how easily that happens. Such library hallucinations mislead developers, break builds, and can even be exploited by attackers who register the fake names. Their 2025 study, accepted at EMNLP, is the first to test how realistic variations in a developer's prompt affect the problem, across seven large models.

The findings expose systemic fragility. A one-character misspelling in a request triggered invented imports in a large share of tasks. Made-up library names offered by the user were accepted almost every time. Prompts that mention dates or versions also induced hallucinations at high rates. From the riskiest prompts, the authors build LibHalluBench, a benchmark for reproducible evaluation, and they urge safeguards against this class of error.

Catalog record

CREATORLukas Twist, Jie M. Zhang, Mark Harman, Helen Yannakoudakis
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE65 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2509-22202

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

Library Hallucinations in LLMs: Risk Analysis Grounded in Developer Queries Lukas Twist, Jie M. Zhang, Mark Harman, Helen Yannakoudakis Abstract Large language models (LLMs) are increasingly used to generate code, yet they continue to hallucinate, often 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 increasing awareness of these risks, little is known about how real-world prompt variations affect hallucination rates. Therefore, we present the first systematic study of how user-level prompt variations impact library hallucinations in LLM-generated code. We evaluate six diverse LLMs across two hallucination types: library name hallucinations (invalid imports) and library member hallucinations (invalid calls from valid libraries). We investigate how realistic user language extracted from developer forums and how user errors of varying degrees (one- or multi-character misspellings and completely fake names/members) affect LLM hallucination rates. Our findings reveal systemic vulnerabilities: one-character misspellings in library names trigger hallucinations in up to 26% of tasks, fake library names are accepted in up to 99% of tasks, and time-related prompts lead to hallucinations in up to 84% of tasks. Prompt engineering shows promise for mitigating hallucinations, but remains inconsistent and LLM-dependent. Our results underscore the fragility of LLMs to natural prompt variation and highlight the urgent need for safeguards against library-related hallucinations and their potential exploitation. 1 Introduction Large language models (LLMs) have rapidly become a cornerstone of modern software development, transforming natural language (NL) prompts into executable code with remarkable accuracy and speed ( Jiang et al. 2024a ) . Their substantial traction in industry–evidenced by GitHub Copilot’s 90% merge rate in enterprise settings ( Novet & Vanian 2025 ) and Microsoft’s usage rate of up to 30% ( Gao & Research 2024 ) –means that LLM-generated code is increasingly deployed in real-world systems. However, the reliability of this code remains a critical concern. A particularly serious failure mode is code hallucinations–in which LLMs fabricate elements of the code they produce–with serious consequences in software engineering ( Zhang et al. 2024 ; Lee et al. 2025 ) . Unlike typical bugs, these hallucinations are generated confidently, may not trigger immediate errors, and can mislead developers into integrating non-existent or insecure components. The problem is particularly acute for external libraries, which are central to modern software engineering and are routinely imported to accelerate development ( Larios Vargas et al. 2020 ) . Despite a clear ground truth of published documentation, LLMs still frequently exhibit library-related hallucinations when generating code ( Spracklen et al. 2024 ) . Such hallucinations bypass dependency validation and can mislead developers into integrating invalid, deprecated, or insecure packages, which pose significant risks in practice ( Wang et al. 2025 ; Krishna et al. 2025 ) . Furthermore, the recent emergence of “slopsquatting”–where a frequently hallucinated library is created by a malicious actor–poses another significant threat, one specific to library hallucinations ( Park 2025 ) . Existing studies on library hallucinations largely define the problem at a broad level, focussing on aggregate evaluations without offering a systematic understanding of what triggers them ( Krishna et al. 2025 ; Spracklen et al. 2024 ) . As a result, we lack a fine-grained understanding of how realistic user-language variations or common naming errors affect hallucination rates. Mitigation efforts have been mainly based on retrieval-augmented generation (RAG), grounding responses in official documentation ( Jain et al. 2024 ) or project-specific contexts ( Wang et al. 2024b ) . Although effective, these approaches require considerable infrastructure that limits practicality for users who rely on off-the-shelf models and prompt engineering. Moreover, RAG may still silently fail if users provide incorrect references that lack retrieval matches, causing LLMs to revert to default generation and produce confident hallucinations. We go beyond previous aggregate evaluations by simulating real-world developer prompts, including user errors and natural language variation, and quantifying their effect on library-related hallucinations across six production-grade LLMs. Understanding how LLMs respond to these language variations offers practical guidance for both LLM users to avoid triggering hallucinations and for LLM builders to realise vulnerabilities. We focus on two classes of library-related hallucinations that can be reliably verified: library name hallucinations , where an LLM generates code that imports a non-existent library; and library member hallucinations , where the code references a non-existent function, class or object from a valid library. We design three systematic experiments to quantify how prompt variations affect library hallucinations in LLM-generated code: 1) We prompt LLMs using descriptions of libraries extracted from Software Recommendations StackExchange 1 1 1 Software Recommendations StackExchange: https://softwarerecs.stackexchange.com/ to simulate authentic developer intent; 2) We introduce controlled language variations simulating different degrees of user error, including one- and multi-character misspellings, or completely fake library names/members; 3) We test whether prompt engineering techniques such as chain-of-thought or step-back prompting can mitigate hallucinations in these settings. Our findings reveal key vulnerabilities in LLM code generation: 1) LLMs largely ignore adjective-based library descriptions but are highly susceptible to time-related prompts – prompts asking for a library “from 2025” produce hallucinated libraries in up to 84% of tasks; 2) LLMs are not robust to user errors in library names – requesting the use of a library with a one-character misspellings causes hallucinations in up to 26% of tasks, multi-character misspellings in up to 79% of tasks, and fake libraries are used in up to 99% of tasks. These results suggest that LLMs may inadvertently amplify typosquatting risks by treating small naming errors as valid packages; 3) prompt-engineering strategies can reduce library hallucination rates in some scenarios, but improvements are not guaranteed, and well-studied reasoning prompts (such as chain-of-thought) often fail or even increase hallucination rates, highlighting the need for further work in library hallucination mitigation . Our contributions are as follows: 1. We present the first systematic empirical study on how prompt variations affect library name and member hallucination rates. 2. We reveal concrete risks tied to user errors and time-related phrasing, showing how high model compliance can increase exposure to typosquatting and slopsquatting attacks. 3. We uncover a novel connection between LLM behaviour and typosquatting: minor prompt errors can lead LLMs to confidently hallucinate, potentially exposing developers to supply chain threats. This complements prior work on slopsquatting by revealing a distinct, previously unexplored risk. 4. We provide practical mitigation insights and release our code and a benchmark suite, including prompts, labels, and full outputs, to support future research on hallucination detection and prevention: https://github.com/itsluketwist/realistic-library-hallucinations 2 Related Work Background on Hallucinations : Hallucinations are a common failure mode in NL generation, referring to outputs that are nonsensical or unfaithful to the source content provided ( Ji et al. 2023 ) . For LLMs, the notion extends to confident but ungrounded predictions, where outputs appear fluent and coherent but conflict with external sources, or the intent or context of the prompt ( Huang et al. 2023 ; Zhang et al. 2023 ) . LLM Code Generation & Hallucinations : LLMs excel in code generation ( Jiang et al. 2024a ) , with increasingly impressive performance across a wide range of tasks ( Chen et al. 2024 ) . Hallucinations during code generation manifest themselves in programming-specific ways, including error-producing code,

Read the complete text