Python’s dynamic nature is a double-edged sword: it’s great for rapid prototyping but a nightmare for maintaining large-scale codebases. While type hints (PEP 484) have brought some order to the chaos, manually annotating millions of lines of legacy code is a Herculean task. Naturally, developers have turned to Large Language Models (LLMs) for help, but how do we know if their "guesses" are actually useful?
A new paper introduces TYPYBENCH, a rigorous framework designed to move beyond simple "exact match" metrics and evaluate whether LLMs can maintain global type consistency across entire software repositories.
TL;DR
TYPYBENCH is a new benchmark that evaluates LLM type inference using two novel metrics: TYPESIM (semantic similarity) and TYPECHECK (global consistency). The study reveals that while modern LLMs are good at guessing types locally, they struggle with complex nested structures and frequently fail to produce a coherent, error-free type system at the repository level.
What Problem Does This Paper Address?
Existing benchmarks for type inference suffer from two major flaws:
- The "Exact Match" Trap: Traditional metrics often penalize a model for predicting
Sequence[int]when the ground truth isList[int], even though they are functionally interchangeable in many contexts. - Local Isolation: Most evaluations look at functions or files in a vacuum. In the real world, a type predicted in
module_a.pymust be consistent with its usage inmodule_b.py. If an LLM gets the type "mostly right" but breaks the project's static analysis, the output isn't production-ready.
TYPYBENCH addresses these gaps by providing a repository-scale evaluation that rewards semantic nuance and punishes global inconsistency.
Methods
As illustrated in the framework overview below, the authors introduce a two-pronged evaluation strategy after stripping typing hints from a repository and prompting an LLM to predict them:
- TYPESIM: Instead of a binary "right or wrong," this metric uses the Jaccard index to compare the attributes and methods supported by the predicted type versus the ground truth. If two types share 90% of the same operations (like
__iter__orappend), they get a high similarity score. It also works recursively for nested types (e.g.,List[Dict[str, int]]). - TYPECHECK: This is the "reality check." The researchers use Mypy, a standard Python static type checker, to run against the entire repository after the LLM has filled in the blanks. The score is based on the number of type errors generated. A "perfect" model would result in zero Mypy errors.
Datasets
The researchers curated a high-quality dataset of 50 popular Python repositories from GitHub and PyPI. As shown in the domain distribution below, these span diverse areas like ML/AI (e.g., AutoGPT, vLLM), Web/API (FastAPI), and Developer Tools (Black, Poetry).
- Total Scale: Over 17 million tokens and 70,000 functions/variables to be inferred.
- Splits: 20 repositories for training, 10 for validation, and 20 for testing.
- Contamination Check: The test set was specifically split into "Test1" (older) and "Test2" (created after early 2024) to see if LLMs performed better on code they might have seen during training.
Key Insights and Ideas
- The Complexity Wall: LLM performance drops off a cliff as type nesting increases. While models are great at identifying a simple
str, they struggle significantly with "Depth 3+" types likeDict[str, List[tuple[int, int]]]. - Consistency vs. Similarity: A surprising finding was that Claude 3.5 Sonnet often produced the most "consistent" codebases (lowest
TYPECHECKerrors), even if it didn't always have the highest "similarity" score. In some cases, it even produced fewer errors than the original human-annotated code. - The "Rare Type" Problem: Models are biased toward common types. When a repository uses a custom class or a rare library type, LLM accuracy takes a significant hit.
- Context Matters: Providing the LLM with the entire repository structure (directory tree and cross-file context) significantly reduces type errors, but it introduces a new challenge: managing massive token counts and long-form output stability.
Key Results
- SOTA Performance: Top-tier models like GPT-4o and Claude 3.5 Sonnet achieve
TYPESIMscores around 0.80, but their "Exact Match" scores for complex types are nearly zero. - Consistency Gap: Most models still trigger hundreds of Mypy errors across a repository. For example, on the
DB-GPTrepo, GPT-4o triggered over 2,400 errors, highlighting that we are still far from "push-button" automated typing. - Data Contamination: There is a clear performance delta (~6% drop in
TYPESIM) when moving from older repositories to those created in 2024, suggesting that LLMs are partially "memorizing" types from their training data.
Limitations and Future Work
The authors acknowledge several hurdles:
- Long Context Limits: Most repositories are too large to fit into a single LLM prompt. Current "file-by-file" inference methods lose the "global picture," leading to consistency errors.
- Hallucination: Models sometimes invent types or import paths that don't exist.
- Future Direction: The researchers argue that the field should stop obsessing over local similarity and start focusing on repository-level consistency. Future tools might need to combine LLMs with iterative static analysis—letting the model "fix" its own Mypy errors until the codebase passes.
Paper Citation: Dong, H., et al. (2025). TYPYBENCH: Evaluating LLM Type Inference for Untyped Python Repositories. Proceedings of the 42nd International Conference on Machine Learning (ICML). GitHub Link
Summary updated