Abstract
Stack Overflow is a primary resource for developers, yet the code snippets provided are often fragmented and require manual "APIzation" to become reusable. This paper introduces Code2API, a Google Chrome extension that leverages Large Language Models (LLMs) and Chain-of-Thought (CoT) prompting to automate the transformation of these snippets into functional APIs. Evaluation shows that Code2API significantly outperforms existing rule-based systems and rivals human performance in naming and parameter inference.
1. Introduction
Software developers frequently turn to Stack Overflow for solutions, but the code snippets they find are often "partial"—they lack the necessary context, method headers, or import statements to be executed immediately. To use these snippets, a developer must manually perform "APIzation": a time-consuming and error-prone process of wrapping the code into a method, naming it descriptively, identifying input parameters, and determining what the function should return.
Previous attempts to automate this, such as the rule-based tool APIzator, struggled with the nuances of natural language. These tools often produced misleading method names or failed to handle the diverse coding styles found on the web. Code2API seeks to bridge this gap by using the reasoning capabilities of LLMs to handle the creative and inferential parts of the APIzation task.
2. Methodology
Code2API is implemented as a Chrome extension that interacts with a middleware layer and a backend LLM (specifically GPT-3.5-turbo). The core innovation lies in its Prompt Engineering strategy, which consists of four main components:
- Role Designation: The model is explicitly instructed to act as a "skilled Java developer," providing the necessary context for high-quality code generation.
- Chain-of-Thought (CoT) Reasoning: The authors established an 8-step logical sequence that mimics a human developer's workflow. This includes recovering imports, defining a wrapper class, creating the method signature, inferring parameters, and finally generating the complete code.
- Few-Shot In-Context Learning: The prompt includes high-quality examples of the input (Stack Overflow post) and the desired output (the API) to guide the LLM. The authors found that "1-shot" learning (providing just one example) offered the most effective balance for their final prompt.
- Contextual Enrichment: Unlike previous tools that only look at the code, Code2API feeds the LLM the Stack Overflow question title and post body. This provides the semantic "why" behind the code, which is crucial for naming methods and identifying variables.
3. Experimental Setup
The researchers evaluated Code2API using several benchmarks to test both accuracy and generalization:
- Java Benchmark: A dataset of 200 APIs originally used to evaluate APIzator, where human-written APIs serve as the ground truth.
- Python Generalization Set: A newly constructed ground-truth dataset of 100 Python APIs to test how easily the tool adapts to other languages.
- Large-Scale Dataset: To demonstrate scalability, the authors generated a massive collection of 6,023 Java and 5,000 Python reusable APIs from Stack Overflow.
4. Key Results and Discussion
4.1 Performance Metrics
Code2API achieved 66% accuracy for input parameters and 65% for return statements, beating the state-of-the-art rule-based model by approximately 10 percentage points. Because the system relies on the general knowledge of LLMs rather than hard-coded grammar rules, it maintained high performance when generalized to Python, achieving 80% accuracy in identifying return statements.
4.2 The Power of Naming
One of the most striking findings was that Code2API generated "descriptive and meaningful" method names 74.3% of the time. This is a massive improvement over the 10% achieved by the rule-based APIzator. Remarkably, Code2API even surpassed human developers in this category, who only achieved 60% accuracy.
4.3 Human Preference and Efficiency
In a user study with 18 volunteers, developers preferred Code2API’s generated APIs 50.5% of the time, while human-written APIs were preferred 48% of the time. Only 1.5% of participants preferred the rule-based results. Furthermore, while a human takes over 4 minutes on average to manually APIze a snippet, Code2API completes the task in roughly 10 seconds without sacrificing quality.
5. Conclusion
Code2API demonstrates that LLMs, when guided by structured reasoning (CoT) and contextual data, can effectively automate the transformation of fragmented code into reusable software components. By outperforming rule-based systems and matching human quality, the tool offers a significant productivity boost for developers. Future work aims to expand support to a wider array of programming languages and explore more advanced LLM backends to further refine accuracy.
Summary updated