Introduction
Large Language Models (LLMs) have revolutionized automated code generation. However, they face significant challenges when working with libraries released after their training data cutoff or when translating high-level user requirements into specific, fine-grained API calls. This "granularity gap" often leads to incorrect or hallucinated code when developers attempt to use specialized libraries.
The Granularity Gap and Knowledge Cutoff
Standard Retrieval-Augmented Generation (RAG) techniques often struggle because a single complex user query (e.g., "Load and parse all CSVs in this folder") does not semantically align with individual, low-level API descriptions in a vector space. This mismatch results in poor retrieval performance for libraries like torchdata, which was released after the training cutoff for many popular models.
The CAPIR Framework
To address these issues, we introduce CAPIR (Compositional API Recommendation), a "divide-and-conquer" framework that decomposes complex tasks into manageable subtasks. By bridging the gap between coarse-grained requirements and fine-grained APIs, CAPIR enables LLMs to effectively utilize libraries they were not originally trained on.
The CAPIR pipeline consists of three main stages:
- Task Decomposition: An LLM-based Decomposer breaks the user's high-level requirement into a sequence of fine-grained subtasks. The system uses a bank of few-shot examples derived from library documentation to teach the LLM the appropriate level of granularity.
- Subtask API Retrieval: For each generated subtask, an embedding-based retriever (such as OpenAI’s
ada-embedding-002) searches library documentation to identify the most relevant APIs. - API Reranking: An LLM-based Reranker filters and orders the candidate APIs across all subtasks, ensuring a concise and accurate list for the final code generation prompt.
Experimental Setup and Datasets
The framework was evaluated using two new benchmarks:
- RAPID (Recommend APIs based on Documentation): Focused on the accuracy of API sequence recommendations across five diverse datasets (Torchdata-AR, Math-AR, RealMatrix-AR, XMLStreamWriter-AR, and Multi-Conala).
- LOCG (Library-Oriented Code Generation): Evaluated the final executable code quality using Torchdata-Code and Multi-ODEX.
Key Results
CAPIR significantly outperforms existing baselines such as CLEAR and standard ADA-retrieval. Ablation studies revealed that the decomposition step is the most critical component, allowing the system to bridge the gap between human intent and technical documentation.
On the Torchdata-AR dataset, CAPIR improved Recall@5 from 18.7% to 43.2% and Precision@5 from 15.5% to 37.1%. Furthermore, in code generation tasks (LOCG), CAPIR helped improve the Pass@100 score from 16.0% to 28.0% for the Torchdata-Code benchmark. These results highlight that LLMs are most effective when used as reasoning engines to structure problems rather than as static knowledge bases.
Conclusion
CAPIR demonstrates that a compositional approach to API recommendation can significantly improve code generation for unfamiliar or evolving software libraries. By leveraging task decomposition and multi-stage reranking, the framework provides a robust solution for the knowledge cutoff and granularity gap challenges in modern software engineering.
Summary updated