Linking API Calls to Language-Neutral Entities for Code Translation

When a program is translated from one programming language to another, the hard part is often not the loops and conditionals. It is the calls to outside libraries and web services. Translators trained without parallel code reproduce control flow well, but they invent methods that do not exist when the target language exposes the same functionality under different names, arguments and object models. Both TransCoder and CoDist name this as their main unsolved failure. This proposal treats each external call as an ambiguous mention of an entity, in the sense used for entity linking: a short string such as getUser or client.query that has to be resolved to one unique entry in a knowledge base. Here the knowledge base holds language-neutral API endpoints, each described once and paired with its binding in every supported language. Once a source call is linked to its endpoint, writing the target call means looking up that endpoint's binding in the target language. Writing it is no longer guesswork.

This structure also addresses the language pair explosion, the problem that $N$ languages need $N(N-1)$ directed converters. Separate API mapping tables for each language pair grow quadratically. A linker into a shared entity space and a binding generator out of it need only one component per language. The remaining obstacle is that labelled linking data is plentiful for a few languages and scarce for the rest. We close that gap with cross-lingual knowledge distillation: a linker trained on the high-resource language teaches a single multilingual linker by passing on its full ranking over candidate endpoints, and the "parallel text" for this transfer is the set of equivalent calls to the same endpoint in different languages.

Core Claim

Our claim is that in API-heavy code translation the main error source is disambiguation: choosing which endpoint a call refers to when several candidates share surface forms. Generating the target syntax is the smaller problem. If the claim holds, a context-aware linker trained with same-name hard negatives, then distilled across languages, should recover most of the gap between translation with no external knowledge and translation given the correct endpoint. Retrieval by surface string should recover much less of that gap, and the advantage should be largest on calls whose names are ambiguous. We frame the claim this way because the obvious alternative, putting a retrieved specification into the prompt, may well be enough to generate the call once the correct entity is known. The proposal stakes its contribution on finding the correct entity.

This differs from the closest work in specific ways. API Pack scrapes 11,213 OpenAPI specifications into 1.1M instruction-to-call pairs across 10 languages. It finds that a large dataset in one "anchor" language plus about 1,000 examples in each other language generalizes across all ten. Its task, however, is generating a single call from a natural-language instruction. It works best when the API is named explicitly, and it scores outputs by string similarity. CoDist reduces the quadratic number of translators to a linear one with a rule-based, language-agnostic token pivot. Its own ablation shows performance collapsing when identifier names are removed, yet it treats identifiers as bags of tokens and has no grounding step. M³ migrates between C/C++ libraries symbolically, using synthesized compiler intermediate representation (LLVM IR) and constraint solving. It assumes stateless functions and uses no learned representations. None of these links call sites to an entity knowledge base using program context, and none transfers a linker across languages by distillation.

Research Program

The knowledge base is built from API Pack's OpenAPI specifications. Each endpoint becomes an entity card with its path, method, parameter schema, description, and the per-language call templates the dataset already provides. To make the evaluation honest about ambiguity, we build ambiguity sets: groups of endpoints that share surface names or paths across different APIs, versions or providers. In these groups the string alone cannot identify the target, and only context can, such as imports, argument names and types, how the response is used, and neighbouring calls. This mirrors the observation in Deep Entity Linking via Eliminating Semantic Ambiguity With BERT that about 80% of text mentions closely match their candidates' surface strings, which makes models complacent. That paper's remedy, hard negatives drawn from candidates with identical surface forms combined with priors and string features, is the training recipe we reuse. Its warning about popularity priors, echoed for LLMs in Who's Who, motivates holding out tail endpoints for testing.

The linker is a bi-encoder followed by a cross-encoder. It encodes a call site together with a window of its program context and scores candidate entity cards. We first train a teacher linker on the anchor languages, cURL and Python, where API Pack has dense data. The multilingual student is then trained on equivalent calls to the same endpoint in other languages. Its target is the teacher's soft score distribution over the candidate set, plus the hard label where one exists. This is the score-matching distillation that worked for cross-language retrieval in HLTCOE at TREC 2023 NeuCLIR and the parallel-text student–teacher setup of OPTICAL. We deliberately use a ranking over candidates as the distillation target and do not align token representations with optimal transport, because the decision we need to transfer is which candidate is correct.

The generator is a code model fine-tuned to rewrite a function. It sees the linked entity card's target-language template for each call site and translates the surrounding code freely. Restricting the intervention to call sites leaves algorithmic translation untouched, so gains or losses can be attributed to the linking step.

Baselines

The naive reference point is direct translation with no knowledge base: CodeLlama-13B fine-tuned on the same API Pack-derived training calls, prompted to translate source to target. The strongest existing competitor for producing correct calls is API Pack's own recipe, a fine-tuned CodeLlama-13B with 3-shot retrieval of similar examples. On unseen APIs (Level 3) it reports 49.5% accuracy, against 44.3% for GPT-4 and 39.5% for GPT-3.5 (API Pack). That figure is for generating a call from an instruction, not for translation. We found no reported result on Lacuna for cross-language API call translation, so we re-run this baseline on our translation task. A second strong competitor is surface-string retrieval-augmented generation: retrieve the top entity cards by lexical and embedding similarity of the call string, put them in the prompt, and let the generator choose. All systems share the same base model, training data, knowledge base and candidate generator, and each gets the same hyperparameter search budget. Beating these baselines means a statistically significant gain in execution-verified call accuracy on the ambiguity sets and on Level-3-style unseen APIs, with no loss on the unambiguous calls.

Four further conditions isolate the proposal's ingredients. An oracle condition supplies the correct entity and sets the ceiling. A no-hard-negative linker tests the disambiguation recipe. A no-distillation linker, trained directly on the scarce per-language labels, tests the cross-lingual transfer. A context-ablated linker, which sees the call string only, tests whether program context does the work. For algorithmic code we also report TransCoder's computational accuracy, 68.7% for Java to Python on GeeksforGeeks (TransCoder), together with CoDist's reported 12.7-point absolute gain over TransCoder-ST (CoDist). These serve as a check that our generator does not regress on code without API calls.

Why not just…?

Why not just prompt a frontier LLM to translate the code and fix the APIs? On unseen APIs, API Pack reports GPT-4 at 44.3%, below a fine-tuned 13B model at 49.5%. That result already holds with explicit API names in the prompt. Who's Who shows LLMs resolving same-name entities toward the popular one, which is exactly the failure that the ambiguity sets target. We include a frontier-model prompting arm, with and without entity cards, so this is measured rather than assumed.

Why not just retrieve the specification by string and let the model choose? This is the real threat. If retrieval by string already puts the right card first, linking adds little. The decisive comparison is the per-subset gap: we predict string retrieval matches the linker on unambiguous calls and falls behind on the ambiguity sets and on tail endpoints. If it does not fall behind there, the core claim fails, and we will report that.

Why not just train one multilingual translator on all directions and rely on API Pack's cross-language synergy? API Pack's synergy result shows that 1,000 examples per secondary language can suffice for generation. Our no-distillation ablation is essentially this setting applied to linking. If it matches the distilled student, distillation is unnecessary, and the proposal reduces to linking plus shared training. That is still a useful finding, and it would remove one component.

Why not just extend CoDist's language-neutral pivot? CoDist's pivot normalizes standard-library calls by rule and collapses without identifiers. It cannot map third-party endpoints whose names differ across languages. We use CoDist as the algorithmic-translation comparison point, not as an API mechanism.

Proposed Experiments

The main benchmark is a translation task built from API Pack. The same endpoint called in two languages gives a source–target pair, which yields 90 directed directions across 10 languages. We hold out entire endpoints, matching API Pack's three difficulty levels, and hold out all direct pairs for some directions to test zero-shot transfer through the entity space. To get past API Pack's string-similarity scoring, we stand up mock servers generated from each OpenAPI specification. Each translated call is executed against the mock server, and the captured HTTP request (method, path, parameters, body schema) is compared with the request the source produces. Mock servers need no real API keys, which sidesteps the personal-data concern that kept API Pack from running calls, and they give a functional rather than textual check. This responds to the tension, documented in XLCoST and TransCoder, between high CodeBLEU scores and code that does not run. We then compose multi-call scripts by embedding several linked calls inside functions drawn from the GeeksforGeeks and XLCoST program sets, and we score these by end-to-end request-trace equivalence together with the existing unit tests.

The decisive checks follow from the claim. The oracle-to-naive gap should be large, which confirms that disambiguation errors matter. The linker should close most of that gap on the ambiguity sets, while string retrieval closes much less of it there and roughly as much on unambiguous calls. Linking accuracy in low-resource languages should rise with distillation from the anchor-language teacher, compared with direct training on the same scarce labels. Computational accuracy on GeeksforGeeks should stay within noise of the generator without linking. If the first check fails, API calls are not the bottleneck and the project stops there. If the second fails, cheaper retrieval is sufficient, and we report it as the recommended method.