What Are Hallucinations in Code?
In general NLP, a hallucination occurs when a model generates content that is nonsensical, unfaithful to the source, or factually incorrect. In code generation, hallucinations take on a uniquely dangerous character: they produce syntactically valid code that compiles or runs — but behaves incorrectly. Code hallucinations are worse because they create a verifiability gap (require execution or deep expertise to detect), they have downstream impact (runtime crashes, security vulnerabilities, silent data corruption), and they create false confidence (syntactically correct code creates a strong illusion of correctness).