ApplyPatch 1.1.0

dotnet add package ApplyPatch --version 1.1.0
                    
NuGet\Install-Package ApplyPatch -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ApplyPatch" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApplyPatch" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ApplyPatch" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ApplyPatch --version 1.1.0
                    
#r "nuget: ApplyPatch, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ApplyPatch@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ApplyPatch&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ApplyPatch&version=1.1.0
                    
Install as a Cake Tool

Build & Tests NuGet Version

ApplyPatch.Net

ApplyPatch.Net is a .NET library for applying the patch formats used by OpenAI tooling:

  • DiffApplier handles the original V4A-style contextual diff format.
  • OSSDiffApplier handles the *** Begin Patch / *** End Patch format used by GPT-OSS style models.

The library applies hunks by matching context in file contents rather than trusting line numbers, with the same kind of fuzzy matching those tools expect.

Installation

dotnet add package ApplyPatch

APIs

DiffApplier

Use this when you already have the original file content in memory and want to apply a contextual diff hunk directly.

using ApplyPatch;

string original = "line1\nline2\nline3\n";
string diff = string.Join("\n", new[]
{
    "@@ line1",
    "-line2",
    "+updated",
    " line3"
});

string result = DiffApplier.ApplyDiff(original, diff);

For create-only diffs, use ApplyDiffMode.Create:

string diff = string.Join("\n", new[]
{
    "+hello",
    "+world"
});

string result = DiffApplier.ApplyDiff(
    input: "",
    diff: diff,
    mode: DiffApplier.ApplyDiffMode.Create);

OSSDiffApplier

Use this when the model returns a full file-oriented patch envelope.

Supported operations:

  • *** Update File: <path>
  • *** Move to: <new path>
  • *** Delete File: <path>
  • *** Add File: <path>

Example:

using ApplyPatch;

string patch = string.Join("\n", new[]
{
    "*** Begin Patch",
    "*** Update File: src/old.txt",
    "*** Move to: src/new.txt",
    "@@",
    " alpha",
    "-beta",
    "+beta2",
    " gamma",
    "*** Delete File: src/remove.txt",
    "*** Add File: src/add.txt",
    "+first",
    "+second",
    "*** End Patch"
});

var files = new Dictionary<string, string>(StringComparer.Ordinal)
{
    ["src/old.txt"] = "alpha\nbeta\ngamma",
    ["src/remove.txt"] = "remove me"
};

var writes = new Dictionary<string, string>(StringComparer.Ordinal);
var removes = new List<string>();

OSSDiffApplier.ApplyPatch(
    patch,
    openFn: path => files[path],
    writeFn: (path, content) => writes[path] = content,
    removeFn: path => removes.Add(path));

OSSDiffApplier.ApplyPatch(...) uses filesystem delegates so you can:

  • apply directly against disk with the built-in file helpers
  • test against an in-memory file map
  • intercept writes, deletes, and moves in your own storage layer

Useful helpers:

  • OSSDiffApplier.IdentifyFilesNeeded(text) returns files that must already exist because they are updated or deleted
  • OSSDiffApplier.IdentifyFilesAdded(text) returns files created by the patch
  • OSSDiffApplier.TextToPatch(text, orig) parses the patch and reports a Fuzz score for non-exact context matches
  • OSSDiffApplier.PatchToCommit(...) and OSSDiffApplier.ApplyCommit(...) let you split parsing from execution

Patch Semantics

Both appliers are context-based:

  • exact context matches are preferred
  • trailing-whitespace matches are allowed with low fuzz
  • trimmed matches are allowed with higher fuzz
  • invalid structure, missing files, duplicate operations, overlapping chunks, and unmatched context throw exceptions

OSSDiffApplier also supports *** End of File in truncated hunks and removes the original file when a patch moves it to a new path.

When To Use Which

Use DiffApplier when your application already knows which file it is patching and only needs to transform one text blob.

Use OSSDiffApplier when you want to consume the higher-level patch format emitted by GPT-OSS style models or similar agents that describe adds, deletes, updates, and moves across multiple files.

Attribution

This library ports the patch application behavior used by OpenAI tooling into C#. The DiffApplier implementation is based on the Python logic from openai-agents-python, and OSSDiffApplier implements the GPT-OSS style apply_patch envelope used by newer model workflows.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 42 3/9/2026