Home Knowledge Base Fill-in-the-Middle (FIM)

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?

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 is the content that goes between

 and .

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.

fill in middlefiminfill

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.