← Latest papers
💻 computer science

Prefactory: Automated Discovery and Application of Library-Adoption Refactorings

This paper introduces Prefactory, an automated system that synthesizes executable search heuristics using LLMs to efficiently detect and apply library-adoption refactorings in Python code, achieving significantly higher detection and validation rates than existing baselines on a new benchmark of real-world projects.

Original authors: Islem Bouzenia, Michael Pradel

Published 2026-07-21
📖 3 min read☕ Coffee break read

Original authors: Islem Bouzenia, Michael Pradel

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you are a librarian tasked with organizing a massive, chaotic library where every book is written by a different author using their own unique style. Some authors reinvent the wheel, writing long, complicated instructions for things that already have a perfect, pre-made tool in the library's back room. In the world of computer programming, this is called "refactoring." It's the art of cleaning up messy, hand-written code and swapping it for clean, efficient calls to existing, well-tested libraries. Think of it like realizing you've been hand-knitting a sweater when you could just buy a perfect one from a store. The problem is that finding these opportunities is like searching for a needle in a haystack, especially when the needle doesn't look like a needle at all. The hand-written code might not even mention the library's name; it just does the same thing in a clunky way. For years, computers have struggled to spot these hidden chances to upgrade code because they are too rigid, and asking a super-smart AI to read every single file in a project is too expensive and slow.

Enter Prefactory, a new tool designed to solve this puzzle. Instead of asking a super-intelligent AI to read the entire library of code from scratch every time (which is like hiring a detective to read every book in the library one by one), Prefactory teaches the AI to build a set of custom "metal detectors" first. These detectors are like specialized metal detectors tuned to find specific shapes and sounds of buried treasure. Once the AI builds these detectors, they can scan the entire codebase in the blink of an eye, finding the messy, hand-written code that matches the pattern of a library function. When a match is found, the AI steps in one last time to perform the swap and double-check that the new code works exactly like the old one.

The researchers tested this idea on a massive collection of 100 real-world examples where developers had already successfully swapped hand-written code for library calls. They found that Prefactory was incredibly good at spotting these opportunities. It successfully identified the right files in 75 out of 100 cases and pinpointed the exact functions to fix in 56 cases. In comparison, the next best method, which relied on a standard AI agent reading through the code, only found the right files in 35 cases and the right functions in 32. Even better, Prefactory managed to produce 40 fully validated, working refactorings that passed all the safety tests.

What makes this approach so special is how it saves money and time. Scanning a project with Prefactory takes an average of just 1.3 seconds and costs less than $0.05 per instance to find and fix. The standard AI agent method, by contrast, costs about three times as much and takes much longer because it has to "think" through the whole project every single time. The paper suggests that by turning the AI's knowledge into reusable, executable detectors, we can make code cleanup fast, cheap, and reliable. However, the authors also note that the system isn't perfect yet; it sometimes struggles with very large, complex libraries where the "hand-written" code looks very different from the library's standard patterns, and it can't fix code that involves complex, multi-file architectural changes. But for the vast majority of everyday code cleanup tasks, this new "detector-building" approach seems to be a game-changer.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →