{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/llm-hallucinations-in-practical-code","title":"LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigation","arxiv_id":"2409.20550","date":"2024-09-30","proceeding":null,"authors":["Ziyao Zhang","Yanlin Wang","Chong Wang","Jiachi Chen","Zibin Zheng"],"abstract":"Code generation aims to automatically generate code from input requirements, significantly enhancing development efficiency. Recent large language models (LLMs) based approaches have shown promising results and revolutionized code generation task. Despite the promising performance, LLMs often generate contents with hallucinations, especially for the code generation scenario requiring the handling of complex contextual dependencies in practical development process. Although previous study has analyzed hallucinations in LLM-powered code generation, the study is limited to standalone function generation. In this paper, we conduct an empirical study to study the phenomena, mechanism, and mitigation of LLM hallucinations within more practical and complex development contexts in repository-level generation scenario. First, we manually examine the code generation results from six mainstream LLMs to establish a hallucination taxonomy of LLM-generated code. Next, we elaborate on the phenomenon of hallucinations, analyze their distribution across different models. We then analyze causes of hallucinations and identify four potential factors contributing to hallucinations. Finally, we propose an RAG-based mitigation method, which demonstrates consistent effectiveness in all studied LLMs. The replication package including code, data, and experimental results is available at https://github.com/DeepSoftwareAnalytics/LLMCodingHallucination","url_abs":"https://arxiv.org/abs/2409.20550v2","url_pdf":"https://arxiv.org/pdf/2409.20550v2.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"llm-hallucinations-in-practical-code","repo_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"none","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"code-generation","task_name":"Code Generation"},{"task_slug":"hallucination","task_name":"Hallucination"},{"task_slug":"rag","task_name":"RAG"}],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2409.20550","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2409.20550"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran":5,"unverified":2},"by_repo_kind":{"official":{"samples":7,"ran":5,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"c8230eeeb2532352","entry":"clean_code","repo":"deepsoftwareanalytics/llmcodinghallucination","repo_kind":"official","path":"eval_original.py","file_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination/blob/HEAD/eval_original.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c8230eeeb2532352"}},{"code_sha256_prefix":"c4a538a65aa5b254","entry":"clean_docstring","repo":"deepsoftwareanalytics/llmcodinghallucination","repo_kind":"official","path":"eval_original.py","file_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination/blob/HEAD/eval_original.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c4a538a65aa5b254"}},{"code_sha256_prefix":"1a8cc6f72a5c0992","entry":"clean_pad","repo":"deepsoftwareanalytics/llmcodinghallucination","repo_kind":"official","path":"model.py","file_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination/blob/HEAD/model.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"1a8cc6f72a5c0992"}},{"code_sha256_prefix":"c7261b10bd59d790","entry":"clean_str","repo":"deepsoftwareanalytics/llmcodinghallucination","repo_kind":"official","path":"model.py","file_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination/blob/HEAD/model.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c7261b10bd59d790"}},{"code_sha256_prefix":"fae4a6b218aa55fb","entry":"compute_EM","repo":"deepsoftwareanalytics/llmcodinghallucination","repo_kind":"official","path":"compute_score.py","file_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination/blob/HEAD/compute_score.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"fae4a6b218aa55fb"}},{"code_sha256_prefix":"1673a2948bb1d361","entry":"init_codegen","repo":"deepsoftwareanalytics/llmcodinghallucination","repo_kind":"official","path":"model.py","file_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination/blob/HEAD/model.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"1673a2948bb1d361"}},{"code_sha256_prefix":"297de4419cd21f7b","entry":"predict","repo":"deepsoftwareanalytics/llmcodinghallucination","repo_kind":"official","path":"eval_original.py","file_url":"https://github.com/deepsoftwareanalytics/llmcodinghallucination/blob/HEAD/eval_original.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"297de4419cd21f7b"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}