Abstract
Automating API synthesis—the generation of code snippets that utilize complex library functions—is a long-standing challenge in software engineering. Traditional search-based methods often struggle with state space explosion and poor code readability. This paper presents a systematic prompt engineering framework that leverages Large Language Models (LLMs) to perform API synthesis. By utilizing structured personas, chain-of-thought reasoning, and iterative feedback loops, the proposed approach achieves a 98.5% success rate across 135 programming tasks, significantly outperforming existing state-of-the-art tools while producing human-readable, production-ready code.
1. Introduction
Modern software development is increasingly dependent on vast third-party libraries. However, navigating these libraries to find and combine the correct functions—a process known as "Jungloid mining"—remains a significant bottleneck. API synthesis aims to alleviate this by automatically generating code from high-level specifications, such as method signatures and input-output examples. While traditional component-based synthesis tools like FrAngel and SyPet use exhaustive search, they are often limited by the astronomical size of the search space and produce cryptic, hard-to-maintain code.
2. Methodology
The proposed framework transforms a general-purpose LLM into a specialized Java API synthesizer through a structured prompting strategy. The architecture consists of four primary components:
- Assistant Context: The LLM is assigned the persona of an expert Java software engineer, priming it for industry best practices and library usage.
- Eight-Step Chain-of-Thought (CoT): The model is guided through a logical implementation sequence, including library imports, helper definitions, core logic, and unit testing.
- Few-Shot Learning: Five diverse examples are provided to establish the expected input/output format.
- Iterative Feedback Loop: If the generated code fails execution, the system automatically provides the error back to the LLM for self-correction.
The iterative feedback loop is crucial for handling edge cases. The system categorizes failures into compilation errors, runtime exceptions, or logic errors (failed test cases) and provides specific prompts to guide the model's repair process.
3. Experimental Evaluation
The approach was evaluated on 135 programming tasks drawn from three sources: the FrAngel benchmark (90 tasks), the SyPet benchmark (30 tasks), and a set of 15 novel tasks involving libraries not present in the training data.
3.1 Key Results
The LLM-based approach demonstrated significant improvements over traditional methods:
- Accuracy: The system solved 133 of 135 tasks (98.5%), surpassing FrAngel's 94.2% success rate on its own benchmark.
- Efficiency: While search-based tools often require minutes of computation, the LLM generated solutions in seconds.
- Code Quality: Unlike the "machine-like" output of search-based tools, the LLM produced structured code with descriptive variable names and comments.
- Generalization: The 100% success rate on novel tasks suggests that the model performs genuine semantic reasoning rather than simple pattern retrieval.
4. Discussion and Limitations
The primary advantage of LLMs in this domain is their ability to understand the semantics of method names and library structures, allowing them to infer developer intent even with sparse specifications. However, a notable failure mode was "re-inventing the wheel," where the model occasionally ignored instructions to use specific external libraries in favor of implementing complex logic from scratch.
5. Conclusion
This research demonstrates that LLMs, when guided by structured prompt engineering and iterative feedback, represent a new frontier in API synthesis. By shifting from brute-force search to semantic reasoning, these models can generate accurate, readable, and maintainable code for complex programming tasks.
Summary updated