Manuscript Offprint

arXiv:2609.03267

Refusing the Impossible: A Taxonomy and Benchmark for Code Hallucination in Large Language Models

Vishnu Asutosh Dasu, Ashish Kundu, Gang Tan

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original. This text is shared under the same licence.

Abstract

Large language models (LLMs) often produce code that looks plausible but is not grounded in reality. The code may import packages that do not exist or claim to implement algorithms that violate proven theorems, while still compiling and running. We study code hallucination as ungrounded generation and separate it from ordinary code error (bugs in otherwise grounded programs). We propose a taxonomy with three dimensions: groundedness (absolute violations of universal truths vs. relative fabrications of contingent or ecosystem-specific facts), manifestation level (syntactic, semantic, or factual), and behavior (from confident fabrication to degenerate output), organized into a severity ordering. We build an adversarial suite of deliberately unsatisfiable tasks where the correct response is to refuse and categorize the responses under our taxonomy. The suite contains 270 prompts across six languages and 24 subcategories, paired with 91 matched solvable controls, and responses are judged by a two-tier protocol validated against human labels (82% agreement, κ=0.73\kappa=0.73). Across twelve open-weight code and reasoning models (4,332 judged responses), models produce ungrounded code on about 60% of unsatisfiable prompts and refuse only 27%, while wrongly refusing 0% of the solvable controls.

1 Introduction

Code generation with LLMs has become ubiquitous and is widely used to develop production systems. Agentic systems such as GitHub Copilot and general-purpose models are used daily by developers [18, 13, 2, 5]. Code quality is commonly assessed by execution-based verification (e.g., pass@k on HumanEval/APPS/MBPP [4, 6, 3]) and real-world issue resolution (e.g., SWE-bench [7]). However, many failures go beyond ordinary bugs.

Recent work has begun to characterize code-specific hallucinations. Liu et al. [12] identify twelve subcategories of hallucination organized into three high-level types. Agarwal et al. [1] introduce CodeMirage, a benchmark of 1,137 hallucinated snippets categorized by syntactic errors, logical flaws, and security vulnerabilities. Tian et al. [16] propose CodeHaluEval, an execution-based framework that classifies hallucinations by observable symptoms. Zhang et al. [21] study hallucinations in repository-level generation and propose RAG-based mitigation. A recent survey [11] covers taxonomies, benchmarks, and mitigations. Other work triggers hallucinations with adversarial prompts. Rahman and Kundu [14] craft prompts that reliably induce ungrounded code, and Twist et al. [17] show that misspellings or future-dated library requests trigger fabricated imports at high rates. At ecosystem scale, Spracklen et al. [15] find that roughly 5% (commercial models) to 22% (open-weight models) of packages recommended by sixteen code LLMs across 576,000 generated samples do not exist. These efforts focus on inducing or measuring hallucinations. We focus on characterizing them with a principled taxonomy and on testing whether models can recognize and refuse impossible tasks.

39th Conference on Neural Information Processing Systems (NeurIPS 2025).

While these efforts advance detection and benchmarking, they share a common limitation. They do not separate grounded errors from ungrounded hallucinations. An off-by-one error caused by a misunderstood specification is a bug in a grounded intent. Generating code that imports a nonexistent library or claims to implement a fictional algorithm is ungrounded. The two call for different fixes. Errors are addressed by testing and static analysis, while hallucinations require recognizing what exists and what is possible. Mixing the two hides root causes and makes targeted mitigation harder. We propose a taxonomy that cleanly separates code error (bugs in grounded programs) from code hallucination (ungrounded generation).

Two regimes of failure. We consider two different failure regimes: (1) Natural prompts describing solvable tasks, where a model may generate grounded code that is simply wrong (e.g., off-by-one errors), and (2) Adversarial prompts that are unsatisfiable by design, such as requests built on fictional algorithms, nonexistent APIs, violated impossibility theorems, or internally contradictory constraints. Here the correct response is to refuse or flag the problem rather than generate code.

Contributions. We make three contributions:

  1. A definition and boundary: We distinguish code hallucination (ungrounded generation) from code error (grounded but buggy), including practical boundary tests.

  2. A multi-dimensional taxonomy: Dimension 1 classifies what truth is violated (absolute vs. relative). Dimension 2 classifies how the hallucination manifests (syntactic, semantic, or factual). Dimension 3 classifies how the model responds (fabrication, hedging, task substitution, degeneration). We organize these into a severity ordering that supports principled comparison.

  3. An evaluation with new findings: We instantiate the adversarial regime as a suite of unsatisfiable prompts with matched solvable controls and a human-validated judge, and evaluate twelve open-weight models. We show five findings: 1) Refusal follows surface plausibility rather than depth of impossibility. 2) As models become more capable, hallucinations drop on theoretical impossibilities but barely on fabricated packages and APIs. 3) Models learn to notice impossibility before they learn to act on it. 4) Phrasing alone shifts hallucination by 17 points. 5) Failure is mostly a property of the prompt.

References (19)