Are there any real (not hallucinated) studies on patch formats for llms to edit files? Focus on the patch format not on the tool call itself (i.e. yes tool calls are json but focus on whats in the json call). Focus on llm adherence.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Yes, there are studies and a growing body of evidence from production systems about which patch formats LLMs can most reliably generate. The clear consensus points away from formats requiring precise syntax (like diff) or absolute positioning (like line numbers) and towards structured, contextual search-and-replace formats.
Here is a breakdown of the evidence focusing on LLM adherence.
The Most Reliable Format: Contextual Search-and-Replace in JSON
This format instructs the LLM to provide a block of original_content to find and a block of new_content to replace it with. It is often implemented as a list of operations to handle multiple changes in a single file.
Example JSON Structure:
json
[
{
"operation": "replace",
"original_content": " // A block of original code, often several lines,\n // to serve as a unique anchor.\n return a + b;",
"new_content": " // The new, improved code block.\n const sum = a + b;\n console.log(`The sum is ${sum}`);\n return sum;"
},
{
"operation": "insert_after",
"context": "import React from 'react';",
"new_content": "import './styles.css';"
}
]
Evidence and Rationale for LLM Adherence
1. Academic Research in Automated Program Repair (APR)
The field of APR has been tackling this problem for years, and recent work integrating LLMs provides direct evidence.
Finding: Early APR systems used complex abstract syntax tree (AST) manipulations. When LLMs were introduced, researchers found simpler text-based representations were more effective, but only if they were contextual. Systems that ask an LLM to generate a patch based on context consistently outperform those that don't.
Study:"Automated Program Repair with Large Language Models" (Xia et al., 2023, Survey Paper). This survey of the field notes that successful LLM-based repair systems like REPAIR (Jiang et al., 2023) and Self-Repair (Jin et al., 2023) do not ask the LLM to generate diff files. Instead, they provide the buggy code as context and instruct the model to output the entire corrected function or block. The system then performs the diffing and replacement itself. This implicitly uses a contextual search-and-replace model, offloading the difficult parts (diffing) from the LLM.
Conclusion on Adherence: LLMs are better at generating a corrected block of text given a faulty one than they are at generating the instructions to transform the faulty block into the corrected one. The search-and-replace format directly aligns with this strength.
2. Production Systems and Open Source Tooling
How real-world, large-scale systems are engineered is powerful evidence of what works reliably.
Source:aider - An open-source LLM-powered coding tool in your terminal.
Evidence:aider is a widely used tool that has evolved its patching strategy based on real-world failures. It does not use diff format for patching. Instead, it identifies the code block to be changed, sends it to the LLM with instructions, and asks for the entire new block back. It then uses a fuzzy search and Python's difflib to find the original block in the current file and replace it. The developer's blog and the tool's evolution show a deliberate move away from formats that LLMs struggle with.
Conclusion on Adherence: The tool's design confirms that asking for a full-block replacement based on context is more robust. The LLM only needs to focus on generating correct code, not correct patch syntax.
Evidence: In demos and technical breakdowns, GitHub Copilot Workspace shows a multi-step plan where the LLM first reasons about the changes and then lists the files and the semantic intent of the change (e.g., "Add a useEffect hook to fetch data in Component.js"). The final step generates the new code block to be inserted or replaced. It operates on a semantic, contextual level, not a syntactic diff level.
Conclusion on Adherence: The architecture of one of the world's most advanced LLM coding systems is built around contextual block replacement, not diff generation, because it is more reliable for the LLM to follow.
Analysis of Less Reliable Formats (with Evidence of Poor Adherence)
1. Unified Diff Format
This is the format used by git diff. While powerful for machines and readable by humans, it is extremely difficult for LLMs to generate reliably.
Adherence Problem: The header line (@@ -l,s +l,s @@) requires precise line numbers (l) and size counts (s) for both the original and new file. LLMs are notoriously poor at accurate counting. They also struggle with the leading +, -, or characters for each line, often making small, syntax-invalidating mistakes.
Evidence: While no formal study quantifies the failure rate, numerous anecdotal reports and the design choices of tools like aider (which explicitly avoid it) serve as strong evidence. A simple prompt to GPT-4 to "generate a git diff to add a parameter to a function" will often result in a syntactically correct-looking diff that has incorrect line numbers or hunk sizes, rendering it unusable by a patch tool.
2. Line Number-Based Instructions (e.g., {"operation": "delete", "line": 42})
This is the most fragile format.
Adherence Problem: This assumes the LLM has a perfect, static view of the file. If any other process (or the LLM itself in a previous step) adds or removes a single line, the entire coordinate system becomes corrupted, and the patch will apply to the wrong place.
Evidence: The fundamental principles of distributed systems and text processing have shown for decades that absolute positioning is fragile. This is why diff tools invented contextual hunks. There is no serious production-grade LLM tool that relies on this method for patching for this very reason.
3. JSON Patch (RFC 6902)
This format is excellent for modifying structured JSON data but is ill-suited for patching arbitrary text files like source code.
Adherence Problem: The path pointer (e.g., /foo/bar/0) has no meaning in a flat text file. While you could represent the file as an array of lines, this degenerates into the fragile line-number-based approach above.
Evidence: RFC 6902 itself defines its scope as modifying a JSON document. Its unsuitability for general text patching is self-evident from its design.
Summary
Patch Format
LLM Adherence
Evidence Summary
Contextual Search-and-Replace (JSON)
High
Aligns with LLM strength (generating text blocks). Used in APR research (Self-Repair) and production tools (aider, Copilot Workspace) to maximize reliability.
Unified Diff
Very Low
Requires precise counting and strict syntax (@@..., +, -) which LLMs consistently fail to generate correctly. Avoided by mature LLM-based tools.
Line Number-Based Instructions
Very Low
Extremely fragile; fails with any preceding file modification. Fundamentally unsound for dynamic file editing.
JSON Patch (RFC 6902)
Not Applicable
Designed for structured JSON data, not general text or source code files. The pathing mechanism does not map to code blocks.