Abstract
Large Language Models (LLMs) frequently fail to utilize updated APIs because their internal training data conflicts with new documentation provided in prompts. This paper introduces ReCode, a framework that leverages rule-based Reinforcement Learning (RL) to train models to prioritize updated information. By employing syntax-aware rewards and Chain-of-Thought reasoning, ReCode allows a 7B parameter model to outperform 32B reasoning models on complex API migration tasks while preserving general coding proficiency.
Introduction
The rapid evolution of software libraries like PyTorch, NumPy, and Pandas creates a significant challenge for "frozen" LLMs. When an API changes—such as the deprecation of np.product in favor of np.prod—models often persist in generating outdated code. This phenomenon, known as knowledge conflict, occurs even when the correct documentation is provided in the prompt. Models exhibit a form of "laziness," defaulting to stale patterns embedded in their parameters.
Standard Supervised Fine-Tuning (SFT) often fails to resolve this, as it encourages memorization rather than the general ability to adapt to new instructions. ReCode addresses this by treating API migration as a reinforcement learning problem.
The ReCode Framework
ReCode introduces a "read and migrate" paradigm through three core components:
1. Reinforcement Fine-Tuning (RFT)
Instead of traditional next-token prediction, ReCode utilizes RL algorithms—specifically Group Reward Policy Optimization (GRPO) and DAPO. These algorithms reward the model for successfully migrating code based on provided documentation, forcing it to interpret the prompt rather than relying solely on internal weights.
2. Syntax-Aware Rewards
A critical innovation in ReCode is its multi-faceted reward function. To ensure code quality and correctness, the authors combine:
- AST-based Syntax Checking: Verifying the syntactic validity of the generated code using Abstract Syntax Trees.
- Modified String Matching (ES and EM): Providing rewards based on the proximity to the target "new" version, contingent on the code being compilable.
3. Chain-of-Thought (CoT) Reasoning
Models are trained to use <think> tags to reason through API changes before generating the final output. This mimics human developer behavior, mapping old logic to new interfaces systematically.
Experimental Setup
The authors constructed a training dataset of 1,786 entries by scraping release notes from major Python libraries (e.g., NumPy, PyTorch, Scikit-Learn). GPT-4 generated the initial code pairs, which were then human-verified. Performance was evaluated on CodeUpdateArena, a benchmark for API updates, and HumanEval+ to monitor general coding skills.
Results and Analysis
ReCode demonstrates that RL generalizes significantly better than SFT for knowledge updates. While SFT often leads models to ignore documentation, RL penalizes "stale" knowledge and rewards adherence to new rules.
Key performance highlights include:
- Superior Efficiency: A ReCode-tuned Qwen2.5-Coder-7B-Instruct achieved a 78.7% Pass@1 on CodeUpdateArena, outperforming the 32B version of the same model (75.7%) and the reasoning-optimized DeepSeek-R1-Distill-Qwen-32B (78.2%).
- Mitigating Catastrophic Forgetting: Unlike SFT, which caused an 11-point drop on HumanEval+, ReCode only resulted in a 2-point loss, preserving the model's general utility.
- Handling Complexity: The models successfully managed multi-step updates and multiple simultaneous API changes within single code blocks.
Conclusion and Future Work
ReCode provides a robust method for updating LLM knowledge without the need for full retraining. By incorporating syntax as a primary signal, the model learns to prioritize valid, updated code. Future research will explore scaling this approach to 70B+ parameter models and expanding beyond Python into other dynamic fields like legal or medical documentation.
Summary updated