Detecting and Correcting Hallucinations in LLM-Generated Code via Deterministic AST Analysis

Fuente: arXiv
Saved in:
Bibliographic Details
Main Authors: Khati, Dipin, Rodriguez-Cardenas, Daniel, Pantzer, Paul, Poshyvanyk, Denys
Format: Preprint
Published: 2026
Subjects:
Online Access:
Tags: Add Tag
No Tags, Be the first to tag this record!
_version_ 1866910001843404800
author Khati, Dipin
Rodriguez-Cardenas, Daniel
Pantzer, Paul
Poshyvanyk, Denys
author_facet Khati, Dipin
Rodriguez-Cardenas, Daniel
Pantzer, Paul
Poshyvanyk, Denys
contents 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 with 100\% precision and 87.6\% recall (0.934 F1-score), and successfully auto-corrected 77.0\% of all identified hallucinations. Our findings demonstrate that this deterministic post-processing approach is a viable and reliable alternative to probabilistic repair, offering a clear path toward trustworthy code generation.
format Preprint
id arxiv_https___arxiv_org_abs_2601_19106
institution arXiv
publishDate 2026
record_format arxiv
spellingShingle Detecting and Correcting Hallucinations in LLM-Generated Code via Deterministic AST Analysis
Khati, Dipin
Rodriguez-Cardenas, Daniel
Pantzer, Paul
Poshyvanyk, Denys
Software Engineering
Artificial Intelligence
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 with 100\% precision and 87.6\% recall (0.934 F1-score), and successfully auto-corrected 77.0\% of all identified hallucinations. Our findings demonstrate that this deterministic post-processing approach is a viable and reliable alternative to probabilistic repair, offering a clear path toward trustworthy code generation.
title Detecting and Correcting Hallucinations in LLM-Generated Code via Deterministic AST Analysis
topic Software Engineering
Artificial Intelligence
url https://arxiv.org/abs/2601.19106