Developer tools · Research feature

BabelCoder Uses Multi-Agent System to Improve Code Translation Accuracy

A new framework uses specialized AI agents to translate code more accurately by validating, detecting, and fixing errors in real time.

Adapted from research by Fazle Rabbi, Soumit Kanti Saha, Tri Minh Triet Pham and colleagues.

arXiv preprint revision · 2026-09-24 · Attribution & reuse
In this article
Editorial illustration of stacked plates.

Editor’s introduction

BabelCoder is a multi-agent system designed to translate code between languages by dividing the task into specialized components. It includes agents for translation, testing, and refinement, each responsible for generating, validating, and repairing code. The system evaluates translation quality using four benchmark datasets and compares results against four existing methods. It achieves higher accuracy in 94% of cases, outperforming baselines by 0.5% to 13.5% on average. The study measures improvements in correctness through validation of natural language specifications and error localization using LLM-driven tools.

Introduction

Software development now involves a diverse array of programming languages, each tailored to specific application requirements. As technologies and project goals evolve, systems often must migrate to more modern or suitable languages. For example, developers are migrating unsafe C code to Rust for better memory safety, and porting legacy COBOL code to modern languages to improve maintainability and performance. However, manually rewriting large codebases remains time-consuming and error-prone and demands significant developer effort and domain expertise.

Recent studies have explored Large Language Models (LLMs) for code translation from multiple perspectives. Early works such as Pan et al. and Yin et al. reported low correctness and inconsistent bugs, showing limited gains from prompt engineering. Subsequent systems including UniTrans, ExeCoder, and CoTR enhance robustness by incorporating testing mechanisms. More recent efforts, such as TransAgent, adopt a multi-agent decomposition strategy to improve modularity, whereas SpecTra utilizes multi-modal specifications (e.g., static assertions and I/O examples) to guide translation. InterTrans advances further by performing translation through multiple intermediate representations, achieving state-of-the-art results on benchmarks such as CodeNet, HumanEval-X, and TransCoder.

As the figure shows, the baseline fails not for lack of Java knowledge but because it loses the source program’s intent: without an intermediate specification, it mistranslates both the assertion and the return type. BabelCoder addresses this by first generating and validating an NL-Specification, and the example concretely illustrates the four limitations above, the NL-Specification supplies the rich, language-agnostic representation missing from single-modal translation (limitation 1), separating translation, validation, and repair across agents replaces monolithic end-to-end generation (limitation 2), validating the NL-Specification against test feedback yields semantic rather than purely syntactic repair (limitation 3), and the bounded, test-terminated refinement loop keeps LLM usage low relative to multi-path search (limitation 4).

In this work, we propose an agentic framework for automated code translation, namely BabelCoder, which first translates the code and then refines it using specifications and tests inferred from the source. BabelCoder is composed of three agents: a Translation Agent, a Test Agent, and a Refinement Agent. The Translation Agent generates an initial translation skeleton using source code and further refines the generated code based on the validated NL-Specification produced by the Refinement Agent. The Test Agent generates inputs for the source code, derives the corresponding oracles, and evaluates the correctness of the translated code by executing the generated tests when needed. The Refinement Agent autonomously generates enriched specifications to improve semantic understanding during translation. It then applies a multi-step iterative repair process to improve the translated code’s quality. To address translation-related bugs, our system integrates multiple complementary repair mechanisms that target compilation errors, runtime failures, and assertion violations. These include general-purpose prompt-based correction and Spectrum-Based Fault Localization (SBFL). In addition, we introduce two novel repair strategies. First, NL-Specification Validation to validate the translated code’s behavior. Since NL-Specification summarizes the source logic in a language-agnostic form, mismatches between it and the output indicate semantic errors, guiding repairs beyond syntactic fixes. Second, finding and fixing scopes of bugs using LLM to focus correction efforts more effectively. To strengthen the model’s semantic understanding and improve repair accuracy, we incorporate summarized error messages and contextual cues directly into the LLM prompts. This enhanced semantic context helps the model reason more effectively about the intent and behavior of the original code. Through this modular and layered architecture, our framework significantly improves code executability and robustness, while also elevating the quality of code translation beyond existing LLM-based systems.

The Design of BabelCoder

A translation begins when BabelCoder passes the source code to the Translation Agent (Agent A). Agent A produces an initial translation from the source code and forwards it to the Refinement Agent (Agent B). Agent B then drives a test-driven refinement loop, and its interactions with the other agents are determined by that loop. In every iteration, Agent B first calls the Test Agent (Agent C) to compile the current translation, run the generated tests, and summarize the resulting errors together with coverage. If all tests pass, Agent B stops and returns the current code. If some tests fail, Agent B takes one of two actions before the next iteration, and the state machine determines which one. When the current input configuration has stopped increasing the number of passing tests, Agent B requests a new translation from Agent A, and the state machine advances the input for the next attempt to the source code, the NL-Specification generated and validated from the source (Section 2.2.2), or both (Section 2.2.3). Otherwise, Agent B repairs the current translation in place, localizing the fault with Spectrum-Based Fault Localization and an LLM-based scope estimator (Section 2.2.5) and applying a targeted fix. The loop repeats until all generated tests pass or a preset maximum number of refinement cycles is reached, and Agent B then outputs the final translation.

We use the term agent in the operational sense adopted in recent work on LLM agents. An agent is an LLM-driven component that uses external tools, observes feedback from its environment, and makes autonomous and state-dependent decisions rather than following a fixed script. Under this definition BabelCoder is not a static workflow, for two reasons. First, its control flow is decided at run time from test feedback. The state machine selects the translation input form, the localization component selects a repair scope, and the loop decides whether to re-translate, which repair to apply, and when to stop, and each of these choices depends on the observed test outcomes. As a result, different inputs follow different paths through the same set of agents. Second, the agents themselves make open-ended decisions that a fixed rule engine cannot. The bug-scope agent classifies a fault into one of several code scopes, and the repair agent formulates and applies the fix, and both are driven by the LLM. The agents also use tools such as the compiler, the test runner, and the coverage and fault-localization machinery, and they exchange intermediate artifacts. These properties are what we mean when we call BabelCoder an agentic framework.

Datasets

Avatar. This dataset contains 9,515 code snippets written in Python and Java, collected from online competitive programming platforms. It includes test cases for 250 problems and provides aligned function-level code across languages. We consider all possible translation directions from two source languages (Python and Java) to five target languages (Python, Java, C, C++, and Go), resulting in eight unique language pairs. In this work, we use the subset curated by Pan et al., which selected only the samples that include test cases (250 problems per language). We filter out samples where the ground-truth solutions fail the provided tests, resulting in 240 valid samples in Java and 239 in Python, yielding a total of 479 data points and 9,381 test cases (4,691 for Java and 4,690 for Python).

CodeNet. A large-scale dataset collected from online programming competitions, containing 14 million code samples across 55 programming languages. In this work, we focus on five languages (C, C++, Go, Java, and Python) as both source and target languages, resulting in 20 language pairs. We directly use the subset curated by Pan et al. without any modification, which includes 200 valid instances per language, totaling 1,000 code samples. Each instance is accompanied by a single test case.

EvalPlus. EvalPlus is an enhanced version of the handcrafted HumanEval dataset, written in Python with 164 samples. It improves the ground truth implementations and provides stronger, more comprehensive test cases. In this work, we evaluate the Python-to-Java translation task using EvalPlus test cases. Since EvalPlus only contains Python implementations, we use Java ground truths curated by Rabbi et al., which are based on HumanEval-X and aligned with EvalPlus test cases.

TransCoder. The code samples in this dataset are collected from GeeksForGeeks in three languages: C++, Java, and Python. In this work, we use the cleaned version released by Yang et al., which contains 464 standalone functions. We focus on the Python-to-Java direction, as our unit test converter currently supports only this translation pair. We obtained the original TransCoder dataset from previous work and selected the same 464 samples as used in UniTrans (one of our baselines) to enable direct comparison. Since the dataset does not include test cases, we automatically generated test cases from the provided Python code. Among the samples, 33 functions either produce standard output (e.g., print) or perform in-place modifications on input parameters without returning values, making them unsuitable for return-value-based evaluation. This issue was not addressed in previous works using this dataset, such as UniTrans and TransAgent. To support meaningful evaluation, we manually added appropriate return statements to these functions. We include these modified datapoints in our artifact to support reproducibility and future research.

Unit Test Conversion

For the benchmarks that we evaluate by functional correctness, namely EvalPlus and TransCoder, the Test Agent judges a translation by comparing the return value of the translated function against an expected value. EvalPlus ships its ground-truth oracles as Python assertions in the PyTest style, while TransCoder provides no test cases, so for TransCoder the Test Agent generates inputs and obtains the expected values by executing the source program. In both cases the oracle is a Python-side assertion that has to be compiled and executed in the target language, so we implement a static unit test converter that turns each such assertion into an equivalent JUnit test. The converter parses the assertion with Python’s abstract syntax tree and reads the literal argument values and the expected output. From these values it infers the Java parameter and return types, covering scalars as well as nested and mixed element lists such as list[int], list[float], and list[list[float]], and it rewrites the Python literals into their Java counterparts. During this step it respects the numeric ranges of the Java types, for example the limits of Integer, Long, Float, and Double, so that a value that would overflow a narrower type is assigned a wider one. It then reconstructs the JUnit test header from the inferred signature. The converter produces the JUnit tests that the Test Agent executes against the translated Java code, both for the tests generated during refinement and for the ground truth tests used in the final EvalPlus evaluation. Because its type inference and literal rewriting are implemented for the Python to Java direction, our functional evaluation uses that direction.

Avatar and CodeNet are different: they provide whole programs that read from standard input and print to standard output. The Test Agent checks these by comparing the printed output against the expected output, which does not depend on the target language, so no converter is needed. This is why EvalPlus and TransCoder are limited to Python-to-Java, while Avatar and CodeNet are evaluated across all of their language pairs. Extending EvalPlus or TransCoder to another target language would require adding the corresponding type and literal rules to the converter, which we leave for future work.

Baselines

LIT. The first is Lost in Translation, which evaluates multilingual code translation using AVATAR, CodeNet, and EvalPlus datasets. They also included two real world projects Apache Click and Common CLI in their experiment each containing 16 datapoints. But according to Pan et al. all the translations for these two datasets failed. Also, as no unit tests were available with real world datasets, we excluded them from our experiment.

UniTrans. The second baseline, UniTrans, leverages the dataset introduced by. This work shares similarities with LIT, as both propose autonomous translation frameworks combined with iterative repair mechanisms. However, the main difference is the integration of test case generation within the framework to assess computational accuracy (CA).

TransAgent. TransAgent uses the same dataset as UniTrans and also uses UniTrans as the initial translation agent with a novel Alignment Agent which aligns source and translated code block by block (i.e., for, if, etc.) and uses the internal variables’ values to find inconsistencies between the source and translated code.

InterTrans. InterTrans conducted experiments using three datasets: CodeNet, HumanEval-X, and the dataset by Lachaux et al.. Their best performance (95.4%) was reported on HumanEval-X. Unlike BabelCoder, InterTrans does not use a GPT model. It evaluates open models such as Magicoder and StarCoder2, and Magicoder gives its best results. It also places the target function’s method signature in the translation prompt, which makes the translation easier. Because InterTrans differs from BabelCoder in its model, its benchmark, and its prompt, a comparison under a shared model is not possible, so we compare on the code that InterTrans released rather than rerunning it. We take this route because rerunning InterTrans under GPT-4o would replace the configuration its authors tuned and reported, so any drop in its accuracy would reflect our setup rather than the method itself. Comparing on the released outputs keeps InterTrans in the setting where it performs best, and we score both systems on the same 31 instances with the same EvalPlus tests, so the difference reflects the translations and not the evaluation. However, as their evaluation was conducted on only 35 samples per language and dataset, making a direct comparison was challenging. To address this, we retrieved their generated code from the provided replication package, focusing specifically on the Python-to-Java translation results from HumanEval-X. Out of 35 samples, we exclude 4 as these HumanEval ground truths fail on EvalPlus test cases. We then compare the results of InterTrans based on these 31 samples with BabelCoder.

RQ1: What is the comparative performance of our approach against existing baselines?

Motivation. Existing works evaluate code translation using different benchmarks, CodeNet and Avatar using standard input/output, and TransCoder and EvalPlus using functional correctness. To enable a comprehensive comparison, RQ1 investigates whether our agent-based framework can consistently outperform these approaches across all four benchmarks. In addition, because BabelCoder operates at a non-zero temperature and is therefore non-deterministic, we also assess whether its accuracy is stable across repeated runs.

Code Translation

Function-Level Code Translation. Previous efforts in code translation with LLMs have largely focused on function-level translation. To improve translation capabilities, several approaches enhance LLMs through model design and contextual augmentation. For example, SteloCoder adapts the StarCoder architecture into a decoder-only LLM for multi-language code translation. SpecTra incorporates multi-modal specifications, i.e., natural language descriptions, control flow graphs, and data flow graphs, to guide translation. AGL-Code adaptively leverages global and local contextual information to translate pseudocode into executable code, while Bhattarai et al. improves few-shot learning through retrieval-augmented generation, injecting relevant translation examples to guide the LLM. Saha et al. use NL-Specification as an intermediate representation while translating code from source to target language.

Several works also focus on translation correction and functional validation. Rectifier learns from faulty-corrected code pairs to repair common translation errors. TransAgent employs a multi-agent system responsible for test generation, syntax error repair, and semantic alignment, using source-target execution behavior for automated translation correction. Similarly, UniTrans leverages test case generation and iterative repair to improve translation accuracy, while ExeCoder incorporates semantics and dependency information to improve executability. CoTR enhances robustness via syntactic transformations and adversarial training.

Finally, for empirical analysis, LIT systematically studies bugs introduced by LLMs during code translation, categorizing common failure modes and proposing targeted evaluation metrics. Complementing model-centric analyses, Rabbi et al. show that code-translation evaluations can also report false failures caused by compilation and runtime configuration rather than incorrect translation logic, highlighting the importance of reliable evaluation infrastructure.

Repository-Level Code Translation. In addition, several works focus specifically on repository-level code translation. K3Trans is a knowledge-augmented LLM approach that leverages target code samples, dependency usage, and historical translations to enhance translation quality and correctness in complex repository contexts. AlphaTrans adopts a neuro-symbolic method that uses static analysis to decompose programs and translates code fragments in reverse call order, supporting both code and test translation with multi-level validation. To aid evaluation efforts of such systems, RepoTransBench introduces a benchmark of 100 real-world repositories with automated test suites, highlighting the challenges current LLMs face in repository-level code translation and exposing common failure modes.

Conclusion and Future Work

This paper presents BabelCoder, a multi-agent autonomous code translation tool that treats translation as a distributed problem, solved collaboratively by multiple agents with the assistance of large language models (LLMs). BabelCoder comprises three specialized agents, each responsible for a distinct sub-task in the translation pipeline. It introduces several novel techniques, including validating NL-Specification using an LLM-based aligner, localizing bugs with an LLM-driven scope finder, generating precise error messages, and repairing bugs using location-aware prompts.

BabelCoder has been evaluated on four public benchmark datasets across five programming languages and compared against four strong baselines. Experimental results show that BabelCoder consistently outperforms existing approaches across nearly all language pairs, achieving higher accuracy and robustness, handling diverse programming languages, varying code complexities, and real-world input variations without significant performance degradation.