Table of Contents
Fetching ...

From Completion to Editing: Unlocking Context-Aware Code Infilling via Search-and-Replace Instruction Tuning

Jiajun Zhang, Zeyu Cui, Jiaxi Yang, Lei Zhang, Yuheng Jing, Zeyao Ma, Tianyi Bai, Zilei Wang, Qiang Liu, Liang Wang, Binyuan Hui, Junyang Lin

TL;DR

The paper addresses the rigidity and security vulnerabilities of Fill-in-the-Middle (FIM) for code completion by introducing Search-and-Replace Infilling (SRI), a context-aware, single-pass editing framework. SRI grounds edits with an explicit search phase and outputs a replace block, enabling robust, patch-based code edits that align with pretraining on diff-style data and work safely with instruction-tuned Chat LLMs. A high-quality SRI-200K dataset is synthesized and used to fine-tune the SRI-Coder series, demonstrating that with as little as 20k fine-tune samples, Chat models can outperform their base counterparts on standard benchmarks while maintaining inference latency comparable to FIM and preserving general coding proficiency. Across five benchmarks and diverse model families, SRI consistently outperforms NL-FIM prompting, scales favorably with model size, and fundamentally improves robustness to contextual noise, suggesting a practical and secure pathway for next-generation auto-completion and assisted development tooling.

Abstract

The dominant Fill-in-the-Middle (FIM) paradigm for code completion is constrained by its rigid inability to correct contextual errors and reliance on unaligned, insecure Base models. While Chat LLMs offer safety and Agentic workflows provide flexibility, they suffer from performance degradation and prohibitive latency, respectively. To resolve this dilemma, we propose Search-and-Replace Infilling (SRI), a framework that internalizes the agentic verification-and-editing mechanism into a unified, single-pass inference process. By structurally grounding edits via an explicit search phase, SRI harmonizes completion tasks with the instruction-following priors of Chat LLMs, extending the paradigm from static infilling to dynamic context-aware editing. We synthesize a high-quality dataset, SRI-200K, and fine-tune the SRI-Coder series. Extensive evaluations demonstrate that with minimal data (20k samples), SRI-Coder enables Chat models to surpass the completion performance of their Base counterparts. Crucially, unlike FIM-style tuning, SRI preserves general coding competencies and maintains inference latency comparable to standard FIM. We empower the entire Qwen3-Coder series with SRI, encouraging the developer community to leverage this framework for advanced auto-completion and assisted development.

From Completion to Editing: Unlocking Context-Aware Code Infilling via Search-and-Replace Instruction Tuning

TL;DR

The paper addresses the rigidity and security vulnerabilities of Fill-in-the-Middle (FIM) for code completion by introducing Search-and-Replace Infilling (SRI), a context-aware, single-pass editing framework. SRI grounds edits with an explicit search phase and outputs a replace block, enabling robust, patch-based code edits that align with pretraining on diff-style data and work safely with instruction-tuned Chat LLMs. A high-quality SRI-200K dataset is synthesized and used to fine-tune the SRI-Coder series, demonstrating that with as little as 20k fine-tune samples, Chat models can outperform their base counterparts on standard benchmarks while maintaining inference latency comparable to FIM and preserving general coding proficiency. Across five benchmarks and diverse model families, SRI consistently outperforms NL-FIM prompting, scales favorably with model size, and fundamentally improves robustness to contextual noise, suggesting a practical and secure pathway for next-generation auto-completion and assisted development tooling.

Abstract

The dominant Fill-in-the-Middle (FIM) paradigm for code completion is constrained by its rigid inability to correct contextual errors and reliance on unaligned, insecure Base models. While Chat LLMs offer safety and Agentic workflows provide flexibility, they suffer from performance degradation and prohibitive latency, respectively. To resolve this dilemma, we propose Search-and-Replace Infilling (SRI), a framework that internalizes the agentic verification-and-editing mechanism into a unified, single-pass inference process. By structurally grounding edits via an explicit search phase, SRI harmonizes completion tasks with the instruction-following priors of Chat LLMs, extending the paradigm from static infilling to dynamic context-aware editing. We synthesize a high-quality dataset, SRI-200K, and fine-tune the SRI-Coder series. Extensive evaluations demonstrate that with minimal data (20k samples), SRI-Coder enables Chat models to surpass the completion performance of their Base counterparts. Crucially, unlike FIM-style tuning, SRI preserves general coding competencies and maintains inference latency comparable to standard FIM. We empower the entire Qwen3-Coder series with SRI, encouraging the developer community to leverage this framework for advanced auto-completion and assisted development.
Paper Structure (52 sections, 10 equations, 14 figures, 11 tables)

This paper contains 52 sections, 10 equations, 14 figures, 11 tables.

Figures (14)

  • Figure 1: Performance disparity between Base models and their Instruction-tuned counterparts on standard code completion benchmarks. The results highlight a significant degradation in completion capabilities (up to 59.0%) when prompting safety-aligned Chat LLMs for FIM tasks, demonstrating that FIM skills do not naturally transfer to the conversational setting.
  • Figure 2: The workflow of our proposed Search-and-Replace Infilling (SRI) method. (1) The process begins with a code completion task containing a pre-existing bug (an incorrect loop variable) and an /* MIDDLE CODE TO COMPLETE */ identifier marking the target location. (2) Our SRI model generates a search-and-replace block that simultaneously infills the missing function body and corrects the contextual bug. (3) This block can then be converted into a standard patch format for versatile application.
  • Figure 3: SRI editable region size sensitivity analysis.
  • Figure 4: Ablation study comparing SRI-Coder-32B against three natural language-based FIM (NL-FIM) fine-tuning variants. (Left) On code completion benchmarks, SRI significantly outperforms the NL-FIM strategies. (Right) On general coding benchmarks, SRI preserves the model's broader competencies, whereas NL-FIM tuning induces measurable performance regression.
  • Figure 5: Scaling laws for FIM vs. SRI. Performance (left, %) and average inference time (right, minutes) across model sizes on code completion benchmarks.
  • ...and 9 more figures