Abstract
Software developers frequently leverage code snippets from platforms like Stack Overflow, yet these snippets often require significant manual refactoring to become reusable APIs. This paper introduces Code2API, a framework that utilizes Large Language Models (LLMs) to automate this "APIzation" process. By employing Chain-of-Thought (CoT) reasoning and in-context learning, Code2API bypasses the limitations of traditional rule-based systems. Our evaluation shows that Code2API achieves superior accuracy in parameter and return statement identification and is highly preferred by professional developers over existing tools.
Introduction
The process of "APIzation"—transforming raw code snippets into structured, reusable methods—is a common but tedious task for developers. Raw snippets from Stack Overflow (SO) often lack import statements, contain hard-coded values instead of parameters, and lack descriptive method names. Traditional automation tools, such as APIzator, rely on hundreds of manually defined, language-specific rules. These systems are brittle, difficult to maintain, and often produce generic, unhelpful method names like get().
Researchers from Zhejiang University propose that the reasoning capabilities of LLMs can handle the nuance of abstraction better than static rule-based systems. By considering the context of the SO question title and post body, an LLM can generate "intention-revealing" names and identify logical parameters that a rule-based system would miss.
Methodology
Code2API utilizes GPT-3.5-turbo and relies on three core prompt engineering strategies rather than expensive model fine-tuning:
- Prompt Role Designation: The model is explicitly instructed to act as a "skilled Java developer."
- Chain-of-Thought (CoT) Reasoning: The authors designed an 8-step logical process for the LLM to follow. This includes recovering imports, defining a wrapper class, creating a descriptive method name based on context, inferring parameters, and finally outputting the complete code.
- Few-shot In-Context Learning: The prompt includes five high-quality examples of SO snippets being transformed into APIs to help the model understand the desired logic of abstraction.
Key Results and Insights
Code2API was evaluated against a human-annotated dataset of 200 Java API pairs and a new Python dataset of 100 highly-viewed snippets. Key findings include:
- Superior Accuracy: Code2API achieved 65% accuracy for identifying parameters and 66% for return statements, surpassing previous state-of-the-art tools by 15.0% and 16.5%, respectively.
- Developer Preference: In a blinded study with 18 professional developers, Code2API’s generated APIs were chosen as the "best" 50.5% of the time, while the previous rule-based tool was preferred only 1.5% of the time.
- High Reliability: 95% of the generated APIs compiled successfully without manual intervention.
- Efficiency: The tool generates a production-ready API in approximately 10 seconds, compared to over 4 minutes for a human developer.
One of the most significant findings was the "Better Than Human" effect: the LLM often recognized that certain hard-coded values should be parameters even when human experts had refactored the snippet exactly as it appeared, resulting in more flexible and scalable APIs.
Conclusion
Code2API demonstrates that LLM-based reasoning can effectively replace complex, hand-crafted rules for software engineering automation. By leveraging context and logical abstraction, the tool produces APIs that are not only compilable but also preferred by professional developers for their readability and flexibility. Future work will explore fine-tuning specialized open-source models and enhancing data filtering to handle noisy Stack Overflow posts.
Summary updated