Aider 'SEARCH/REPLACE block failed to match' Fix 2026: Every Cause of the Edit-Format Error and How to Clear It

aiderollamaedit-formattroubleshootinglocal-llm

TL;DR: Aider fails to apply an edit for one reason: the model returned a SEARCH block that does not byte-for-byte match the file. The fix is almost always to match the edit format to the model — whole for weak or local models, diff for strong ones, diff-fenced for Gemini — then to re-/add the file so Aider’s copy is current. Do those two things and the retry loop stops.

SymptomMost likely causeThe fix
SEARCH/REPLACE block failed to match! on a large or old fileModel can’t reproduce the block exactlyUse --edit-format whole, or /drop then /add the file
The LLM did not conform to the edit format on a local modeldiff is too hard for a small modelSwitch that model to whole in .aider.model.settings.yml
Fails, retries 3×, then gives upWrong format for the model familyMatch format to model (Gemini → diff-fenced)
Worked, then broke mid-sessionAuto-formatter or you edited the file outside Aider/drop and /add again to refresh the cache

Honest take: 90% of these errors are a format mismatch, not a bug. Put a strong model on diff and a small local model on whole, keep files small, and re-add anything you touched outside the chat — the loop disappears.

Verified against Aider 0.86.2 (released Feb 12, 2026) on July 19, 2026.

What the error actually means

Aider does not ask the model to rewrite your whole file for every change (unless you tell it to). Instead it asks for a SEARCH/REPLACE block: a chunk of the existing code to find, and the code to replace it with. The block looks like this:

mymodule.py
<<<<<<< SEARCH
def add(a, b):
    return a - b
=======
def add(a, b):
    return a + b
>>>>>>> REPLACE

Aider then does a literal, character-for-character search for everything between SEARCH and =======. If that text is not found in the file exactly — same indentation, same blank lines, same comments, same trailing whitespace — the match fails and you get one of these two messages:

The LLM did not conform to the edit format.

or the more specific:

# 1 SEARCH/REPLACE block failed to match!

## SearchReplaceNoExactMatch: This SEARCH block failed to exactly match lines in mymodule.py

Aider will retry a couple of times, sending the error back to the model and asking it to fix the block. Strong models usually recover on the second try. Weak models loop and eventually give up. Understanding why the block didn’t match tells you which fix to reach for.

The root cause in one sentence

The SEARCH section must be an exact copy of lines that currently exist in the file. Every mismatch below comes down to a violation of that rule:

  • The model paraphrased the code instead of copying it (dropped a blank line, “cleaned up” a comment, changed ' to ").
  • The model used the wrong format entirely — it emitted a git-style @@ ... @@ diff or a Markdown code block instead of a SEARCH/REPLACE block.
  • Aider’s copy of the file is stale — the file changed on disk after you added it, so the block matches what the model saw but not what’s there now.
  • Whitespace drift — the file uses CRLF line endings, tabs vs spaces, or a formatter re-indented it between turns.

Fix 1 — Match the edit format to your model (fixes most cases)

Aider ships several edit formats, and models differ wildly in which one they can follow. Pick the wrong one and even a capable model fails constantly. The formats you care about:

FormatWhat the model returnsBest for
wholeThe entire updated fileWeak and local models; small files
diffSEARCH/REPLACE blocksStrong models (Claude, GPT, DeepSeek) — the default
diff-fencedSEARCH/REPLACE with the path inside the fenceThe Gemini family
udiffA simplified unified diffGPT-4 Turbo-class models
architectA reasoning model plans, a second “editor” model formats the editFrontier reasoning models

Set it at launch:

# Force whole-file rewrites — the most forgiving format
aider --edit-format whole

# Gemini models want diff-fenced
aider --model gemini/gemini-2.5-pro --edit-format diff-fenced

If a specific model always misbehaves, encode the right default so you never have to remember. Create or edit .aider.model.settings.yml in your project (or home directory):

- name: ollama_chat/qwen2.5-coder:7b
  edit_format: whole
  use_repo_map: true

This is the single highest-leverage change. Gemini 2.5 Pro, for example, is well documented (Aider issue #3713) for botching SEARCH/REPLACE blocks until several retries — putting it on diff-fenced clears that up.

Fix 2 — For local / small models, use whole

Local models running through Ollama or LM Studio are the number-one source of this error. Producing a SEARCH block that matches the source byte-for-byte is genuinely hard, and a 7B–14B model will drop a blank line or re-wrap a comment on almost every edit. Aider’s own guidance is blunt about this: most local LLMs struggle even with the whole format, and all of them do far worse than a hosted frontier model.

So give them the easiest possible job — regenerate the whole file:

aider --model ollama_chat/qwen2.5-coder:14b --edit-format whole

Two things make whole work better locally:

  1. Use a coder-tuned model. qwen2.5-coder, deepseek-coder-v2, and codestral follow the format far better than a general chat model of the same size.
  2. Raise the context window. Ollama defaults to a 2K–4K context, which silently truncates the file the model is supposed to reproduce — a guaranteed match failure. Set num_ctx to at least 8K–16K. That trap is the same one behind most local-model context problems; we cover it in detail in our Ollama context length fix guide.

If you’re deciding whether your machine can even run a model big enough to hold the file, our sister site covers the hardware math in best local AI models by VRAM.

Fix 3 — Shrink the scope

The bigger the file and the more files in the chat, the more chances the model has to misquote a block. If you added ten files and pointed a mid-size model at a 900-line module, failures are almost guaranteed.

  • /drop everything you don’t need for the current change. Fewer files = a cleaner map and fewer distractors.
  • Ask for one change at a time. “Refactor the whole auth layer” invites five shaky blocks; “rename get_user to fetch_user in auth.py” produces one clean block.
  • For a big edit on a big file, switch that turn to --edit-format whole so a single fuzzy block can’t sink the whole operation.

Fix 4 — Refresh Aider’s copy of the file

If the edit worked a minute ago and now fails on the same file, Aider is probably holding a stale copy. This happens when:

  • A formatter (Prettier, Black, gofmt) reformatted the file after Aider read it.
  • You edited the file in your own editor while Aider had it open.
  • A previous Aider edit partially applied and left the file in a state the model doesn’t expect.

The cure is to force Aider to re-read from disk:

/drop src/app.py
/add src/app.py

Then retry the request. You can also /reset to clear the entire chat and file set and start clean — useful when several files have drifted at once.

Fix 5 — Rule out whitespace and line-ending drift

A SEARCH block can look identical and still fail because of invisible characters:

  • CRLF vs LF. A file with Windows line endings won’t match a block the model wrote with Unix endings. Normalize with .gitattributes (* text=auto) or your editor’s line-ending setting.
  • Tabs vs spaces. If the file mixes them, the model often “helpfully” normalizes to spaces in its SEARCH block — which then doesn’t match the tab in the file.
  • Trailing whitespace. An editor that strips trailing spaces on save changes the file out from under a block that included them.

If you can’t tell what’s different, run the change on a fresh checkout of the file with a consistent formatter applied before you start the Aider session, so the model and the file agree on the same style.

Fix 6 — Fix architect-mode mismatches

In architect mode Aider uses two models: a reasoning model to plan the change and an editor model to turn that plan into edits. The edit-format error here comes from the editor model, not the planner. If your editor model is weak, set its format independently:

aider --model o3 --editor-model gpt-4o --editor-edit-format editor-diff

If the editor is a small local model, use editor-whole instead. The plan can come from a smart-but-slow model while a reliable formatter applies it.

Fix 7 — Upgrade Aider

Edit-block matching has a fuzzy-matching layer that has improved steadily across releases — Aider works hard to accept edits that are “almost” correctly formatted. If you’re on an old version, upgrade before you debug further:

# Recommended installer keeps aider in its own isolated environment
python -m pip install aider-install && aider-install

# Or upgrade the package directly
python -m pip install --upgrade aider-chat

Confirm the version with aider --version (0.86.2 as of this writing).

Quick decision guide by model

Your modelSet this formatNotes
Claude / GPT-5 / DeepSeek V4 (hosted)diff (default)Leave it; these handle SEARCH/REPLACE well
Gemini 2.5 Pro / Flashdiff-fencedThe single most common Gemini fix
Any Ollama / LM Studio local modelwholePlus num_ctx ≥ 8K and a coder-tuned model
Reasoning model as architectarchitect + editor-diffWeak editor → editor-whole
Very large single filewhole for that turnAvoids one bad block failing everything

Not sure which format your model does best with? The Aider Polyglot leaderboard publishes the format each ranked model was tested with — a good starting point for BYOK setups.

A worked example

Here’s the whole fix loop in practice with a local model that kept failing:

$ aider --model ollama_chat/qwen2.5-coder:7b app.py
# ... request an edit ...
# The LLM did not conform to the edit format.
# 1 SEARCH/REPLACE block failed to match!

# Stop. Set the right format for a small model:
$ aider --model ollama_chat/qwen2.5-coder:7b --edit-format whole app.py

# Still shaky? The context window is truncating the file. In your Modelfile
# or via the API options, raise it:
#   PARAMETER num_ctx 16384
# Then re-run. The edit now applies cleanly.

Two changes — whole format and a real context window — turn a constant failure into a reliable one.

FAQ

Is “SEARCH/REPLACE block failed to match” a bug in Aider? Almost never. It means the model produced a block that doesn’t match the file. Aider is doing exactly what it should — refusing to apply an edit it can’t place safely.

Why does it retry and then give up? Aider sends the error back to the model up to a few times, asking it to correct the block. Strong models fix it on the retry; weak models keep producing the same broken block, so Aider stops rather than loop forever.

Does whole format cost more? Yes — the model returns the entire file, so you pay for more output tokens. For hosted frontier models, keep diff to save money. Use whole only for local models (where tokens are free) or as a targeted fix for a stubborn file.

Will switching models fix it? Often, yes. A hosted model like Claude or DeepSeek on the default diff format almost never hits this. If a local model fails constantly even on whole, it may simply be too small for reliable editing — step up a size class or move that task to a hosted model.

My local model prints @@ or Markdown fences instead of SEARCH blocks. What now? That’s the model ignoring the format instructions. Force --edit-format whole, which is far simpler to follow, and use a coder-tuned model. If it still emits diffs, the model isn’t capable enough for agentic editing.

Sources

Related on AICoderScope: Aider Model Not Found Fix 2026 · Aider with Local LLM via Ollama · Ollama Context Length Fix for Cline, Continue.dev, and Aider. For self-hosted open-source coding stacks, see aifoss.dev. For the hardware behind local models, see runaihome.com.

Last verified: Jul 19 2026.

Was this article helpful?