Detecting and Correcting Hallucinations in LLM-Generated Code via Deterministic AST Analysis
Dipin Khati, Daniel Rodriguez-Cardenas, Paul Pantzer, Denys Poshyvanyk

TL;DR
This paper presents a deterministic static-analysis framework that detects and auto-corrects hallucinations in LLM-generated code by parsing code into ASTs and validating against a dynamically generated knowledge base, achieving high precision and recall.
Contribution
It introduces a novel static-analysis method for detecting and fixing hallucinations in code generated by LLMs, outperforming probabilistic approaches.
Findings
Detected hallucinations with 100% precision and 87.6% recall.
Successfully auto-corrected 77.0% of hallucinations.
Demonstrated reliability of deterministic post-processing for trustworthy code generation.
Abstract
Large Language Models (LLMs) for code generation boost productivity but frequently introduce Knowledge Conflicting Hallucinations (KCHs), subtle, semantic errors, such as non-existent API parameters, that evade linters and cause runtime failures. Existing mitigations like constrained decoding or non-deterministic LLM-in-the-loop repair are often unreliable for these errors. This paper investigates whether a deterministic, static-analysis framework can reliably detect \textit{and} auto-correct KCHs. We propose a post-processing framework that parses generated code into an Abstract Syntax Tree (AST) and validates it against a dynamically-generated Knowledge Base (KB) built via library introspection. This non-executing approach uses deterministic rules to find and fix both API and identifier-level conflicts. On a manually-curated dataset of 200 Python snippets, our framework detected KCHs…
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
Taxonomy
TopicsSoftware Engineering Research · Advanced Malware Detection Techniques · Adversarial Robustness in Machine Learning
