TL;DR
Small open-source code models used for IDE autocomplete frequently hallucinate non-existent methods, variables, and imports. This paper introduces an execution-free pipeline that uses frontier models to synthesize these plausible-but-wrong completions as "hard negatives." By filtering and fine-tuning smaller models on data derived from these hard negatives, the authors achieve massive gains in hallucination mitigation (+18.8 exact match points) without the need for slow, language-specific execution sandboxes.
What Problem Does This Paper Address?
AI-powered code autocomplete needs to be incredibly fast, delivering suggestions within a few hundred milliseconds of a keystroke. Because of this speed requirement and strict data privacy constraints, production code assistants rely on small, local models (typically in the 3B to 7B parameter range) rather than massive frontier APIs.
However, these smaller models suffer from a persistent issue: code hallucination. They frequently generate completions that look syntactically correct but violate the project's actual state—such as calling a method that doesn't exist, fabricating arguments, or inventing fake library imports.
Traditional ways to fix this rely on running the generated code in a secure sandbox to see if it passes unit tests. Unfortunately, this breaks down during mid-keystroke autocomplete because the code is often incomplete and won't even parse, let alone run. Other methods require massive, expensive human-labeled preference datasets.
Methods
The authors propose an execution-free alternative by flipping the standard synthetic data recipe on its head. Instead of trying to generate correct code and verifying it via execution, they use frontier models (like GPT-4/5 variants) to intentionally generate wrong-but-plausible code to serve as hard negatives.
Because every real-world code snippet already contains the "golden" ground-truth completion, they don't need an executor to verify correctness. The pipeline works as follows:
- FIM Sampling: Extract real prefix/suffix contexts from public repositories.
- Hallucination Generation: A panel of three frontier models generates a specific type of hallucination (e.g., creating a fake method name or an undefined variable).
- Difficulty Scoring (Fool Rate): A separate panel of LLM judges reviews the code blind. The percentage of judges deceived by the hallucination determines its "fool rate."
- Curation & Fine-Tuning: The dataset is filtered to keep the hardest, most deceptive examples. Smaller models are then trained using Supervised Fine-Tuning (SFT) on the golden completions of these difficult contexts.
Datasets
The authors scraped public GitHub repositories to collect ~400K multilingual Fill-in-the-Middle (FIM) contexts across eight languages: C#, Go, Java, JavaScript, PHP, Python, Ruby, and Rust. This was curated down to a balanced 100K-row training set.
For evaluation, they tested their models across four major benchmarks:
- Delulu: A Docker-verified multilingual FIM hallucination benchmark.
- HumanEval-Infilling: The canonical benchmark for Python FIM evaluation.
- SAFIM: A broad, enterprise-style FIM benchmark evaluating API and block completions.
- Real-FIM-Eval: A dataset of real, recent code additions extracted from GitHub commits.
Key Insights and Ideas
- SFT is Better Than DPO for Autocomplete: Common wisdom suggests using contrastive preference optimization (like DPO) for chosen/rejected pairs. However, the authors found that standard DPO collapses on FIM tasks because it fails to learn stopping behavior, causing models to over-generate past the autocomplete gap. Simple SFT on data curated via hard negatives naturally respects boundary lines.
- Cross-Lingual Transfer Ignites Performance: Training a model on non-Python languages (like Java or C#) significantly improves its performance on Python benchmarks. This is because the underlying structural pattern of a hallucination—such as inventing an identifier or fabricating a parameter—is fundamentally language-independent.
- The "Disputed Middle" is Most Valuable: When filtering data by difficulty, the most effective training signals don't come from the easiest hallucinations or the completely indistinguishable ones. Instead, they come from the "disputed middle"—examples where LLM judges disagree on whether the code is a hallucination or not.
Key Results
The authors applied their recipe to Qwen2.5-Coder-7B-Instruct and Qwen2.5-Coder-3B-Instruct:
- 7B Model: Achieved a massive +18.8 point lift in Exact Match (EM) on the Delulu benchmark, showing improvements across every single language and hallucination type. It also improved performance across all general FIM benchmarks (HumanEval and SAFIM).
- 3B Model: Gained +12.8 EM points on Delulu. Remarkably, the fine-tuned 3B model outperformed the original base 7B model by 15.7 points, proving that high-quality targeted data can compensate for smaller parameter sizes.
Limitations and Future Work
- Taxonomy Constraints: The pipeline currently targets four specific identifier-level hallucination types (methods, parameters, undefined variables, and imports). It does not address other real-world hallucinations like off-by-one errors, type-annotation drift, or async/await misuse.
- Small-Model Capacity Trade-offs: While the 7B model improved across the board, the smaller 3B model suffered from a slight capacity trade-off, regressing on multi-line Python completions as it over-indexed on the shorter completions common in the training set.
- Generation Cost: Generating millions of synthetic tokens from frontier models to build the training pool requires significant upfront compute and API costs.
Summary updated