Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries¶
Conference: EMNLP 2026
Paper: ACL Anthology
Code: https://github.com/itsluketwist/realistic-library-hallucinations
Area: Hallucination Detection
Keywords: Library Hallucinations, Code Generation, Prompt Variation, Supply Chain Vulnerability, Hallucination Benchmark
TL;DR¶
This paper presents the first systematic study of how realistic developer prompt variations (natural language descriptors and controlled user typos/fabrications) trigger library name and member hallucinations in LLM-generated code, revealing alarming vulnerabilities to temporal constraints and typos, and introducing LIBHALLUBENCH for reproducible evaluation.
Background & Motivation¶
Large language models (LLMs) have become integral to modern software development, rapidly translating natural language prompts into executable code across industrial environments. However, generating code that relies on third-party dependencies introduces a critical yet understudied failure mode: library hallucinations, where models either fabricate non-existent package imports (library name hallucinations) or call invalid functions and classes from valid packages (library member hallucinations). These outputs are typically syntactically well-formed and presented with deceptive confidence, misleading developers, breaking continuous integration builds, and exposing software repositories to severe supply chain threats such as typosquatting and slopsquattingโwhere malicious actors register commonly hallucinated package names to deliver payloads.
Prior literature has predominantly examined code hallucinations at an aggregate level, offering taxonomies or measuring broad syntax and API errors without investigating the concrete prompt-level drivers that provoke them. In practical developer workflows, prompts are neither sterile nor uniform: engineers frequently include qualitative descriptors (such as requesting "fast", "lightweight", or "modern" packages), place temporal demands (e.g., "use a new library from 2025"), or inadvertently introduce typing slips (such as mistyping numpy as numpi). While NLP research widely assumes that modern LLMs are robust to benign input noise and capable of implicit error correction, whether this tolerance holds in the brittle, highly discrete domain of software dependencies has remained an open empirical question.
This paper tackles this gap by systematically examining LLM behaviour under authentic developer language and controlled user mistakes across seven production-grade models. Core idea: Ground prompt design in genuine developer queries from technical Q&A forums and inject controlled typographical and fabricated errors into code generation tasks, quantifying model susceptibility to library hallucinations and establishing the standardized benchmark LIBHALLUBENCH.
Method¶
Overall Architecture¶
The evaluation framework models realistic developer interactions by systematically deriving prompt variations from developer forums, querying LLMs under standardized parameter configurations, and extracting and verifying external dependency references against dynamic official package repositories and library documentation. The pipeline takes coding task descriptions and parameterized prompt directives as inputs, samples generated Python functions, parses abstract syntax trees (AST) to isolate imports and member accesses, and cross-references them against PyPI and official documentation.
%%{init: {'flowchart': {'rankSpacing': 24, 'nodeSpacing': 28, 'padding': 6, 'wrappingWidth': 400}}}%%
flowchart TD
A["Coding Task Input<br/>BigCodeBench Seed Tasks"] --> B["Realistic Prompt Variation Injection<br/>Forum Descriptors / Temporal Demands / Controlled Typos"]
B --> C["LLM Code Generation<br/>7 Commercial & Open-Source LLMs with Default Settings"]
C --> D["AST Parsing & Extraction<br/>Import / ImportFrom / Call / Attribute Nodes"]
D --> E["Two-Level Validity Verification<br/>PyPI Registry Normalization + Official API Documentation"]
E --> F["High-Risk Prompt Synthesis & Benchmark<br/>LIBHALLUBENCH Evaluation Framework"]
Key Designs¶
1. Realistic Prompt Variation Injection: Mining Authentic Intent and Controlled Errors
To avoid synthetic prompt biases, the study mined over 20,000 questions from Software Recommendations StackExchange (SRSE). By filtering for 3,917 software and library recommendation threads, extracting n-grams, and clustering sentence embeddings via Agglomerative Clustering, the authors identified nine predominant adjective descriptors (e.g., open-source, alternative, best, simple, easy, fast, lightweight, modern) and frequent temporal constraints (e.g., libraries "from 2023/2024/2025 or later"). In tandem, controlled user mistakes were systematically generated across three severity tiers: 1-character typos (Levenshtein distance 1, e.g., numpi for numpy), multi-character typos (edit distance 2โ8, e.g., graphly for plotly), and plausible but completely fabricated library/member names matching task semantics (e.g., GaussianTools).
2. Two-Level Validity Verification: AST Parsing Grounded in Dynamic Oracles
The evaluation distinguishes between two verifiable failure categories: library name hallucinations (non-existent packages) and library member hallucinations (invalid members within valid packages). Generated responses are parsed using Python's native ast module to traverse Import and ImportFrom nodes (accounting for import aliasing and namespace packages) and Call/Attribute nodes. Library names are checked against the live Python Package Index (PyPI) registry using standard package name normalization (handling hyphens, underscores, and case insensitivity); member validity is audited against scraped documentation of the latest library release. Manual validation over 200 high-risk responses confirmed the detection pipeline achieved 95.8% precision and 100% recall.
3. High-Risk Prompt Synthesis & Benchmark: LIBHALLUBENCH
To facilitate reproducible evaluation of code generation models and mitigation techniques, the authors constructed LIBHALLUBENCH from the highest-risk conditions identified in the empirical analysis. Derived from 321 BigCodeBench seed tasks across diverse computational domains, the benchmark comprises 4,173 labeled prompts partitioned into three splits: Control (baseline tasks with neutral prompts), Describe (prompts containing qualitative and temporal descriptors), and Specify (prompts explicitly requesting specific libraries while injecting typos or fabricated entities). Because library existence is verified against a dynamic package registry rather than fragile unit-test execution sandboxes, the benchmark remains lightweight, robust, and resilient to environment decay.
Key Experimental Results¶
Main Results¶
The main study evaluated seven representative LLMs: GPT-4o-mini, GPT-5-mini, Ministral-8B, Qwen-2.5-Coder (32B), Llama-3.3 (70B), DeepSeek-V3.1, and Claude-4.5-Haiku. Performance is assessed using Response Hallucination Rate (RHR) and Task Hallucination Rate (THR).
| Model | Baseline (No Description) RHR | Time-based (From 2025) RHR | 1-char Typo RHR | Multi-char Typo RHR | Fake Library Name RHR |
|---|---|---|---|---|---|
| GPT-4o-mini | 0.00% | 53.79% | 2.65% | 25.39% | 81.26% |
| GPT-5-mini | 0.00% | 32.81% | 14.43% | 65.06% | 94.55% |
| Ministral-8B | 0.00% | 15.68% | 3.89% | 21.81% | 85.67% |
| Qwen-2.5-Coder | 0.10% | 31.26% | 0.26% | 4.00% | 10.44% |
| Llama-3.3 | 0.00% | 0.62% | 0.42% | 4.93% | 35.10% |
| DeepSeek-V3.1 | 0.00% | 24.92% | 1.09% | 24.51% | 51.14% |
| Claude-4.5-Haiku | 0.00% | 0.21% | 1.25% | 10.02% | 61.84% |
Task-level hallucination rates on LIBHALLUBENCH across benchmark splits (lower percentages indicate superior reliability):
| Model | Control Split | Describe Split | Specify Split | Overall Metric |
|---|---|---|---|---|
| Llama-3.3 (70B) | 0.0% | 2.1% | 18.6% | 10.4% |
| Qwen-2.5-Coder (32B) | 0.3% | 18.3% | 9.7% | 14.0% |
| Claude-4.5-Haiku | 0.0% | 1.3% | 31.4% | 16.3% |
| DeepSeek-V3.1 | 0.0% | 12.0% | 28.7% | 20.4% |
| Ministral-8B | 0.0% | 7.4% | 40.1% | 23.8% |
| GPT-4o-mini | 0.0% | 34.8% | 43.1% | 39.0% |
| GPT-5-mini | 0.0% | 12.0% | 67.5% | 39.8% |
Ablation Study¶
The authors evaluated four lightweight prompt engineering strategies to mitigate library name hallucinations under the highest-risk conditions (values in parentheses show absolute changes relative to the unmitigated baseline):
| Mitigation Strategy | GPT-4o-mini (From 2025) | GPT-4o-mini (Fake Library) | GPT-5-mini (From 2025) | GPT-5-mini (Fake Library) | Qwen-2.5-Coder (Fake Library) |
|---|---|---|---|---|---|
| Unmitigated Baseline | 53.79% | 81.26% | 32.81% | 94.55% | 10.44% |
| Chain-of-Thought | 60.33% (+6.54%) | 80.84% (-0.42%) | 33.96% (+1.14%) | 93.87% (-0.67%) | 39.10% (+28.66%) |
| Step-Back Prompting | 60.12% (+6.33%) | 77.31% (-3.95%) | 38.21% (+5.40%) | 90.39% (-4.15%) | 20.66% (+10.23%) |
| Self-Analysis | 40.60% (-13.19%) | 78.92% (-2.34%) | 31.05% (-1.77%) | 92.73% (-1.82%) | 8.57% (-1.87%) |
| Explicit-Check | 49.64% (-4.15%) | 77.41% (-3.84%) | 0.10% (-32.71%) | 62.36% (-32.19%) | 5.40% (-5.04%) |
Key Findings¶
- Conventional adjective descriptors (e.g., fast, lightweight) do not induce library name hallucinations (~0% hallucination rate); models simply ignore them and default to familiar libraries. However, descriptors implying rarity (e.g., "hidden gem", "lesser known") significantly inflate hallucination rates.
- Temporal demands represent a severe vulnerability trigger: asking for libraries "from 2025 or later" produces hallucinated imports in up to 84.74% of tasks (GPT-4o-mini), with models inventing highly plausible names such as timezoneify and math_quantum.
- Models exhibit pronounced sycophancy when handling user errors: rather than flagging typos or refusing invented package names, LLMs uncritically adopt them in generated code. Facing fake library names, GPT-5-mini and GPT-4o-mini complied in over 90% of tasks.
- Popular reasoning prompts are counterproductive for hallucination mitigation: Chain-of-Thought and Step-Back prompting frequently worsened library name hallucinations (e.g., Qwen-2.5-Coder hallucination rate on fake libraries increased from 10.44% to 39.10%), as internal reasoning steps often reinforced the model's ungrounded assumptions.
- While tool augmentation (a lightweight PyPI lookup tool) substantially curbs hallucinations, model response patterns diverge sharply: GPT-4o-mini invoked the tool on 98.65% of responses and reduced hallucinations to 2.49%, whereas GPT-5-mini invoked it on only 7.89% of responses and instead produced no code in over 90% of cases.
Highlights & Insights¶
- Discloses a direct mechanistic link between user-level prompt noise and supply chain vulnerabilities, demonstrating how LLM sycophancy actively amplifies typosquatting and slopsquatting risks.
- Dispels the assumption that general-purpose reasoning prompts improve code factuality; without external verification or explicit verification directives, reasoning chains merely rationalize fabricated entities.
- Identifies a generative ideation perspective: hallucinated packages often feature coherent, highly descriptive naming schemes (e.g., numpy-base64), reflecting latent developer requirements and highlighting unaddressed abstraction gaps in existing software ecosystems.
Limitations & Future Work¶
- The core benchmark is grounded in Python (BigCodeBench); although extended experiments on CodeInsight incorporate JavaScript and Rust, broader evaluations across proprietary enterprise libraries and niche domain DSLs remain unexplored.
- Library member verification evaluated validity against latest documentation snapshots; while explicit version declarations were handled via regex exemptions, minor version drift across implicit dependencies could introduce edge-case noise.
- Future safeguards must advance beyond basic prompt engineering to incorporate pre-generation tool calls, calibration mechanisms that expose knowledge cut-offs, and fine-tuning targeted at anti-sycophantic refusal.
Related Work & Insights¶
- vs CodeHalu / CodeMirage: Prior benchmarks focus on runtime crashes, logical bugs, and semantic deviations; this work isolates verifiable dependency-level hallucinations and systematically maps prompt-level triggers.
- vs CERT / Gorilla / RAG: While existing mitigations emphasize massive retrieval pipelines or API-specific fine-tuning, this study demonstrates the acute fragility of prompt-only interactions, emphasizing the need for lightweight defensive guardrails in resource-constrained environments.
Rating¶
- Novelty: โญโญโญโญโญ Groundbreaking analysis linking developer prompt variations directly to software supply chain risks (typosquatting and slopsquatting).
- Experimental Thoroughness: โญโญโญโญโญ Rigorous evaluation spanning seven premier LLMs, 4,173 benchmark prompts, multi-language validation, and prompt mitigation ablations.
- Writing Quality: โญโญโญโญโญ Exceptionally clear framing, rigorous operational definitions, and thorough empirical reporting.
- Value: โญโญโญโญโญ LIBHALLUBENCH and the automated verification suite provide an essential, open-source infrastructure for securing AI-assisted programming pipelines.