← Latest papers
💬 NLP

Library Hallucinations in LLM-Generated Code: A Risk Analysis Grounded in Developer Queries

This paper presents the first systematic study of how realistic developer prompt variations, such as misspellings and fabricated names, trigger library hallucinations in LLM-generated code, revealing significant vulnerabilities and introducing the LibHalluBench benchmark to enable reproducible evaluation and mitigation of these risks.

Original authors: Lukas Twist, Jie M. Zhang, Mark Harman, Helen Yannakoudakis

Published 2026-05-20
📖 5 min read🧠 Deep dive

Original authors: Lukas Twist, Jie M. Zhang, Mark Harman, Helen Yannakoudakis

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you have a very talented, confident, but slightly forgetful assistant who is an expert at writing computer code. You ask them to build a specific tool, and they happily start typing. However, this assistant has a dangerous habit: sometimes, when they don't know the exact name of a tool they need, they just make one up. They invent a library (a pre-made collection of code) that doesn't exist, but they write it down with such confidence that you might not realize it's fake until you try to use it and it breaks.

This paper is a deep dive into why this assistant makes up these fake tools, specifically when you give them slightly different instructions or make small mistakes.

Here is the breakdown of their findings using simple analogies:

1. The "Time Travel" Trap

The researchers found that if you ask your assistant for a tool "from 2025" (a year that hasn't happened yet or is beyond their training), the assistant gets very nervous and starts hallucinating wildly.

  • The Analogy: Imagine asking a librarian for a book published in the future. Instead of saying, "I don't have that," the librarian might invent a title, a cover, and a summary that sounds perfect but doesn't exist.
  • The Result: When asked for libraries "from 2025," up to 85% of the time, the assistant invented a fake library. They are terrible at knowing their own "knowledge cutoff" (the date they stopped learning).

2. The "Typo" Trap (Sloppy Spelling)

If you accidentally misspell a real library name, the assistant often doesn't correct you. Instead, they double down and pretend the misspelled version is real.

  • The Analogy: Imagine you ask a chef for "Spagetti" (missing the 'h'). Instead of saying, "Did you mean Spaghetti?", the chef might just grab a box labeled "Spagetti" from a shelf they made up, or worse, they might invent a new dish called "Spagetti" that doesn't exist.
  • The Result:
    • A one-letter typo (like numpi instead of numpy) caused fake libraries to be used in 26% of cases.
    • A fake library name (something that sounds real but isn't, like "GaussianTools") was accepted and used in up to 99% of cases. The assistant is so eager to please that they will use a fake tool rather than tell you it doesn't exist.

3. The "Adjective" Trap (What Doesn't Work)

Interestingly, if you ask for a library that is "fast," "easy," or "modern," the assistant usually ignores those words and just picks a standard, real library they know.

  • The Analogy: If you ask for a "fast car," the assistant just gives you a standard Toyota. They don't invent a "super-fast-flying-car" because they know those adjectives are vague.
  • The Result: Adjectives like "fast" or "lightweight" rarely caused hallucinations. The danger comes from specific constraints (like dates) or errors (like typos).

4. The "Reasoning" Trap (Thinking Too Hard)

The researchers tried to fix this by telling the assistant to "think step-by-step" or "check your facts" before answering.

  • The Analogy: It's like telling a nervous student, "Take a deep breath and think hard about this math problem." Sometimes, this helps. But often, the student just gets more confident in their wrong answer because they spent more time justifying it.
  • The Result: These "reasoning" tricks didn't work consistently. In fact, they sometimes made the hallucinations worse. The assistant would confidently explain why their fake library was real, making it harder for you to catch the mistake.

5. The "Tool" Solution (Checking the Phone Book)

The researchers tested giving the assistant a simple tool: a button that checks if a library actually exists in the official database (PyPI).

  • The Analogy: Instead of the assistant guessing, you give them a phone book. If they say "I'll use Spagetti," they check the book, see it's not there, and then say, "Oh, that doesn't exist."
  • The Result: This helped a lot, but it wasn't a magic fix. The assistant still sometimes forgot to check the book, or checked it but decided to ignore the result and use the fake name anyway.

The Big Takeaway: LIBHALLUBENCH

Because this is such a big problem, the authors created a new test called LIBHALLUBENCH.

  • The Analogy: Think of this as a "driving test" specifically designed to see if a self-driving car will hallucinate a fake stop sign. It contains thousands of tricky questions (like "give me a library from 2025" or "use this misspelled name") to systematically test how likely different AI models are to make up fake tools.

Why Should You Care?

If you are a developer using AI to write code, and the AI invents a fake library, your code will crash when you try to run it. Even worse, if a hacker creates a fake library that matches the one the AI invented (a trick called "slopsquatting"), your system could get infected with malware.

In short: AI code generators are great, but they are dangerously confident when they don't know the answer. They will invent fake tools to satisfy your request, especially if you ask for something from the future or make a small typo. We need better ways to stop them from making things up.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →