For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Apply Patch

Allow models to propose structured diffs that your integration applies.

The apply_patch tool lets GPT-5.1 create, update, and delete files in your codebase using structured diffs. Instead of just suggesting edits, the model emits patch operations that your application applies and then reports back on, enabling iterative, multi-step code editing workflows.

When to use

Some common scenarios where you would use apply_patch:

  • Multi-file refactors – Rename symbols, extract helpers, or reorganize modules across many files at once.
  • Bug fixes – Have the model both diagnose issues and emit precise patches.
  • Tests & docs generation – Create new test files, fixtures, and documentation alongside code changes.
  • Migrations & mechanical edits – Apply repetitive, structured updates (API migrations, type annotations, formatting fixes, etc.).

If you can describe your repo and desired change in text, apply_patch can usually generate the corresponding diffs.

Use apply patch tool with Responses API

At a high level, using apply_patch with the Responses API looks like this:

  1. Call the Responses API with the apply_patch tool
    • Provide the model with context about available files (or a summary) in your input, or give the model tools for exploring your file system.
    • Enable the tool with tools=[{"type": "apply_patch"}].
  2. Let the model return one or more patch operations
    • The Response output includes one or more apply_patch_call objects.
    • Each call describes a single file operation: create, update, or delete.
  3. Apply patches in your environment
    • Run a patch harness or script that:
      • Interprets the operation diff for each apply_patch_call.
      • Applies the patch to your working directory or repo.
      • Records whether each patch succeeded and any logs or error messages.
  4. Report patch results back to the model
    • Call the Responses API again, either with previous_response_id or by passing back your conversation items into input.
    • Include an apply_patch_call_output event for each call_id, with a status and optional output string.
    • Keep tools=[{"type": "apply_patch"}] so the model can continue editing if needed.
  5. Let the model continue or explain changes
    • The model may issue more apply_patch_call operations, or
    • Provide a human-facing explanation of what it changed and why.

Example: Renaming a function with Apply Patch Tool

Step 1: Ask the model to plan and emit patches

Ask the model to plan and emit patches
from openai import OpenAI

client = OpenAI()

# For brevity, we are including file context in the example input.
# Most agentic use cases should instead equip the model with tools
# for exploring file system state.
RESPONSE_INPUT = """
The user has the following files:
<BEGIN_FILES>
===== lib/fib.py
def fib(n):
    if n <= 1:
        return n
    return fib(n-1) + fib(n-2)

===== run.py
from lib.fib import fib

def main():
  print(fib(42))
<END_FILES>

You are a helpful coding assistant that should assist the user with whatever they
ask.

User query:
Help me rename the fib() function to fibonacci()
"""

response = client.responses.create(
    model="gpt-5.6",
    input=RESPONSE_INPUT,
    tools=[{"type": "apply_patch"}],
)

# response.output may contain multiple apply_patch_call entries, e.g.:
# - update lib/fib.py
# - update run.py
patch_calls = [
    item.model_dump() for item in response.output if item.type == "apply_patch_call"
]

Example apply_patch_call object

Example apply_patch_call object
{
    "id": "apc_08f3d96c87a585390069118b594f7481a088b16cda7d9415fe",
    "type": "apply_patch_call",
    "status": "completed",
    "call_id": "call_Rjsqzz96C5xzPb0jUWJFRTNW",
    "operation": {
        "type": "update_file",
        "diff": "
@@
-def fib(n):
+def fibonacci(n):
    if n <= 1:
        return n
-    return fib(n-1) + fib(n-2)                                                  +    return fibonacci(n-1) + fibonacci(n-2),
",
        "path": "lib/fib.py"
    }
}

Step 2: Apply the patch and send results back

Apply the patch and return results
from apply_patch_harness import apply_operation  # your implementation

results = []
for call in patch_calls:
    op = call["operation"]
    success, maybe_log_output = apply_operation(op)

    results.append(
        {
            "type": "apply_patch_call_output",
            "call_id": call["call_id"],
            "status": "completed" if success else "failed",
            "output": maybe_log_output,
        }
    )

followup = client.responses.create(
    model="gpt-5.6",
    previous_response_id=response.id,
    input=results,
    tools=[{"type": "apply_patch"}],
)