Fill in Middle

Fill-in-the-Middle (FIM) is a training objective and inference capability that enables code models to complete missing code given both the text before and after the cursor position — solving the fundamental limitation of standard left-to-right language models that can only see preceding context, by training models to predict masked spans using both prefix (code above cursor) and suffix (code below cursor), making it the essential technique powering production IDE features like Copilot's inline completions where the model must generate code that fits coherently between existing code blocks.

What Is FIM?

  • Definition: A training technique where code spans are randomly extracted from their original position and relocated to the end of the training sequence — the model learns to reconstruct the missing middle given both the prefix (text before the gap) and suffix (text after the gap).
  • The Problem with Standard LLMs: Causal (left-to-right) language models only see text before the cursor. When a developer's cursor is inside a function with code both above and below, a standard model cannot use the code below to inform its suggestion — leading to completions that conflict with what comes next.
  • FIM Training Format: During training, a random span is extracted and the sequence is rearranged:
Original: def add(a, b): result = a + b; return result
FIM Format: <PRE> def add(a, b): <SUF> return result <MID> result = a + b;

The model learns that `<MID>` is the content that goes between `<PRE>` and `<SUF>`.

Why FIM Matters for IDE Completions

ScenarioWithout FIMWith FIM
Cursor inside function bodyOnly sees code above — may conflict with return statement belowSees code above AND below — generates consistent completion
Editing between existing linesUnaware of following codeAdapts to match surrounding context
Adding to middle of classIgnores existing methods belowGenerates code consistent with class structure
Infilling function argumentsOnly sees function nameSees both function signature and body

Models Trained with FIM

ModelFIM SupportFIM TrainingPerformance Impact
Code LlamaYes (all variants)7% of training data+15% on infilling benchmarks
StarCoderYes50/50 prefix-suffix-middleSignificant improvement
DeepSeek CoderYesRepository-level FIMState-of-the-art infilling
InCoder (pioneer)Yes (pioneered FIM)Full FIM trainingFirst model to demonstrate FIM
CodestralYesOptimized FIMProduction IDE focus

FIM is the indispensable training technique that enables practical IDE code completion — by teaching models to predict missing code given both surrounding context, FIM transforms language models from left-to-right text generators into bidirectionally-aware coding assistants that generate completions fitting seamlessly into existing codebases.

Take fill in middle further

Ask the copilot about this term, or have our engineers assess it against your process.