Hallucinations of Large Language Models in Cross-Language Code Translation: Phenomena and Mitigation
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.
Create a lesson
Metadata
- Categories
- cs.SE, cs.AI
Related papers
Scientific Coding with Large Language Models: Evidence from Finite Element Analysis
Shared title terminologycs.SEErfan Hamdi, Joel Shor, Emma Lejeune
Beyond Benchmarks: Evaluating Large Language Models and Agentic Patterns for Static Code Analysis Warning Classification
Shared title terminologycs.SELakmal Deshapriya, Sherlock A. Licorish, Brendon J. Woodford et al.
TrustedCode: Evaluating Large Language Models for Developing Trusted Code through Automated Program Verification
Shared title terminologycs.SEMuhammad Danial, Muhammad Taimoor Khan
JANUS: Agentic Cross-Language Fuzzing Harness Generation for Android Native Libraries
Shared title terminologycs.SEXiangyu Zhang, Yucheng Su, Lingling Fan et al.
Code Comprehensor: Hierarchical Documentation and Cross-Modal Retrieval for Large Repositories
Shared title terminologycs.SESujoy Roychowdhury, Joseph H. R. Isaac, Jiangning Gao et al.
From Industrial Product Documentation to Automated Digital Product Passport Generation: An Experimental Study of Large Language Models
Shared title terminologycs.SEMarco Buecheler, Grant Richards, Nathan Hartman