AI
Static analysis catches only half of AI code hallucinations, new study finds
An empirical analysis reveals the limits and potential of static analysis tools in detecting when large language models invent nonexistent library features.
Large language models frequently fabricate library features when generating code, with researchers estimating that somewhere between 8% and 40% of code outputs incorporate functions or modules that don't actually exist. A new empirical study examines whether static analysis—a conventional software testing approach—can reliably catch these mistakes.
The Scale of the Hallucination Problem#
When tasked with generating code that relies on external libraries, LLMs struggle significantly more than with general programming tasks. The problem becomes apparent when evaluated against benchmarks that specifically test library-dependent code generation, where fictional library calls represent a substantial portion of failures.
What Static Analysis Can and Cannot Do#
Researchers assessed how well static analysis tools—automated inspection techniques that examine code without executing it—could identify these hallucinations. Their findings paint a mixed picture:
- Static analysis successfully detected 16–70% of all errors, depending on the model and dataset
- Detection rates for library hallucinations specifically ranged from 14–85%
- Through detailed manual review, the team established an upper bound on what these methods could theoretically achieve: 48.5–77% of cases
The variation depends heavily on which LLM generated the code and which dataset was used for testing, suggesting that different models and problem domains present distinct technical challenges.
Why Static Analysis Has Limits#
Some hallucinations lie beyond what static analysis can plausibly identify. Code may pass formal inspection rules while still invoking functions that don't exist in a library, or the errors may be semantic rather than syntactic—visible only at runtime or through deeper domain knowledge.
The Practical Takeaway#
Static analysis emerges as an inexpensive first line of defense that can catch a meaningful share of hallucinations without requiring sophisticated runtime validation or human review. However, the technology represents only a partial solution, with inherent ceilings on effectiveness that no amount of refinement will overcome. The research quantifies both the opportunity and the limitations of this approach for making AI-generated code more reliable.

