NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Generative Type Inference for Python

Yun Peng, Chaozheng Wang, Wenxuan Wang, Cuiyun Gao, Michael R. Lyu

Introduction

Python’s dynamic typing system offers significant flexibility for rapid development, yet it frequently introduces challenges for long-term software reliability and maintenance. Runtime type errors are a common pitfall in large-scale Python projects. While optional type hints (introduced in PEP 484) provide a solution, the manual effort required to annotate millions of lines of legacy code is prohibitive. Consequently, automated type inference has become a critical area of research.

The Limitations of Existing Paradigms

Existing automated type inference tools generally fall into three categories, each with significant drawbacks:

  1. Rule-based Systems: These rely on formal static analysis. While highly accurate, they often suffer from low coverage, failing when encountering dynamic features or external library calls where type information is opaque.
  2. Supervised Learning Models: These treat type inference as a multi-class classification problem. They require massive, meticulously labeled datasets and often struggle to generalize to "rare" or custom user-defined types.
  3. Cloze-style Models: Utilizing pre-trained models like CodeBERT, these systems treat type inference as a "fill-in-the-blank" task. However, they operate as "black boxes," providing predictions without interpretable reasoning, which limits developer trust.
A comparison of traditional type inference paradigms: (1) Rule-based, (2) Supervised, and (3) Cloze-Style.

The fundamental missing link in these approaches is the integration of domain-specific logic. While Large Language Models (LLMs) possess vast general coding knowledge, they do not inherently follow the formal logical steps required for rigorous type inference.

Methodology: The TYPEGEN Framework

To address these gaps, we introduce TYPEGEN, a few-shot generative approach that utilizes In-Context Learning (ICL) to teach LLMs to emulate the reasoning of a static analyzer. TYPEGEN operates through a four-phase workflow designed to maximize precision and interpretability:

1. Code Slicing

To minimize input noise and stay within LLM context windows, TYPEGEN constructs a Type Dependency Graph (TDG). It then prunes the source code to retain only the statements that directly influence the target variable's type, resulting in a concise "code slice."

2. Type Hint Collection

LLMs cannot predict user-defined types they have not encountered. TYPEGEN performs an automated import analysis to identify all available user-defined and third-party types within the project environment, providing these to the model as a "cheat sheet."

3. Chain-of-Thought (CoT) Construction

This is the core innovation of TYPEGEN. The system translates the internal logic of static analysis into natural language reasoning steps. For example, instead of a direct prediction, the model is prompted to reason: "Variable A is assigned from a dictionary, therefore its type is dict."

4. Type Generation

The LLM receives the code slice, the collected type hints, and a small set of CoT examples. It then generates both a logical explanation and the final type prediction.

The TYPEGEN prompting architecture, illustrating the integration of code slices, available type hints, and Chain-of-Thought reasoning.

Experimental Evaluation

The study utilized the ManyTypes4Py benchmark, a comprehensive dataset containing over 240,000 training and 85,000 test instances. For the LLM evaluation, a statistically significant sample of 10,000 instances was used to assess performance across various model scales (from 1.3B to 175B parameters).

Key Results and Insights

TYPEGEN demonstrates a significant leap in performance over state-of-the-art baselines:

  • Accuracy Gains: TYPEGEN outperformed Type4Py by 10.0% for function arguments and 22.5% for return values in Top-1 Exact Match metrics.
  • Hybrid Superiority: It surpassed HiTyper—a hybrid system combining static analysis with deep learning—by 15.5% for argument prediction, suggesting that generative reasoning is more flexible than rigid hybrid pipelines.
  • The "Small Model" Multiplier: Interestingly, domain knowledge injection via CoT prompts benefited smaller models (e.g., GPT-Neo 1.3B) more significantly than larger ones, acting as a force multiplier for less computationally expensive models.
  • Interpretability: By providing natural language explanations, TYPEGEN bridges the trust gap, allowing developers to verify the model's logic rather than blindly accepting a predicted label.

Conclusion and Future Work

TYPEGEN proves that teaching LLMs the logic of type inference through Chain-of-Thought prompting and code slicing is more effective than traditional supervised classification. While the approach is highly effective, future work will focus on inter-procedural analysis to better track data flow across different files, which remains a challenge for predicting function argument types that lack local definitions.

Summary updated

BibTeX
@inproceedings{peng2023generative,
  author = {Yun Peng and Chaozheng Wang and Wenxuan Wang and Cuiyun Gao and Michael R. Lyu},
  title = {{Generative Type Inference for Python}},
  booktitle = {ASE},
  year = {2023},
  url = {https://openreview.net/forum?id=YV7BlxLlJ9},
}
Figure