Manuscript Offprint

arXiv:2610.27868

Hallucinations of Large Language Models in Cross-Language Code Translation: Phenomena and Mitigation

Yali Du, Yunji Zhang, Ming Li

YALI DU, National Key Laboratory for Novel Software Technology, and the School of Artificial Intelligence, Nanjing University, China

YUNJI ZHANG, National Key Laboratory for Novel Software Technology, and the School of Artificial Intelligence, Nanjing University, China

MING LI*, National Key Laboratory for Novel Software Technology, and the School of Artificial Intelligence, Nanjing University, China

Code translation converts source code from one programming language to another while preserving its functionality. Although Large Language Models (LLMs) have improved code translation, their outputs still contain hallucinations: generated code that conflicts with the source program, violates target-language facts, or introduces contextually inconsistent dependencies. These failures are especially harmful in translation because small semantic deviations can silently change program behavior. Existing hallucination studies mainly focus on natural-language generation or code generation, leaving the forms, distribution, and mitigation of hallucinations in cross-language code translation insufficiently characterized. This paper presents a systematic empirical study of LLM hallucinations in code translation. Across three benchmark datasets, six language pairs, and seven LLMs, we construct a taxonomy with three major categories: Semantic Consistency Conflicts, Factual Knowledge Conflicts, and Project Context Deviations. We manually annotate 13,919 hallucination cases and analyze how hallucination types vary across models, datasets, and language directions. Guided by the taxonomy, we further propose HaluCodeTran, a mitigation framework that combines BM25-based retrieval of relevant translation examples with taxonomy-aware reflective refinement. The study provides an execution-tested characterization of code-translation hallucinations and a practical baseline for building more reliable LLM-based code migration tools.

CCS Concepts: • Software and its engineering → Software maintenance tools.

Additional Key Words and Phrases: Hallucination, Code Translation, Large Language Models

ACM Reference Format:

Yali Du, Yunji Zhang, and Ming Li. 2026. Hallucinations of Large Language Models in Cross-Language Code Translation: Phenomena and Mitigation. 1, 1 (October 2026), 22 pages.

1 Introduction

Code translation, also known as source-to-source compilation or transpilation, involves converting code from one programming language to another while preserving its original functionality. This technique plays a critical role in modern software development. For example, many industries spend hundreds of millions of dollars to convert code written in older programming languages (e.g., FORTRAN and COBOL) to newer ones (e.g., Java, C++) [41]. By translating code into modern programming languages, organizations can reduce maintenance costs, improve reliability, and

∗^{*}Ming Li is the corresponding author.

Authors' Contact Information: Yali Du, National Key Laboratory for Novel Software Technology, and the School of Artificial Intelligence, Nanjing University, China, duyl@lamda.nju.edu.cn; Yunji Zhang, National Key Laboratory for Novel Software Technology, and the School of Artificial Intelligence, Nanjing University, China, zhangyj@lamda.nju.edu.cn; Ming Li, National Key Laboratory for Novel Software Technology, and the School of Artificial Intelligence, Nanjing University, China, lim@lamda.nju.edu.cn.

  1. ACM XXXX-XXXX/2026/10-ART https://doi.org/

Du et al.

enhance non-functional attributes like security and performance [34]. Automatic code translation can alleviate the burden of program migration and facilitate the development of software systems, which has drawn sustained attention from neural, LLM-based, and project-scale migration research [2, 9, 32, 34, 40, 58]. Recent advancements in Large Language Models (LLMs) have improved code translation and code-generation capabilities, while also raising new concerns about correctness, calibration, and domain-specific reliability [12, 44, 46, 58].

However, the translation quality of current LLM-driven approaches is still unsatisfactory, partly because generated programs can contain hallucinations: code fragments that conflict with source-program requirements, violate target-language facts, or introduce contextually inconsistent dependencies. These hallucinations can lead to functional errors, incomprehensible code, deprecated or misused APIs, and security vulnerabilities, undermining the reliability of translated codebases [34, 52–54, 65]. Most existing hallucination research focuses on Natural Language Generation (NLG) tasks, such as question answering [42] and summarization [18, 37]. Recent software-engineering studies have begun to characterize hallucinations, code-generation errors, and API misuse in LLM-based programming tasks [24, 27, 52, 54, 65], but code translation introduces a different failure surface. Unlike intent-to-code generation, code translation must preserve a structured source program while mapping syntax, types, libraries, APIs, and idioms into a different target language. A generated program may compile and still violate the source semantics, while a superficially small API or data-flow mismatch can completely change behavior. This makes hallucination in code translation a distinct empirical object rather than a direct instance of natural-language or code-generation hallucination.

To address this gap, we conduct an empirical study focused on hallucinations in cross-language code translation. Through open coding and empirical validation, we develop a taxonomy of code translation hallucinations, categorizing errors into three main types: (1) Semantic Consistency Conflicts (e.g., functional requirement mismatches, data inconsistencies), (2) Factual Knowledge Conflicts (e.g., syntax errors, incorrect library/API usage), (3) Project Context Deviations (e.g., dead code, dependency conflicts). We instantiate this taxonomy on outputs from seven LLMs, including GPT-4o [16] and Qwen 2.5-Coder [57], over three widely used code translation datasets: CodeNet [36], Avatar [2], and Transcoder [40]. The resulting annotations provide a basis for analyzing which hallucination categories dominate code translation failures and which model/language-pair conditions make them more likely.

Guided by the taxonomy, we propose HaluCodeTran, a mitigation framework that combines retrieval-based contextual grounding with reflective iterative refinement. The retrieval component provides relevant source-target translation examples to reduce factual and idiomatic uncertainty, while the reflection component asks the model to inspect the intermediate translation against the source program and taxonomy categories before producing a revision. On CodeNet, HaluCodeTran improves computational accuracy by up to 23 percentage points and reduces hallucination cases by up to 32% compared with vanilla translation, outperforming Chain-of-Thought, Chain-of-Verification, and Retrieval-Augmentation in average accuracy for the evaluated models.

We summarize our contributions as follows:

• We define hallucination in code translation and establish a taxonomy that distinguishes semantic consistency conflicts, factual knowledge conflicts, and project context deviations.

• We construct a manually labeled hallucination dataset covering 13,919 cases from seven LLMs, three datasets, and six translation directions, enabling fine-grained analysis across models and language pairs.

• We propose HaluCodeTran, a taxonomy-aware mitigation framework that combines retrieval-augmented examples with reflective refinement, and evaluate it against representative prompting and retrieval baselines.

References (34)