Hallucinations of Large Language Models in Cross-Language Code Translation: Phenomena and Mitigation
Organizations: National Key Laboratory for Novel Software Technology, and the School of Artificial Intelligence, Nanjing University, China
Abstract
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.
Figures & tables
| model | dataset | FRM | DCV | SCC | BSC | LKC | AKC | FKC | RDC | IFC | DC | PCD |
| Qwen 2.5 | Avatar | 33.52% | 17.85% | 51.37% | 8.01% | 0.55% | 10.20% | 18.76% | 2.55% | 12.39% | 14.94% | 29.87% |
| CodeNet | 21.41% | 32.85% | 54.26% | 6.44% | 0.21% | 11.43% | 18.09% | 0.62% | 12.27% | 14.76% | 27.65% | |
| Transcoder | 19.08% | 62.61% | 81.69% | 2.43% | 0.26% | 3.46% | 6.15% | 0.38% | 6.40% | 5.38% | 12.16% | |
| DS-Coder | Avatar | 36.81% | 15.54% | 52.35% | 9.00% | 1.84% | 7.98% | 18.81% | 4.29% | 12.88% | 11.66% | 28.83% |
| CodeNet | 29.22% | 24.94% | 54.16% | 11.84% | 2.02% | 5.79% | 19.65% | 1.76% | 11.84% | 12.59% | 26.20% | |
| Transcoder | 25.72% | 60.49% | 86.21% | 1.44% | 0.72% | 2.01% | 4.17% | 1.15% | 3.45% | 5.03% | 9.63% |
Code Translation
Code translation converts programs between languages, frameworks, and representations while aiming to preserve behavior and sometimes improve safety or performance.
See all 41 papers