Editor’s introduction
The study evaluates TransAGENT, a multi-agent system designed to improve large language model-based code translation by localizing and correcting errors during the process. It compares performance against existing methods like UniTrans and Agentless, measuring translation accuracy and program repair effectiveness. TransAGENT outperforms UniTrans by up to 33.3% in accuracy and achieves a 56.7% average improvement in repair performance. The Syntax Error Fixer and Semantic Error Fixer components show consistent gains, with the Syntax Error Fixer achieving up to 27.2% better performance in Python-to-Java translation compared to UniTrans. These improvements are attributed to refined planning strategies and fine-grained fault localization. The system also demonstrates strong generalizability across different large language models, with performance gains observed in tasks like Java-to-C++ and C++-to-Python. Evaluation is based on a newly constructed benchmark to reduce data leakage risks and includes a comparison with SDA-Trans, a learning-based method with symbolic analysis.
Introduction
Code translation aims at transforming the code from one programming language (i.e., source program) to another (i.e., target program) while still preserving the same functionality. Code translation is prevalent in software development and maintenance, given the needs of performance optimization, system modernization, or technology transitions. While manually performing code translation can be time-consuming and error-prone, various automated code translation techniques (i.e., transpilers) have been proposed.
Traditional rule-based code translation relies on manually written transformation rules to convert source code into target code. However, crafting these rules requires extensive effort, and the resulting target programs often suffer from poor readability and usability. To address this issue, a series of learning-based code translation methods has been proposed to enhance translation quality. These methods train models on large amounts of parallel data (i.e., paired source and target programs), enabling the models to learn translation patterns and mappings between different languages. However, high-quality parallel data for training is often scarce in practice, and the process of model training is also very time-consuming. For example, training the TransCoder model requires 32 V100 GPUs for 12 days.
Recent advancements in large language models (LLMs) further enhance learning-based code translation. The translation paradigm has also shifted from “train-then-translate” to “translate-then-fix”, where LLMs first translate the source into the target program and then fix translation errors. Translation errors in target programs fall into two categories: syntax errors, which violate the grammar of the target language, and semantic errors, where the program produces outputs that differ from the source program for the same input. Effective use of LLMs for error repair requires precise localization and concrete guidance. Syntax errors can be detected by compilers, but the suggested fixes are often not straightforward. Semantic errors are difficult to locate, as they manifest only as output discrepancies without causing compilation or runtime failures.
However, existing methods rely solely on end-to-end LLM reasoning to fix translation errors, which limits their effectiveness. For example, UniTrans, the latest LLM-based translation approach, leverages function-level test cases to guide LLMs in repairing semantic errors in the target program. Since LLMs cannot accurately access the dynamic runtime behavior of the target program, this prevents the detection of fine-grained logical discrepancies and the pinpointing of the exact location to repair. Consequently, UniTrans produces only a marginal accuracy gain in the error-fixing stage, increasing from 31.25% to 31.90% on the Java-to-Python task with Llama-7B.
To enhance LLM in code translation, particularly for fixing syntax and semantic errors in the target program, we propose TransAGENT, an LLM-based multi-agent system. The main insight of TransAGENT is to pinpoint the error locations in the target program and provide specific fix suggestions, thus reducing the difficulty of error fixing for LLMs. TransAGENT employs four collaborative agents: Initial Code Translator, Syntax Error Fixer, Code Aligner, and Semantic Error Fixer. First, Initial Code Translator generates test cases and an initial target program for the given source program; Second, Syntax Error Fixer captures compiler error messages, converts them into more specific fix suggestions, and leverages LLM to fix the syntax errors; Third, Code Aligner divides the source program into blocks using the control flow graph (CFG) and then maps each block to the target program with LLM; Lastly, based on the mapped blocks between the source and target program, Semantic Error Fixer localizes the error block in the target program which exhibits different runtime behaviors from its aligned block in the source program, and then uses LLMs to specifically fix the error block with the observed runtime difference.
Approach
In particular, whenever the target program passes all the generated tests, the workflow terminates, and the target program is returned as the final target program; otherwise, the workflow proceeds to fix the syntax or semantic errors of the target program.
Benchmark
Limitations of Existing Benchmarks. Although there are many existing code translation benchmarks, they have the following limitations. First, all existing benchmarks suffer from potential data leakage issues, as their translation tasks are constructed from public programming competitions by the training data timestamp of the most recent LLMs (e.g., the most widely-used evaluation dataset TransCoder-ST is created in 2020). Second, some benchmarks involve the translation between only two languages, such as the Java-C# benchmark CodeTrans or the Java-Python benchmark AVATAR, which limits the generalization of evaluation. Third, some benchmarks (e.g., CodeNet ) suffer from quality issues, and half of its tasks are manually identified as incorrect code by experts, thus can harm the soundness of the evaluation.
New Benchmark Construction. To mitigate the aforementioned limitations, particularly the critical issue of data leakage, we establish a novel benchmark for code translation. Our selection of programming languages for this benchmark focuses on Java, Python, and C++. This choice is primarily driven by their long-standing and consistent presence among the top three most popular languages according to the TIOBE index. Furthermore, this aligns with the prevailing focus of prior research in code translation, which predominantly concentrates on translation among these three languages. Specifically, from the programming competition websites (e.g., LeetCode and GeeksforGeeks ), we collect the solutions of programming tasks for these three programming languages, which are released after August 2023. As the solutions in these websites typically come with only two or three test cases, which can be insufficient for guaranteeing the semantic correctness of code, we further leverage gpt-4o-mini to generate 10 additional test cases per solution, to ensure the sufficiency of tests for each translation task. We execute the collected code solutions with test cases and discard the tasks whose solutions exhibit inconsistent behaviors between different languages. Lastly, two authors of this work further manually check each translation task to ensure the benchmark quality.
Implementation
Baseline Implementation. For UniTrans, we obtain its implementation from its replication package and make the following adjustments for comparison with TransAGENT. First, we replace the backbone LLM in UniTrans with the same LLM used in TransAGENT. Second, we modify its fixing phase by splitting it into a syntax error fixer and a semantic error fixer, so as to compare with relevant components of TransAGENT. Furthermore, we align its iteration strategy (i.e., only iterating within a fixed threshold of iterations) into the same dynamic strategy as TransAGENT, for fair comparison. For TransCoder implementation, we directly replicate it with the released implementation with its optimal model weights. We fix the beam_size parameter at 10 and select the first output to re-evaluate it on our benchmark. For TransAGENTTM, we replace the Code Aligner component with the TransMap-related implementation. For Agentless, we directly use the publicly released implementation.
TransAGENT Implementation. For each agent in TransAGENT, (i) Initial Code Translator adopts the same setting as UniTrans; (ii) Syntax Error Fixer adopts javac for Java, GCC for C++, and the Python interpreter for Python, for syntax validation; (iii) Code Aligner adopts Joern, a static code analysis tool to generate control flow graphs, which supports multiple languages; (iv) in Semantic Error Fixer, for Error Block Localization, we insert log statements at either the entry or exit points of each block to capture the runtime values of all variables within the block. If the code block contains a return statement, a log statement is inserted at the entry to capture the return value; otherwise, it is located at the exit. For Runtime Value Comparison, we convert the recorded variable values into “JSON” format for standardized comparison. Data types such as “List,” “Array,” and “Deque” are mapped to “JSON” arrays, and “int,” “float,” and other numeric types are converted to “JSON” numbers. Such a conversion helps standardize comparison across different programming languages, which might have varying data types and structures. In addition, we also include a one-shot example in all prompts to guide LLMs in generating the required output format.
Experimental Procedure
RQ2 (Ablation Evaluation). RQ2 evaluates the contribution of each agent in TransAGENT (except Initial Code Translator as it is a basic component widely used in previous code translation work but not the contribution of our approach). For better illustration, we adopt the following abbreviations: ICT for Initial Code Translator, SynEF for Syntax Error Fixer, and SemEF for Semantic Error Fixer. In particular, we investigate the following variants of TransAGENT for an ablation study. (i) ICT: including only Initial Code Translator for code translation; (ii) ICT + SynEF: including both ICT and SynEF agents; (iii) ICT + SynEF + SemEF: including ICT, SynEF, and SemEF agents. Additionally, we further evaluate the complementarity between two fixing strategies for semantic errors (i.e., vanilla and value-aware fixing strategies) with the following variants: (iv) ICT + SynEF + Val, which applies the value-aware fixing strategy to the ICT + SynEF variant, and (v) ICT + SynEF + Val + Van, which further applies the vanilla fixing strategy to the ICT + SynEF + Val variant.
RQ3 (Repair Accuracy). RQ3 assesses the program repair performance of TransAGENT in comparison with Agentless and TransAGENTTM. Given an initial target program containing translation errors, generated by the Initial Code Translator, we apply each approach to repair it. A repair succeeds if the resulting target program passes all test cases; otherwise, it fails. We use CA as the evaluation metric.
RQ2: Ablation Evaluation
Both the Syntax Error Fixer and the Semantic Error Fixer in TransAGENT yield larger gains in translation performance than their counterparts in UniTrans. For example, in the Python-to-Java task, the Syntax Error Fixer in TransAGENT outperforms that in UniTrans by 27.2% (=32.9%-5.7%), while the Semantic Error Fixer shows a 6.1% (=7.1%-1.0%) improvement. The substantial performance gains of the Syntax Error Fixer in TransAGENT over UniTrans stem from its planning strategy for handling error messages, while the advantage of the Semantic Error Fixer lies in its fine-grained fault localization prior to repair.
The Semantic Error Fixer in TransAGENT is more effective at repairing translation errors between dynamic and static languages than between dynamic languages. For example, in the Java-to-C++ scenario, the Semantic Error Fixer improves the performance of TransAGENT by 3.0%, whereas in the Python-to-Java scenario it delivers a 7.1% improvement. This difference arises from the structural gaps between dynamic and static languages. Dynamic languages such as Python often employ compact constructs, for example, “stnum = "".join(sorted(str(num)))”, which combine multiple operations (e.g., type conversion, string manipulation, sorting, and aggregation) into a single line. When performing program translation, LLMs struggle to disentangle the densely packed operations in such expressions, often resulting in semantic mismatches. In this case, the sorted function in Python defaults to ascending order, but the ICT incorrectly translates it into Arrays.sort(.., Collections.reverseOrder), which produces descending order and breaks semantic consistency. To address such errors, the Semantic Error Fixer compares runtime states to identify inconsistencies. During repair, it then provides test cases (e.g., input num = 2134 with expected output 1234) that reveal the correct behavior, enabling the LLM to generate accurate fixes.
Both Value-aware and Vanilla fixing strategies in Semantic Error Fixer can improve the overall performance of TransAGENT in code translation. For instance, in the Java-to-Python translation scenario, the Value-aware strategy increases CA by 6.3% over ICT + SynEF, while the Vanilla strategy provides an additional 1.9% improvement. This shows that the two fixing strategies are complementary, enhancing the ability of TransAGENT to correct errors and improve translation performance.
Threats to Validity
We categorize threats to the effectiveness of this work into internal effectiveness and external effectiveness. (i) One threat to validity is potential bugs in the code implementation of TransAGENT, which could lead to translation failures. To mitigate this, we use instances of translation failures to debug and improve the implementation. (ii) Another threat is potential data leakage due to overlap between the evaluation dataset and the training data of LLMs. To address this, we manually construct a new dataset from a time frame after the knowledge cutoff date of LLM, specifically after August 2023. We also design comprehensive test cases and calculate line coverage to ensure the equivalence of the constructed source and target code. (iii) Another threat to internal effectiveness is that TransAGENT assumes the source program is executable, as it relies on runtime feedback to perform block-level alignment and to validate repairs. If the source code is not runnable (e.g., missing dependencies, incomplete environments, or uncompilable projects), TransAGENT may fail to obtain reliable execution signals and thus cannot proceed. This executability assumption is also common in execution-guided translation/repair approaches. To mitigate this threat in our evaluation, we only include instances whose source programs can be executed in a controlled environment. Regarding test data, TransAGENT does not rely on pre-existing test suites, as it can automatically generate additional tests when needed. (iv) Another threat involves the reproduction of baseline and the calculation of metrics. To minimize these threats, we strictly follow the reproduction documentation and use the source code provided for the baseline. All baselines are evaluated consistently on our collected dataset, and we reuse the code provided in for implementing evaluation metrics like CodeBLEU.
Code Translation
Early code translation research relied on program analysis techniques with manually formulated rules, such as C2Rust for translating C into Rust, and Sharpen and JavaSharp for converting Java to C#. However, these methods are time-intensive and limit the accuracy and readability of the translated programs. Learning-based approaches have since emerged, significantly improving over rule-based methods but still struggling with data scarcity and the time-consuming training process.
LLMs have shown promise in software engineering tasks, such as code generation, program repair, and code summarization. Pan et al. identify key syntactic and semantic challenges in LLM-based code translation. Therefore, adopting the “translate-then-fix” paradigm is an effective way to enhance LLM-based translation performance. UniTrans, a state-of-the-art method, adopts this paradigm to conduct code translation. However, the error-fixing stage suffers from poor performance due to the lack of detailed runtime error information. TransMap improves the manual fixing process by aligning source and target code, but this alignment is purely statement-level and solely relies on the LLM, without structural or semantic guidance. As a result, this mapping approach is fragile in complex mapping scenarios; for example, a single source line may translate into multiple target lines, or lines may be shifted, ultimately compromising the effectiveness of error repair. To address these issues, we propose TransAGENT. In the mapping phase, TransAGENT employs CFG-based decomposition to partition the source program into atomic blocks, each forming an independent structural unit. LLMs then map these blocks to the target program. This hybrid approach improves mapping robustness and mitigates the limitations of TransMap. In the fixing phase, TransAGENT compares the intermediate execution states of the source and target programs to detect semantic discrepancies. This runtime-based localization offers clearer repair cues, substantially improving error correction accuracy and overcoming the limitations of UniTrans.
Conclusion
In this paper, we introduce TransAGENT, a novel multi-agent system to improve LLM-based code translation with fine-grained execution alignment. The experimental results show that TransAGENT outperforms the latest LLM-based code translation technique UniTrans in both translation effectiveness and efficiency; additionally, our ablation study demonstrates the contribution of each agent, and the error repair evaluation shows that TransAGENT exhibits superior error-repair capabilities compared with Agentless; lastly, we further evaluate the generalization of TransAGENT across different LLMs.


