Sub topic 3
Fill-in-the-middle prompting
This chapter assumes you know what a large language model is, can read Python, and understand the prefix and suffix context described in the previous chapter. It covers how a model is asked to write text between two pieces of existing code.
Why left-to-right is not enough
A standard language model predicts the next token from the tokens before it. That works at the end of a file. In an editor, the cursor is usually in the middle of code, and the text after it matters. Consider this function:
def add(a, b):
return
print(add(1, 2))A model that sees only the text before the cursor knows nothing about how add is called. It also cannot tell whether the code after the cursor already contains a closing bracket or a following statement. Fill-in-the-middle (FIM) prompting fixes this by giving the model both sides and asking it to produce only the missing middle.
How a FIM prompt is built
FIM models are trained on documents that are cut into three parts: a prefix, a middle, and a suffix. The training text is rearranged so the model sees the prefix and suffix first and then learns to generate the middle. Special sentinel tokens mark each part. At request time, you build the same layout and leave the middle empty.
The sentinel tokens differ between models. This example uses the StarCoder style.
def build_fim_prompt(prefix: str, suffix: str) -> str:
# StarCoder-style sentinel tokens; other models use different ones.
return f"<fim_prefix>{prefix}<fim_suffix>{suffix}<fim_middle>"
document = "def add(a, b):\n return \n\nprint(add(1, 2))\n"
cursor = document.index("return ") + len("return ")
prompt = build_fim_prompt(document[:cursor], document[cursor:])
print(prompt)The prompt ends with <fim_middle>. The model continues from that point, so its output is the text that belongs at the cursor. For this input, a good answer is a + b.
Prompt order
The order in the prompt is a design choice. The layout above is called prefix-suffix-middle (PSM). Some models use suffix-prefix-middle (SPM), where the suffix comes first. Both put the middle last, because generation always continues from the end of the prompt. Use the order and tokens the model was trained with. A mismatch does not raise an error. It produces poor completions, which makes it hard to diagnose.
Where the stop point comes from
The model does not know where the middle should end unless it learned to. FIM training teaches it to emit an end-of-text marker once the middle joins smoothly with the suffix. In practice, completions can still run on, repeat the suffix, or stop early. Three controls help:
- Set a maximum token count so a runaway completion cannot become very long.
- Add stop sequences, such as a blank line, when you only want a single statement.
- Post-process the result, which is the topic of a later chapter.
Common errors
| Problem | Likely cause | Fix |
|---|---|---|
| Output repeats code that follows the cursor | Model does not stop at the join with the suffix | Trim the overlap in post-processing |
| Output ignores the suffix | Wrong sentinel tokens or wrong order | Check the model's documentation |
| Output is unrelated to the file | Prefix was truncated at the wrong place | Keep text nearest the cursor when truncating |
| Poor quality at end of file | Suffix is empty | Use a normal prefix-only prompt or an empty suffix as trained |
When the context is too long, trim the prefix from the far end and the suffix from the far end. The text closest to the cursor is the most useful on both sides.
Exercise
A FIM prompt for a model that uses the StarCoder tokens ends with <fim_middle>. Why must the middle come last, and what happens if you send the tokens in the wrong order for a model trained on a different layout?
Show answer
The model generates by continuing from the end of the prompt, so the position where the middle begins must be the last thing it sees. If the order or tokens do not match the model's training layout, it treats the sentinels as plain text or misreads which part is which, and the completions degrade without any error message.
Exercise
Write the prompt string that buildfimprompt returns for prefix "x = " and suffix "\nprint(x)".
Show answer
<fimprefix>x = <fimsuffix> print(x)<fim_middle>