Abstract
TypeScript and Python are two programming languages that support optional type annotations, which are useful but tedious to introduce and maintain. This has motivated automated type prediction: given an untyped program, produce a well-typed output program. Large language models (LLMs) are promising for type prediction, but there are challenges: fill-in-the-middle performs poorly, programs may not fit into the context window, generated types may not type check, and it is difficult to measure how well-typed the output program is. We address these challenges by building OpenTau, a search-based approach for type prediction that leverages large language models. We propose a new metric for type prediction quality, give a tree-based program decomposition that searches a space of generated types, and present fill-in-the-type fine-tuning for LLMs. We evaluate our work with a new dataset for TypeScript type prediction, and show that 47.4% of files type check (14.5% absolute improvement) with an overall rate of 3.3 type errors per file. All code, data, and models are available at: https://github.com/GammaTauAI/opentau.
Introduction
Migrating from untyped languages like JavaScript to gradually typed ones like TypeScript is a significant productivity challenge. While type annotations prevent bugs and improve documentation, manually annotating large codebases is labor-intensive. Large Language Models (LLMs) offer a potential solution for automated type prediction, but they face three major hurdles: limited context windows for large programs, a tendency to "hallucinate" extraneous code instead of simple type annotations, and the difficulty of verifying whether a predicted type is both accurate and useful (avoiding "cheat" types like any).
Methodology
To address these challenges, we developed OPENTAU, a system that treats type prediction as a search problem guided by the compiler. The system consists of four primary innovations:
Tree-Based Program Decomposition
Rather than processing a file as a single block of text, OPENTAU decomposes the program into a tree of code blocks, such as functions and classes. It processes this tree from the bottom up, predicting types for inner functions (leaves) first and using those completed snippets as context for parent nodes. This allows the system to handle programs that would otherwise exceed the LLM's context window.
Fill-in-the-Type (FIT) Fine-tuning
We fine-tuned a 1.1B parameter model (SantaCoder) specifically for type prediction. Unlike standard training, FIT trains the model to connect a prefix and suffix with exactly one type annotation. This specialized training significantly reduces syntax errors and prevents the model from generating unnecessary code.
Search, Ranking, and Usage Context
OPENTAU generates multiple candidates for every "hole" in the code. It then searches through combinations of these candidates, using the TypeScript compiler to identify errors. Candidates are ranked using a novel Typedness Metric that rewards specific, restrictive types (e.g., string[]) over vague ones (e.g., any). Additionally, the system injects "Usage Context" by scanning the codebase for call sites and prepending them as comments to the LLM's prompt, helping the model infer types from how a function is actually used.
Evaluation and Results
We evaluated OPENTAU on a curated dataset of 744 high-quality TypeScript files. The results demonstrate that the combination of FIT fine-tuning and program decomposition significantly outperforms baseline models.
As shown in the results table, the full OPENTAU configuration (FIT with usages) achieved a success rate where 47.4% of files type-checked correctly. This represents a 14.5% absolute improvement over the baseline SantaCoder model (32.9%). Furthermore, the system averaged only 3.3 type errors per file, and the FIT fine-tuning nearly eliminated syntax errors entirely.
Conclusion
OPENTAU demonstrates that treating type prediction as a search-and-verify problem is highly effective. By using the compiler as a judge and decomposing programs into manageable trees, we can leverage LLMs to automate complex migration tasks. While challenges remain regarding complex generics and dynamic language features, the integration of specialized fine-tuning and structural decomposition provides a robust framework for future automated programming tools.
Summary updated