Abstract
Large Language Models (LLMs) have demonstrated significant potential in automated code generation, yet they often struggle with code translation across different programming languages due to the "API gap." This paper introduces APIRAT (API Retrieval Augmented Translation), a framework designed to mitigate API-related translation errors by integrating multi-source API knowledge. By utilizing API sequences, back-translations, and high-quality mappings within a retrieval-augmented generation (RAG) loop, APIRAT achieves up to a 15.1% improvement in computational accuracy over existing baselines on benchmarks such as CodeNet and AVATAR.
Introduction
While LLMs like GPT-4 excel at general-purpose coding tasks, migrating software between languages (e.g., Java to Python) remains a significant challenge. The primary obstacle is not basic syntax, but the semantic mismatch between specialized APIs. A preliminary study revealed that over 60% of translation errors stem from API mistranslations. These failures are categorized into twelve distinct fault patterns, ranging from simple parameter type mismatches to complex logic errors in API sequences.
Methodology
To address these challenges, we propose APIRAT, which employs a "translate-test-refine" loop powered by Retrieval-Augmented Generation (RAG). The framework provides the LLM with a "cheat sheet" of API knowledge during the translation process to prevent hallucinations and logic mismatches.
The APIRAT Workflow
The process begins with an initial direct translation and the automated generation of test cases. If the translated code fails these tests, the augmentation phase begins, retrieving three types of knowledge:
- API Sequence Retrieval: Searching a database of 200,000 GitHub API sequences to find idiomatic target-language implementations of the source logic.
- API Sequence Back-Translation: Translating retrieved target sequences back into the source language to provide a semantic "sanity check" for the LLM.
- API Mapping: Utilizing curated mappings that detail usage restrictions and discrepancies between equivalent functions.
Evaluation and Results
APIRAT was evaluated on the CodeNet and AVATAR benchmarks, as well as a newly introduced dataset, APISEQDATA. The framework consistently outperformed vanilla LLMs and state-of-the-art retrieval methods.
Performance Gains
On the CodeNet dataset (Python to Java), APIRAT achieved a computational accuracy of 91.5%, a significant jump from the 82.0% achieved by direct translation. On the more rigorous AVATAR benchmark, Java-to-Python translation accuracy improved from 67.5% to 75.9%. Notably, OpenAI’s text-embedding-3-large outperformed specialized code-search models in retrieving relevant API knowledge, suggesting that semantic intent is well-captured by high-dimensional text embeddings.
Model Generalizability
The benefits of APIRAT were observed across various models, including GPT-3.5-Turbo, GPT-4o-mini, and StarCoder. Smaller or specialized models like StarCoder saw the most dramatic relative improvements, nearly doubling their accuracy in specific tasks when augmented with multi-source API knowledge.
Conclusion
APIRAT demonstrates that the "Lost in Translation" problem in code migration is largely an API knowledge problem. By providing LLMs with structured, multi-source API context, we can significantly reduce hallucinations and logic errors. Future work will focus on extending this framework to repository-level migrations involving complex inter-file dependencies and incorporating broader third-party library support.
Summary updated