Abstract
APRIL is a novel framework designed to enhance the ability of Large Language Models (LLMs) to synthesize complex code for scientific Python libraries. By integrating Automatic Prompt Optimization (APO) with Reinforcement Learning from Verifiable Rewards (RLVR), APRIL achieves a 93.8% success rate in generating functionally correct APIs, significantly outperforming expert-prompted baselines.
Introduction
Component-based API synthesis—generating code that utilizes specific library functions—is a critical but challenging task in software engineering. Traditional methods often rely on labor-intensive formal specifications. While LLMs have advanced the field, they frequently suffer from hallucinations and "context blindness," where they fail to account for specific library versions or complex internal dependencies.
The Challenge of API Synthesis
The primary hurdle in this domain is moving beyond simple text mimicry toward functional correctness. As illustrated in the training dynamics below, standard models often exhibit high performance on training data while failing to generalize to validation tasks. This gap highlights the limitations of traditional fine-tuning and the need for a system that learns from verifiable functional rewards.
The APRIL Framework
The APRIL (API Synthesis with APO and RLVR) pipeline addresses these challenges through a three-stage process designed to optimize both instructions and model weights based on verifiable functional rewards.
1. Automated Test Generation
The system utilizes an AI agent to generate comprehensive test suites for target APIs based on docstrings and reference implementations. This provides the "verifiable reward" signal necessary for subsequent optimization.
2. Automatic Prompt Optimization (APO)
APO treats the prompt as an optimizable variable. By analyzing failure cases, the system generates "textual critiques" (text gradients) and iteratively refines the prompt to address specific model shortcomings.
3. Reinforcement Learning from Verifiable Rewards (RLVR)
The model is fine-tuned using Group Relative Policy Optimization (GRPO). Unlike standard supervised fine-tuning, RLVR prioritizes functional correctness—passing unit tests—over surface-level similarity to human-written code.
Experimental Evaluation
APRIL was evaluated on a benchmark of 81 real-world API synthesis tasks across three major scientific Python libraries:
- NumPy (36 tasks): Focused on array-centric operations and numerical routines.
- scikit-learn (33 tasks): Focused on data transformation and machine learning model training.
- SciPy (12 tasks): Focused on algebraic and statistical procedures.
An additional 40 tasks were used for training the APO and RLVR components.
Results and Key Insights
APRIL demonstrated a significant leap over baseline models:
- 100% Executability: All generated APIs were syntactically correct and runnable.
- 93.8% Success Rate: The framework achieved nearly universal success across the validation suite.
- Benchmark Gains: Success rates improved by 16.6% for NumPy, 14.9% for scikit-learn, and 16.7% for SciPy.
The success of APRIL underscores the importance of verifiable rewards. In programming, correctness is binary; by using unit tests as a reward signal, APRIL avoids the "surface-level similarity" trap of traditional fine-tuning. Furthermore, the synergy between Prompting and Tuning suggests that APO helps the model understand the task, while RLVR helps it master the library's specific patterns.
Limitations and Future Work
Currently, APRIL depends on reference implementations to generate test oracles, which may be a bottleneck for entirely new development. Future research will explore expanding the framework to more diverse libraries and handling incomplete user-provided test cases.
Summary updated