arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2601.19106cs.SEcs.AI

通过确定性AST分析检测和纠正LLM生成的代码中的幻觉

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

William & Mary(威廉玛丽大学)

Dipin Khati, Daniel Rodriguez-Cardenas, Paul Pantzer, Denys Poshyvanyk

AI总结:

本文提出通过确定性AST分析检测并纠正LLM生成代码中的知识冲突幻觉,实现高精度和召回率的自动修复。

摘要:

大型语言模型(LLM)在代码生成中提高了生产力,但经常引入知识冲突幻觉(KCHs),即微妙的语义错误,如不存在的API参数,这些错误逃避了检查器并导致运行时失败。现有的缓解方法如受限制的解码或非确定性的LLM-in-the-loop修复往往对这些错误不可靠。本文研究是否可以使用确定性的静态分析框架可靠地检测并自动纠正KCHs。我们提出了一种后处理框架,将生成的代码解析为抽象语法树(AST),并将其与通过库内省生成的动态知识库(KB)进行验证。这种非执行方法使用确定性规则来查找并修复API和标识符级别的冲突。在手动整理的200个Python片段数据集上,我们的框架检测KCHs的精确度为100%,召回率为87.6%(0.934 F1分数),并成功自动纠正了所有识别出的幻觉中的77.0%。我们的发现表明,这种确定性的后处理方法是概率性修复的可行且可靠的替代方案,为可信的代码生成提供了清晰的道路。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑