Learn best practices, features, and migration guidance for GPT-4.1.
Introduction
The GPT-4.1 family of models represents a significant step forward from GPT-4o in capabilities across coding, instruction following, and long context. In this prompting guide, we collate a series of important prompting tips derived from extensive internal testing to help developers fully leverage the improved abilities of this new model family.
Many typical best practices still apply to GPT-4.1, such as providing context examples, making instructions as specific and clear as possible, and inducing planning via prompting to maximize model intelligence. However, we expect that getting the most out of this model will require some prompt migration. GPT-4.1 is trained to follow instructions more closely and more literally than its predecessors, which tended to more liberally infer intent from user and system prompts. This also means, however, that GPT-4.1 is highly steerable and responsive to well-specified prompts - if model behavior is different from what you expect, a single sentence firmly and unequivocally clarifying your desired behavior is almost always sufficient to steer the model on course.
Please read on for prompt examples you can use as a reference, and remember that while this guidance is widely applicable, no advice is one-size-fits-all. AI engineering is inherently an empirical discipline, and large language models are inherently nondeterministic; in addition to following this guide, we advise building informative evals and iterating often to ensure your prompt engineering changes are yielding benefits for your use case.
What’s new
Closer and more literal instruction following than previous GPT models
Stronger coding and long-context behavior
Better API-native tool use when schemas are passed through the tools field
Prompt migration guidance for agentic workflows and diff generation
Migration quickstart
Update the model slug to gpt-4.1.
Use either the Responses API or Chat Completions API, depending on your integration.
Remove reasoning-specific parameters; GPT-4.1 is a non-reasoning model.
Pass tool schemas through the API tools field instead of injecting tool definitions into the prompt.
Review prompts for literal instruction following, add explicit persistence and tool-use rules where needed, and validate changes with evals.
Model, API, and feature updates
The GPT-4.1 family includes gpt-4.1, gpt-4.1-mini, and gpt-4.1-nano.
GPT-4.1 has a 1M-token context window and low latency without a reasoning step.
The family supports the Responses API and Chat Completions API.
GPT-4.1 and GPT-4.1 mini support supervised fine-tuning.
Supported tools include function calling, web search, file search, image generation, code interpreter, and remote MCP.
Prompting best practices
1. Agentic Workflows
GPT-4.1 is a great place to build agentic workflows. In model training we emphasized providing a diverse range of agentic problem-solving trajectories, and our agentic harness for the model achieves state-of-the-art performance for non-reasoning models on SWE-bench Verified, solving 55% of problems.
System Prompt Reminders
In order to fully utilize the agentic capabilities of GPT-4.1, we recommend including three key types of reminders in all agent prompts. The following prompts are optimized specifically for the agentic coding workflow, but can be easily modified for general agentic use cases.
Persistence: this ensures the model understands it is entering a multi-message turn, and prevents it from prematurely yielding control back to the user. Our example is the following:
You are an agent - please keep going until the user’s query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved.
Tool-calling: this encourages the model to make full use of its tools, and reduces its likelihood of hallucinating or guessing an answer. Our example is the following:
If you are not sure about file content or codebase structure pertaining to the user’s request, use your tools to read files and gather the relevant information: do NOT guess or make up an answer.
Planning [optional]: if desired, this ensures the model explicitly plans and reflects upon each tool call in text, instead of completing the task by chaining together a series of only tool calls. Our example is the following:
You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.
GPT-4.1 is trained to respond very closely to both user instructions and system prompts in the agentic setting. The model adhered closely to these three simple instructions and increased our internal SWE-bench Verified score by close to 20% - so we highly encourage starting any agent prompt with clear reminders covering the three categories listed above. As a whole, we find that these three instructions transform the model from a chatbot-like state into a much more “eager” agent, driving the interaction forward autonomously and independently.
Tool Calls
Compared to previous models, GPT-4.1 has undergone more training on effectively utilizing tools passed as arguments in an OpenAI API request. We encourage developers to exclusively use the tools field to pass tools, rather than manually injecting tool descriptions into your prompt and writing a separate parser for tool calls, as some have reported doing in the past. This is the best way to minimize errors and ensure the model remains in distribution during tool-calling trajectories - in our own experiments, we observed a 2% increase in SWE-bench Verified pass rate when using API-parsed tool descriptions versus manually injecting the schemas into the system prompt.
Developers should name tools clearly to indicate their purpose and add a clear, detailed description in the “description” field of the tool. Similarly, for each tool param, lean on good naming and descriptions to ensure appropriate usage. If your tool is particularly complicated and you’d like to provide examples of tool usage, we recommend that you create an # Examples section in your system prompt and place the examples there, rather than adding them into the “description” field, which should remain thorough but relatively concise. Providing examples can be helpful to indicate when to use tools, whether to include user text alongside tool calls, and what parameters are appropriate for different inputs. Remember that you can use “Generate Anything” in the Prompt Playground to get a good starting point for your new tool definitions.
Prompting-Induced Planning & Chain-of-Thought
As mentioned already, developers can optionally prompt agents built with GPT-4.1 to plan and reflect between tool calls, instead of silently calling tools in an unbroken sequence. GPT-4.1 is not a reasoning model - meaning that it does not produce an internal chain of thought before answering - but in the prompt, a developer can induce the model to produce an explicit, step-by-step plan by using any variant of the Planning prompt component shown above. This can be thought of as the model “thinking out loud.” In our experimentation with the SWE-bench Verified agentic task, inducing explicit planning increased the pass rate by 4%.
Sample Prompt: SWE-bench Verified
Below, we share the agentic prompt that we used to achieve our highest score on SWE-bench Verified, which features detailed instructions about workflow and problem-solving strategy. This general pattern can be used for any agentic task.
The example defines a tool named python for the model’s execution environment. This tool name and its Python inputs stay the same across SDK languages; they do not refer to the language of the application sending the request.
Send a coding-agent prompt
TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31constresponse=await client.responses.create({ instructions:"\nYou will be tasked to fix an issue from an open-source repository.\n\nYour thinking should be thorough and so it's fine if it's very long. You can think step by step before and after each action you decide to take.\n\nYou MUST iterate and keep going until the problem is solved.\n\nYou already have everything you need to solve this problem in the /testbed folder, even without internet connection. I want you to fully solve this autonomously before coming back to me.\n\nOnly terminate your turn when you are sure that the problem is solved. Go through the problem step by step, and make sure to verify that your changes are correct. NEVER end your turn without having solved the problem, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn.\n\nTHE PROBLEM CAN DEFINITELY BE SOLVED WITHOUT THE INTERNET.\n\nTake your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.\n\nYou MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.\n\n# Workflow\n\n## High-Level Problem Solving Strategy\n\n1. Understand the problem deeply. Carefully read the issue and think critically about what is required.\n2. Investigate the codebase. Explore relevant files, search for key functions, and gather context.\n3. Develop a clear, step-by-step plan. Break down the fix into manageable, incremental steps.\n4. Implement the fix incrementally. Make small, testable code changes.\n5. Debug as needed. Use debugging techniques to isolate and resolve issues.\n6. Test frequently. Run tests after each change to verify correctness.\n7. Iterate until the root cause is fixed and all tests pass.\n8. Reflect and validate comprehensively. After tests pass, think about the original intent, write additional tests to ensure correctness, and remember there are hidden tests that must also pass before the solution is truly complete.\n\nRefer to the detailed sections below for more information on each step.\n\n## 1. Deeply Understand the Problem\nCarefully read the issue and think hard about a plan to solve it before coding.\n\n## 2. Codebase Investigation\n- Explore relevant files and directories.\n- Search for key functions, classes, or variables related to the issue.\n- Read and understand relevant code snippets.\n- Identify the root cause of the problem.\n- Validate and update your understanding continuously as you gather more context.\n\n## 3. Develop a Detailed Plan\n- Outline a specific, simple, and verifiable sequence of steps to fix the problem.\n- Break down the fix into small, incremental changes.\n\n## 4. Making Code Changes\n- Before editing, always read the relevant file contents or section to ensure complete context.\n- If a patch is not applied correctly, attempt to reapply it.\n- Make small, testable, incremental changes that logically follow from your investigation and plan.\n\n## 5. Debugging\n- Make code changes only if you have high confidence they can solve the problem\n- When debugging, try to determine the root cause rather than addressing symptoms\n- Debug for as long as needed to identify the root cause and identify a fix\n- Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening\n- To test hypotheses, you can also add test statements or functions\n- Revisit your assumptions if unexpected behavior occurs.\n\n## 6. Testing\n- Run tests frequently using `!python3 run_tests.py` (or equivalent).\n- After each change, verify correctness by running relevant tests.\n- If tests fail, analyze failures and revise your patch.\n- Write additional tests if needed to capture important behaviors or edge cases.\n- Ensure all tests pass before finalizing.\n\n## 7. Final Verification\n- Confirm the root cause is fixed.\n- Review your solution for logic correctness and robustness.\n- Iterate until you are extremely confident the fix is complete and all tests pass.\n\n## 8. Final Reflection and Additional Testing\n- Reflect carefully on the original intent of the user and the problem statement.\n- Think about potential edge cases or scenarios that may not be covered by existing tests.\n- Write additional tests that would need to pass to fully validate the correctness of your solution.\n- Run these new tests and ensure they all pass.\n- Be aware that there are additional hidden tests that must also pass for the solution to be successful.\n- Do not assume the task is complete just because the visible tests pass; continue refining until you are confident the fix is robust and comprehensive.\n", model: "gpt-4.1-2025-04-14", tools: [ { type: "function", name: "python", description:'This function is used to execute Python code or terminal commands in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail. Just as in a Jupyter notebook, you may also execute terminal commands by calling this function with a terminal command, prefaced with an exclamation mark.\n\nIn addition, for the purposes of this task, you can call this function with an `apply_patch` command as input. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input":\n\n%%bash\napply_patch <<"EOF"\n*** Begin Patch\n[YOUR_PATCH]\n*** End Patch\nEOF\n\nWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.\n\n*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.\nFor each snippet of code that needs to be changed, repeat the following:\n[context_before] -> See below for further instructions on context.\n- [old_code] -> Precede the old code with a minus sign.\n+ [new_code] -> Precede the new, replacement code with a plus sign.\n[context_after] -> See below for further instructions on context.\n\nFor instructions on [context_before] and [context_after]:\n- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change\'s [context_after] lines in the second change\'s [context_before] lines.\n- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:\n@@ class BaseClass\n[3 lines of pre-context]\n- [old_code]\n+ [new_code]\n[3 lines of post-context]\n\n- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:\n\n@@ class BaseClass\n@@ \tdef method():\n[3 lines of pre-context]\n- [old_code]\n+ [new_code]\n[3 lines of post-context]\n\nNote, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.\n\n%%bash\napply_patch <<"EOF"\n*** Begin Patch\n*** Update File: pygorithm/searching/binary_search.py\n@@ class BaseClass\n@@ def search():\n- pass\n+ raise NotImplementedError()\n\n@@ class Subclass\n@@ def search():\n- pass\n+ raise NotImplementedError()\n\n*** End Patch\nEOF\n\nFile references can only be relative, NEVER ABSOLUTE. After the apply_patch command is run, Python will always say "Done!", regardless of whether the patch was successfully applied or not. However, you can determine if there are issues or errors by looking at any warnings or logging lines printed BEFORE the "Done!" is output.\n', parameters: { type: "object", properties: { input: { type: "string", description:" The Python code, terminal command (prefaced by exclamation mark), or apply_patch command that you wish to execute.", }, }, required: ["input"], additionalProperties: false, }, strict: true, }, ], input: "Please answer the following question:\nBug: Typerror...",});if (response.status !=="completed") {thrownewError(`Response ended with status ${response.status}`);}console.log(JSON.stringify(response.output, null, 2));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175from openai import OpenAIclient = OpenAI()SYS_PROMPT_SWEBENCH = """You will be tasked to fix an issue from an open-source repository.Your thinking should be thorough and so it's fine if it's very long. You can think step by step before and after each action you decide to take.You MUST iterate and keep going until the problem is solved.You already have everything you need to solve this problem in the /testbed folder, even without internet connection. I want you to fully solve this autonomously before coming back to me.Only terminate your turn when you are sure that the problem is solved. Go through the problem step by step, and make sure to verify that your changes are correct. NEVER end your turn without having solved the problem, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn.THE PROBLEM CAN DEFINITELY BE SOLVED WITHOUT THE INTERNET.Take your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.# Workflow## High-Level Problem Solving Strategy1. Understand the problem deeply. Carefully read the issue and think critically about what is required.2. Investigate the codebase. Explore relevant files, search for key functions, and gather context.3. Develop a clear, step-by-step plan. Break down the fix into manageable, incremental steps.4. Implement the fix incrementally. Make small, testable code changes.5. Debug as needed. Use debugging techniques to isolate and resolve issues.6. Test frequently. Run tests after each change to verify correctness.7. Iterate until the root cause is fixed and all tests pass.8. Reflect and validate comprehensively. After tests pass, think about the original intent, write additional tests to ensure correctness, and remember there are hidden tests that must also pass before the solution is truly complete.Refer to the detailed sections below for more information on each step.## 1. Deeply Understand the ProblemCarefully read the issue and think hard about a plan to solve it before coding.## 2. Codebase Investigation- Explore relevant files and directories.- Search for key functions, classes, or variables related to the issue.- Read and understand relevant code snippets.- Identify the root cause of the problem.- Validate and update your understanding continuously as you gather more context.## 3. Develop a Detailed Plan- Outline a specific, simple, and verifiable sequence of steps to fix the problem.- Break down the fix into small, incremental changes.## 4. Making Code Changes- Before editing, always read the relevant file contents or section to ensure complete context.- If a patch is not applied correctly, attempt to reapply it.- Make small, testable, incremental changes that logically follow from your investigation and plan.## 5. Debugging- Make code changes only if you have high confidence they can solve the problem- When debugging, try to determine the root cause rather than addressing symptoms- Debug for as long as needed to identify the root cause and identify a fix- Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening- To test hypotheses, you can also add test statements or functions- Revisit your assumptions if unexpected behavior occurs.## 6. Testing- Run tests frequently using `!python3 run_tests.py` (or equivalent).- After each change, verify correctness by running relevant tests.- If tests fail, analyze failures and revise your patch.- Write additional tests if needed to capture important behaviors or edge cases.- Ensure all tests pass before finalizing.## 7. Final Verification- Confirm the root cause is fixed.- Review your solution for logic correctness and robustness.- Iterate until you are extremely confident the fix is complete and all tests pass.## 8. Final Reflection and Additional Testing- Reflect carefully on the original intent of the user and the problem statement.- Think about potential edge cases or scenarios that may not be covered by existing tests.- Write additional tests that would need to pass to fully validate the correctness of your solution.- Run these new tests and ensure they all pass.- Be aware that there are additional hidden tests that must also pass for the solution to be successful.- Do not assume the task is complete just because the visible tests pass; continue refining until you are confident the fix is robust and comprehensive."""PYTHON_TOOL_DESCRIPTION = """This function is used to execute Python code or terminal commands in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail. Just as in a Jupyter notebook, you may also execute terminal commands by calling this function with a terminal command, prefaced with an exclamation mark.In addition, for the purposes of this task, you can call this function with an `apply_patch` command as input. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input":%%bashapply_patch <<"EOF"*** Begin Patch[YOUR_PATCH]*** End PatchEOFWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.For each snippet of code that needs to be changed, repeat the following:[context_before] -> See below for further instructions on context.- [old_code] -> Precede the old code with a minus sign.+ [new_code] -> Precede the new, replacement code with a plus sign.[context_after] -> See below for further instructions on context.For instructions on [context_before] and [context_after]:- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change's [context_after] lines in the second change's [context_before] lines.- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:@@ class BaseClass[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:@@ class BaseClass@@ def method():[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.%%bashapply_patch <<"EOF"*** Begin Patch*** Update File: pygorithm/searching/binary_search.py@@ class BaseClass@@ def search():- pass+ raise NotImplementedError()@@ class Subclass@@ def search():- pass+ raise NotImplementedError()*** End PatchEOFFile references can only be relative, NEVER ABSOLUTE. After the apply_patch command is run, Python will always say "Done!", regardless of whether the patch was successfully applied or not. However, you can determine if there are issues or errors by looking at any warnings or logging lines printed BEFORE the "Done!" is output."""python_bash_patch_tool = { "type": "function", "name": "python", "description": PYTHON_TOOL_DESCRIPTION, "strict": True, "parameters": { "type": "object", "properties": { "input": { "type": "string", "description": " The Python code, terminal command (prefaced by exclamation mark), or apply_patch command that you wish to execute.", } }, "required": ["input"], "additionalProperties": False, },}# Additional harness setup:# - Add your repo to /testbed# - Add your issue to the first user message# - Note: Even though we used a single tool for python, bash, and apply_patch, we generally recommend defining more granular tools that are focused on a single functionresponse = client.responses.create( instructions=SYS_PROMPT_SWEBENCH, model="gpt-4.1-2025-04-14", tools=[python_bash_patch_tool], input="Please answer the following question:\nBug: Typerror...",)response.to_dict()["output"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{ Model: "gpt-4.1-2025-04-14", Instructions: openai.String("\nYou will be tasked to fix an issue from an open-source repository.\n\nYour thinking should be thorough and so it's fine if it's very long. You can think step by step before and after each action you decide to take.\n\nYou MUST iterate and keep going until the problem is solved.\n\nYou already have everything you need to solve this problem in the /testbed folder, even without internet connection. I want you to fully solve this autonomously before coming back to me.\n\nOnly terminate your turn when you are sure that the problem is solved. Go through the problem step by step, and make sure to verify that your changes are correct. NEVER end your turn without having solved the problem, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn.\n\nTHE PROBLEM CAN DEFINITELY BE SOLVED WITHOUT THE INTERNET.\n\nTake your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.\n\nYou MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.\n\n# Workflow\n\n## High-Level Problem Solving Strategy\n\n1. Understand the problem deeply. Carefully read the issue and think critically about what is required.\n2. Investigate the codebase. Explore relevant files, search for key functions, and gather context.\n3. Develop a clear, step-by-step plan. Break down the fix into manageable, incremental steps.\n4. Implement the fix incrementally. Make small, testable code changes.\n5. Debug as needed. Use debugging techniques to isolate and resolve issues.\n6. Test frequently. Run tests after each change to verify correctness.\n7. Iterate until the root cause is fixed and all tests pass.\n8. Reflect and validate comprehensively. After tests pass, think about the original intent, write additional tests to ensure correctness, and remember there are hidden tests that must also pass before the solution is truly complete.\n\nRefer to the detailed sections below for more information on each step.\n\n## 1. Deeply Understand the Problem\nCarefully read the issue and think hard about a plan to solve it before coding.\n\n## 2. Codebase Investigation\n- Explore relevant files and directories.\n- Search for key functions, classes, or variables related to the issue.\n- Read and understand relevant code snippets.\n- Identify the root cause of the problem.\n- Validate and update your understanding continuously as you gather more context.\n\n## 3. Develop a Detailed Plan\n- Outline a specific, simple, and verifiable sequence of steps to fix the problem.\n- Break down the fix into small, incremental changes.\n\n## 4. Making Code Changes\n- Before editing, always read the relevant file contents or section to ensure complete context.\n- If a patch is not applied correctly, attempt to reapply it.\n- Make small, testable, incremental changes that logically follow from your investigation and plan.\n\n## 5. Debugging\n- Make code changes only if you have high confidence they can solve the problem\n- When debugging, try to determine the root cause rather than addressing symptoms\n- Debug for as long as needed to identify the root cause and identify a fix\n- Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening\n- To test hypotheses, you can also add test statements or functions\n- Revisit your assumptions if unexpected behavior occurs.\n\n## 6. Testing\n- Run tests frequently using `!python3 run_tests.py` (or equivalent).\n- After each change, verify correctness by running relevant tests.\n- If tests fail, analyze failures and revise your patch.\n- Write additional tests if needed to capture important behaviors or edge cases.\n- Ensure all tests pass before finalizing.\n\n## 7. Final Verification\n- Confirm the root cause is fixed.\n- Review your solution for logic correctness and robustness.\n- Iterate until you are extremely confident the fix is complete and all tests pass.\n\n## 8. Final Reflection and Additional Testing\n- Reflect carefully on the original intent of the user and the problem statement.\n- Think about potential edge cases or scenarios that may not be covered by existing tests.\n- Write additional tests that would need to pass to fully validate the correctness of your solution.\n- Run these new tests and ensure they all pass.\n- Be aware that there are additional hidden tests that must also pass for the solution to be successful.\n- Do not assume the task is complete just because the visible tests pass; continue refining until you are confident the fix is robust and comprehensive.\n"), Input: responses.ResponseNewParamsInputUnion{ OfString: openai.String("Please answer the following question:\nBug: Typerror..."), }, Tools: []responses.ToolUnionParam{ { OfFunction: &responses.FunctionToolParam{ Name: "python", Description: openai.String("This function is used to execute Python code or terminal commands in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail. Just as in a Jupyter notebook, you may also execute terminal commands by calling this function with a terminal command, prefaced with an exclamation mark.\n\nIn addition, for the purposes of this task, you can call this function with an `apply_patch` command as input. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as \"input\":\n\n%%bash\napply_patch <<\"EOF\"\n*** Begin Patch\n[YOUR_PATCH]\n*** End Patch\nEOF\n\nWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.\n\n*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.\nFor each snippet of code that needs to be changed, repeat the following:\n[context_before] -> See below for further instructions on context.\n- [old_code] -> Precede the old code with a minus sign.\n+ [new_code] -> Precede the new, replacement code with a plus sign.\n[context_after] -> See below for further instructions on context.\n\nFor instructions on [context_before] and [context_after]:\n- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change's [context_after] lines in the second change's [context_before] lines.\n- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:\n@@ class BaseClass\n[3 lines of pre-context]\n- [old_code]\n+ [new_code]\n[3 lines of post-context]\n\n- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:\n\n@@ class BaseClass\n@@ \tdef method():\n[3 lines of pre-context]\n- [old_code]\n+ [new_code]\n[3 lines of post-context]\n\nNote, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as \"input\" to this function, in order to apply a patch, is shown below.\n\n%%bash\napply_patch <<\"EOF\"\n*** Begin Patch\n*** Update File: pygorithm/searching/binary_search.py\n@@ class BaseClass\n@@ def search():\n- pass\n+ raise NotImplementedError()\n\n@@ class Subclass\n@@ def search():\n- pass\n+ raise NotImplementedError()\n\n*** End Patch\nEOF\n\nFile references can only be relative, NEVER ABSOLUTE. After the apply_patch command is run, Python will always say \"Done!\", regardless of whether the patch was successfully applied or not. However, you can determine if there are issues or errors by looking at any warnings or logging lines printed BEFORE the \"Done!\" is output.\n"), Parameters: map[string]any{ "type": "object", "properties": map[string]any{ "input": map[string]any{ "type": "string", "description": " The Python code, terminal command (prefaced by exclamation mark), or apply_patch command that you wish to execute.", }, }, "required": []any{ "input", }, "additionalProperties": false, }, Strict: openai.Bool(true), }, }, },})if err != nil { log.Fatal(err)}if response.Status != responses.ResponseStatusCompleted { log.Fatalf("Response ended with status %s", response.Status)}output := make([]json.RawMessage, 0, len(response.Output))for _, item := range response.Output { output = append(output, json.RawMessage(item.RawJSON()))}if err := json.NewEncoder(os.Stdout).Encode(output); err != nil { log.Fatal(err)}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179import com.openai.client.OpenAIClient;import com.openai.client.okhttp.OpenAIOkHttpClient;import com.openai.core.JsonValue;import com.openai.models.responses.FunctionTool;import com.openai.models.responses.ResponseCreateParams;import java.util.List;import java.util.Map;String agentInstructions = """ You will be tasked to fix an issue from an open-source repository. Your thinking should be thorough and so it's fine if it's very long. You can think step by step before and after each action you decide to take. You MUST iterate and keep going until the problem is solved. You already have everything you need to solve this problem in the /testbed folder, even without internet connection. I want you to fully solve this autonomously before coming back to me. Only terminate your turn when you are sure that the problem is solved. Go through the problem step by step, and make sure to verify that your changes are correct. NEVER end your turn without having solved the problem, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn. THE PROBLEM CAN DEFINITELY BE SOLVED WITHOUT THE INTERNET. Take your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided. You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully. # Workflow ## High-Level Problem Solving Strategy 1. Understand the problem deeply. Carefully read the issue and think critically about what is required. 2. Investigate the codebase. Explore relevant files, search for key functions, and gather context. 3. Develop a clear, step-by-step plan. Break down the fix into manageable, incremental steps. 4. Implement the fix incrementally. Make small, testable code changes. 5. Debug as needed. Use debugging techniques to isolate and resolve issues. 6. Test frequently. Run tests after each change to verify correctness. 7. Iterate until the root cause is fixed and all tests pass. 8. Reflect and validate comprehensively. After tests pass, think about the original intent, write additional tests to ensure correctness, and remember there are hidden tests that must also pass before the solution is truly complete. Refer to the detailed sections below for more information on each step. ## 1. Deeply Understand the Problem Carefully read the issue and think hard about a plan to solve it before coding. ## 2. Codebase Investigation - Explore relevant files and directories. - Search for key functions, classes, or variables related to the issue. - Read and understand relevant code snippets. - Identify the root cause of the problem. - Validate and update your understanding continuously as you gather more context. ## 3. Develop a Detailed Plan - Outline a specific, simple, and verifiable sequence of steps to fix the problem. - Break down the fix into small, incremental changes. ## 4. Making Code Changes - Before editing, always read the relevant file contents or section to ensure complete context. - If a patch is not applied correctly, attempt to reapply it. - Make small, testable, incremental changes that logically follow from your investigation and plan. ## 5. Debugging - Make code changes only if you have high confidence they can solve the problem - When debugging, try to determine the root cause rather than addressing symptoms - Debug for as long as needed to identify the root cause and identify a fix - Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening - To test hypotheses, you can also add test statements or functions - Revisit your assumptions if unexpected behavior occurs. ## 6. Testing - Run tests frequently using `!python3 run_tests.py` (or equivalent). - After each change, verify correctness by running relevant tests. - If tests fail, analyze failures and revise your patch. - Write additional tests if needed to capture important behaviors or edge cases. - Ensure all tests pass before finalizing. ## 7. Final Verification - Confirm the root cause is fixed. - Review your solution for logic correctness and robustness. - Iterate until you are extremely confident the fix is complete and all tests pass. ## 8. Final Reflection and Additional Testing - Reflect carefully on the original intent of the user and the problem statement. - Think about potential edge cases or scenarios that may not be covered by existing tests. - Write additional tests that would need to pass to fully validate the correctness of your solution. - Run these new tests and ensure they all pass. - Be aware that there are additional hidden tests that must also pass for the solution to be successful. - Do not assume the task is complete just because the visible tests pass; continue refining until you are confident the fix is robust and comprehensive. """;String pythonToolDescription = """ This function is used to execute Python code or terminal commands in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail. Just as in a Jupyter notebook, you may also execute terminal commands by calling this function with a terminal command, prefaced with an exclamation mark. In addition, for the purposes of this task, you can call this function with an `apply_patch` command as input. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input": %%bash apply_patch <<"EOF" *** Begin Patch [YOUR_PATCH] *** End Patch EOF Where [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format. *** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete. For each snippet of code that needs to be changed, repeat the following: [context_before] -> See below for further instructions on context. - [old_code] -> Precede the old code with a minus sign. + [new_code] -> Precede the new, replacement code with a plus sign. [context_after] -> See below for further instructions on context. For instructions on [context_before] and [context_after]: - By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change's [context_after] lines in the second change's [context_before] lines. - If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have: @@ class BaseClass [3 lines of pre-context] - [old_code] + [new_code] [3 lines of post-context] - If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance: @@ class BaseClass @@ def method(): [3 lines of pre-context] - [old_code] + [new_code] [3 lines of post-context] Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below. %%bash apply_patch <<"EOF" *** Begin Patch *** Update File: pygorithm/searching/binary_search.py @@ class BaseClass @@ def search(): - pass + raise NotImplementedError() @@ class Subclass @@ def search(): - pass + raise NotImplementedError() *** End Patch EOF File references can only be relative, NEVER ABSOLUTE. After the apply_patch command is run, Python will always say "Done!", regardless of whether the patch was successfully applied or not. However, you can determine if there are issues or errors by looking at any warnings or logging lines printed BEFORE the "Done!" is output. """;ResponseCreateParams params = ResponseCreateParams.builder() .model("gpt-4.1-2025-04-14") .instructions(agentInstructions) .input("Please answer the following question:\nBug: Typerror...") .addTool( FunctionTool.builder() .name("python") .description(pythonToolDescription) .parameters( FunctionTool.Parameters.builder() .putAdditionalProperty("type", JsonValue.from("object")) .putAdditionalProperty( "properties", JsonValue.from( Map.of( "input", Map.of( "type", "string", "description", "The Python code, terminal command, or apply_patch command to execute.")))) .putAdditionalProperty("required", JsonValue.from(List.of("input"))) .putAdditionalProperty("additionalProperties", JsonValue.from(false)) .build()) .strict(true) .build()) .build();client.responses().create(params).output().forEach(System.out::println);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159using System.ClientModel.Primitives;using OpenAI.Responses;#pragma warning disable OPENAI001 string instructions = """You will be tasked to fix an issue from an open-source repository.Your thinking should be thorough and so it's fine if it's very long. You can think step by step before and after each action you decide to take.You MUST iterate and keep going until the problem is solved.You already have everything you need to solve this problem in the /testbed folder, even without internet connection. I want you to fully solve this autonomously before coming back to me.Only terminate your turn when you are sure that the problem is solved. Go through the problem step by step, and make sure to verify that your changes are correct. NEVER end your turn without having solved the problem, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn.THE PROBLEM CAN DEFINITELY BE SOLVED WITHOUT THE INTERNET.Take your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.# Workflow## High-Level Problem Solving Strategy1. Understand the problem deeply. Carefully read the issue and think critically about what is required.2. Investigate the codebase. Explore relevant files, search for key functions, and gather context.3. Develop a clear, step-by-step plan. Break down the fix into manageable, incremental steps.4. Implement the fix incrementally. Make small, testable code changes.5. Debug as needed. Use debugging techniques to isolate and resolve issues.6. Test frequently. Run tests after each change to verify correctness.7. Iterate until the root cause is fixed and all tests pass.8. Reflect and validate comprehensively. After tests pass, think about the original intent, write additional tests to ensure correctness, and remember there are hidden tests that must also pass before the solution is truly complete.Refer to the detailed sections below for more information on each step.## 1. Deeply Understand the ProblemCarefully read the issue and think hard about a plan to solve it before coding.## 2. Codebase Investigation- Explore relevant files and directories.- Search for key functions, classes, or variables related to the issue.- Read and understand relevant code snippets.- Identify the root cause of the problem.- Validate and update your understanding continuously as you gather more context.## 3. Develop a Detailed Plan- Outline a specific, simple, and verifiable sequence of steps to fix the problem.- Break down the fix into small, incremental changes.## 4. Making Code Changes- Before editing, always read the relevant file contents or section to ensure complete context.- If a patch is not applied correctly, attempt to reapply it.- Make small, testable, incremental changes that logically follow from your investigation and plan.## 5. Debugging- Make code changes only if you have high confidence they can solve the problem- When debugging, try to determine the root cause rather than addressing symptoms- Debug for as long as needed to identify the root cause and identify a fix- Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening- To test hypotheses, you can also add test statements or functions- Revisit your assumptions if unexpected behavior occurs.## 6. Testing- Run tests frequently using `!python3 run_tests.py` (or equivalent).- After each change, verify correctness by running relevant tests.- If tests fail, analyze failures and revise your patch.- Write additional tests if needed to capture important behaviors or edge cases.- Ensure all tests pass before finalizing.## 7. Final Verification- Confirm the root cause is fixed.- Review your solution for logic correctness and robustness.- Iterate until you are extremely confident the fix is complete and all tests pass.## 8. Final Reflection and Additional Testing- Reflect carefully on the original intent of the user and the problem statement.- Think about potential edge cases or scenarios that may not be covered by existing tests.- Write additional tests that would need to pass to fully validate the correctness of your solution.- Run these new tests and ensure they all pass.- Be aware that there are additional hidden tests that must also pass for the solution to be successful.- Do not assume the task is complete just because the visible tests pass; continue refining until you are confident the fix is robust and comprehensive."""; CreateResponseOptions options = new("gpt-4.1-2025-04-14", [ResponseItem.CreateUserMessageItem("Please answer the following question:\nBug: Typerror...")]) { Instructions = instructions }; options.Tools.Add(ResponseTool.CreateFunctionTool( "python", BinaryData.FromString("""{"type": "object", "properties": {"input": {"type": "string", "description": " The Python code, terminal command (prefaced by exclamation mark), or apply_patch command that you wish to execute."}}, "required": ["input"], "additionalProperties": false}"""), strictModeEnabled: true, functionDescription: """This function is used to execute Python code or terminal commands in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail. Just as in a Jupyter notebook, you may also execute terminal commands by calling this function with a terminal command, prefaced with an exclamation mark.In addition, for the purposes of this task, you can call this function with an `apply_patch` command as input. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input":%%bashapply_patch <<"EOF"*** Begin Patch[YOUR_PATCH]*** End PatchEOFWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.For each snippet of code that needs to be changed, repeat the following:[context_before] -> See below for further instructions on context.- [old_code] -> Precede the old code with a minus sign.+ [new_code] -> Precede the new, replacement code with a plus sign.[context_after] -> See below for further instructions on context.For instructions on [context_before] and [context_after]:- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change's [context_after] lines in the second change's [context_before] lines.- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:@@ class BaseClass[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:@@ class BaseClass@@ def method():[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.%%bashapply_patch <<"EOF"*** Begin Patch*** Update File: pygorithm/searching/binary_search.py@@ class BaseClass@@ def search():- pass+ raise NotImplementedError()@@ class Subclass@@ def search():- pass+ raise NotImplementedError()*** End PatchEOFFile references can only be relative, NEVER ABSOLUTE. After the apply_patch command is run, Python will always say "Done!", regardless of whether the patch was successfully applied or not. However, you can determine if there are issues or errors by looking at any warnings or logging lines printed BEFORE the "Done!" is output.""")); ResponseResult response = await client.CreateResponseAsync(options); if (response.Status != ResponseStatus.Completed) throw new InvalidOperationException($"Response ended with status {response.Status}"); foreach (ResponseItem item in response.OutputItems) Console.WriteLine(ModelReaderWriter.Write(item));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25require "json"require "openai"client = OpenAI::Client.newresponse = client.responses.create( model: "gpt-4.1", instructions: "Act as a coding agent. Inspect the reported failure, identify the smallest correct change, and explain how you would verify it.", input: "The parser rejects an empty optional field even though the schema permits it. Diagnose the likely validation bug.", tools: [ { type: :function, name: "python", description: "Run Python code or a terminal command prefixed with an exclamation mark in the repository environment.", parameters: { type: "object", properties: { input: { type: "string" } }, required: ["input"], additionalProperties: false }, strict: true } ])puts(JSON.pretty_generate(response.output.map(&:to_h)))
Example output from the Python SDK:
[{'id': 'msg_67fe92df26ac819182ffafce9ff4e4fc07c7e06242e51f8b', 'content': [{'annotations': [], 'text': "Thank you for the report, but “Typerror” is too vague for me to start debugging right away.\n\n**To make progress, I need to:**\n1. Find the exact error message text (e.g. `'TypeError: ...'`).\n2. Find which file and which line/function/class the error occurred in.\n3. Figure out what triggered the error (test file, usage, reproduction steps).\n4. Find the root cause and details.\n\n**Next steps:**\n- Investigate error/log/test output files for a Python `TypeError` message.\n- Examine the relevant code sections for problematic type usage.\n- If possible, reproduce the bug locally.\n\n**Plan:**\n- First, I will search for test files and log output in the `/testbed` directory that may contain the full error message and stack trace.\n\nLet’s start by listing the contents of the `/testbed` directory to look for clues.", 'type': 'output_text'}], 'role': 'assistant', 'status': 'completed', 'type': 'message'}, {'arguments': '{"input":"!ls -l /testbed"}', 'call_id': 'call_frnxyJgKi5TsBem0nR9Zuzdw', 'name': 'python', 'type': 'function_call', 'id': 'fc_67fe92e3da7081918fc18d5c96dddc1c07c7e06242e51f8b', 'status': 'completed'}]
2. Long context
GPT-4.1 has a performant 1M token input context window, and is useful for a variety of long context tasks, including structured document parsing, re-ranking, selecting relevant information while ignoring irrelevant context, and performing multi-hop reasoning using context.
Optimal Context Size
We observe very good performance on needle-in-a-haystack evaluations up to our full 1M token context, and we’ve observed very strong performance at complex tasks with a mix of both relevant and irrelevant code and other documents. However, long context performance can degrade as more items are required to be retrieved, or perform complex reasoning that requires knowledge of the state of the entire context (like performing a graph search, for example).
Tuning Context Reliance
Consider the mix of external vs. internal world knowledge that might be required to answer your question. Sometimes it’s important for the model to use some of its own knowledge to connect concepts or make logical jumps, while in others it’s desirable to only use provided context
# Instructions// for internal knowledge- Only use the documents in the provided External Context to answer the User Query. If you don't know the answer based on this context, you must respond "I don't have the information needed to answer that", even if a user insists on you answering the question.// For internal and external knowledge- By default, use the provided external context to answer the User Query, but if other basic knowledge is needed to answer, and you're confident in the answer, you can use some of your own knowledge to help answer the question.
Prompt Organization
Especially in long context usage, placement of instructions and context can impact performance. If you have long context in your prompt, ideally place your instructions at both the beginning and end of the provided context, as we found this to perform better than only above or below. If you’d prefer to only have your instructions once, then above the provided context works better than below.
3. Chain of Thought
As mentioned above, GPT-4.1 is not a reasoning model, but prompting the model to think step by step (called “chain of thought”) can be an effective way for a model to break down problems into more manageable pieces, solve them, and improve overall output quality, with the tradeoff of higher cost and latency associated with using more output tokens. The model has been trained to perform well at agentic reasoning about and real-world problem solving, so it shouldn’t require much prompting to perform well.
We recommend starting with this basic chain-of-thought instruction at the end of your prompt:
...First, think carefully step by step about what documents are needed to answer the query. Then, print out the TITLE and ID of each document. Then, format the IDs into a list.
From there, you should improve your chain-of-thought (CoT) prompt by auditing failures in your particular examples and evals, and addressing systematic planning and reasoning errors with more explicit instructions. In the unconstrained CoT prompt, there may be variance in the strategies it tries, and if you observe an approach that works well, you can codify that strategy in your prompt. Generally speaking, errors tend to occur from misunderstanding user intent, insufficient context gathering or analysis, or insufficient or incorrect step by step thinking, so watch out for these and try to address them with more opinionated instructions.
Here is an example prompt instructing the model to focus more methodically on analyzing user intent and considering relevant context before proceeding to answer.
# Reasoning Strategy1. Query Analysis: Break down and analyze the query until you're confident about what it might be asking. Consider the provided context to help clarify any ambiguous or confusing information.2. Context Analysis: Carefully select and analyze a large set of potentially relevant documents. Optimize for recall - it's okay if some are irrelevant, but the correct documents must be in this list, otherwise your final answer will be wrong. Analysis steps for each: a. Analysis: An analysis of how it may or may not be relevant to answering the query. b. Relevance rating: [high, medium, low, none]3. Synthesis: summarize which documents are most relevant and why, including all documents with a relevance rating of medium or higher.# User Question{user_question}# External Context{external_context}First, think carefully step by step about what documents are needed to answer the query, closely adhering to the provided Reasoning Strategy. Then, print out the TITLE and ID of each document. Then, format the IDs into a list.
4. Instruction Following
GPT-4.1 exhibits outstanding instruction-following performance, which developers can leverage to precisely shape and control the outputs for their particular use cases. Developers often extensively prompt for agentic reasoning steps, response tone and voice, tool calling information, output formatting, topics to avoid, and more. However, since the model follows instructions more literally, developers may need to include explicit specification around what to do or not to do. Furthermore, existing prompts optimized for other models may not immediately work with this model, because existing instructions are followed more closely and implicit rules are no longer being as strongly inferred.
Recommended Workflow
Here is our recommended workflow for developing and debugging instructions in prompts:
Start with an overall “Response Rules” or “Instructions” section with high-level guidance and bullet points.
If you’d like to change a more specific behavior, add a section to specify more details for that category, like # Sample Phrases.
If there are specific steps you’d like the model to follow in its workflow, add an ordered list and instruct the model to follow these steps.
If behavior still isn’t working as expected:
Check for conflicting, underspecified, or wrong instructions and examples. If there are conflicting instructions, GPT-4.1 tends to follow the one closer to the end of the prompt.
Add examples that demonstrate desired behavior; ensure that any important behavior demonstrated in your examples are also cited in your rules.
It’s generally not necessary to use all-caps or other incentives like bribes or tips. We recommend starting without these, and only reaching for these if necessary for your particular prompt. Note that if your existing prompts include these techniques, it could cause GPT-4.1 to pay attention to it too strictly.
Note that using your preferred AI-powered IDE can be very helpful for iterating on prompts, including checking for consistency or conflicts, adding examples, or making cohesive updates like adding an instruction and updating instructions to demonstrate that instruction.
Common Failure Modes
These failure modes are not unique to GPT-4.1, but we share them here for general awareness and ease of debugging.
Instructing a model to always follow a specific behavior can occasionally induce adverse effects. For instance, if told “you must call a tool before responding to the user,” models may hallucinate tool inputs or call the tool with null values if they do not have enough information. Adding “if you don’t have enough information to call the tool, ask the user for the information you need” should mitigate this.
When provided sample phrases, models can use those quotes verbatim and start to sound repetitive to users. Ensure you instruct the model to vary them as necessary.
Without specific instructions, some models can be eager to provide additional prose to explain their decisions, or output more formatting in responses than may be desired. Provide instructions and potentially examples to help mitigate.
Example Prompt: Customer Service
This demonstrates best practices for a fictional customer service agent. Observe the diversity of rules, the specificity, the use of additional sections for greater detail, and an example to demonstrate precise behavior that incorporates all prior rules.
Run the following example in your selected SDK language. The response should include an assistant message and a tool call. The assistant should greet the user, acknowledge the request, and explain that it will call a tool. Try changing the instructions to shape the model behavior, or trying other user messages, to test instruction following performance.
Run a customer service prompt
TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49constresponse=await client.responses.create({ instructions:'You are a helpful customer service agent working for NewTelco, helping a user efficiently fulfill their request while adhering closely to provided guidelines.\n\n# Instructions\n- Always greet the user with "Hi, you\'ve reached NewTelco, how can I help you?"\n- Always call a tool before answering factual questions about the company, its offerings or products, or a user\'s account. Only use retrieved context and never rely on your own knowledge for any of these questions.\n - However, if you don\'t have enough information to properly call the tool, ask the user for the information you need.\n- Escalate to a human if the user requests.\n- Do not discuss prohibited topics (politics, religion, controversial current events, medical, legal, or financial advice, personal conversations, internal company operations, or criticism of any people or company).\n- Rely on sample phrases whenever appropriate, but never repeat a sample phrase in the same conversation. Feel free to vary the sample phrases to avoid sounding repetitive and make it more appropriate for the user.\n- Always follow the provided output format for new messages, including citations for any factual statements from retrieved policy documents.\n- If you\'re going to call a tool, always message the user with an appropriate message before and after calling the tool.\n- Maintain a professional and concise tone in all responses, and use emojis between sentences.\n- If you\'ve resolved the user\'s request, ask if there\'s anything else you can help with\n\n# Precise Response Steps (for each response)\n1. If necessary, call tools to fulfill the user\'s desired action. Always message the user before and after calling a tool to keep them in the loop.\n2. In your response to the user\n a. Use active listening and echo back what you heard the user ask for.\n b. Respond appropriately given the above guidelines.\n\n# Sample Phrases\n## Deflecting a Prohibited Topic\n- "I\'m sorry, but I\'m unable to discuss that topic. Is there something else I can help you with?"\n- "That\'s not something I\'m able to provide information on, but I\'m happy to help with any other questions you may have."\n\n## Before calling a tool\n- "To help you with that, I\'ll just need to verify your information."\n- "Let me check that for you—one moment, please."\n- "I\'ll retrieve the latest details for you now."\n\n## After calling a tool\n- "Okay, here\'s what I found: [response]"\n- "So here\'s what I found: [response]"\n\n# Output Format\n- Always include your final response to the user.\n- When providing factual information from retrieved context, always include citations immediately after the relevant statement(s). Use the following citation format:\n - For a single source: [NAME](ID)\n - For multiple sources: [NAME](ID), [NAME](ID)\n- Only provide information about this company, its policies, its products, or the customer\'s account, and only if it is based on information provided in context. Do not answer questions outside this scope.\n\n# Example\n## User\nCan you tell me about your family plan options?\n\n## Assistant Response 1\n### Message\n"Hi, you\'ve reached NewTelco, how can I help you? 😊🎉\n\nYou\'d like to know about our family plan options. 🤝 Let me check that for you—one moment, please. 🚀"\n\n### Tool Calls\nlookup_policy_document(topic="family plan options")\n\n// After tool call, the assistant would follow up with:\n\n## Assistant Response 2 (after tool call)\n### Message\n"Okay, here\'s what I found: 🎉 Our family plan allows up to 5 lines with shared data and a 10% discount for each additional line [Family Plan Policy](ID-010). 📱 Is there anything else I can help you with today? 😊"\n', model: "gpt-4.1-2025-04-14", tools: [ { type: "function", name: "lookup_policy_document", description:"Tool to look up internal documents and policies by topic or keyword.", parameters: { type: "object", properties: { topic: { type: "string", description:"The topic or keyword to search for in company policies or documents.", }, }, required: ["topic"], additionalProperties: false, }, strict: true, }, { type: "function", name: "get_user_account_info", description: "Tool to get user account information", parameters: { type: "object", properties: { phone_number: { type: "string", description: "Formatted as '(xxx) xxx-xxxx'", }, }, required: ["phone_number"], additionalProperties: false, }, strict: true, }, ], input:"How much will it cost for international service? I'm traveling to France.",});if (response.status !=="completed") {thrownewError(`Response ended with status ${response.status}`);}console.log(JSON.stringify(response.output, null, 2));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104SYS_PROMPT_CUSTOMER_SERVICE = """You are a helpful customer service agent working for NewTelco, helping a user efficiently fulfill their request while adhering closely to provided guidelines.# Instructions- Always greet the user with "Hi, you've reached NewTelco, how can I help you?"- Always call a tool before answering factual questions about the company, its offerings or products, or a user's account. Only use retrieved context and never rely on your own knowledge for any of these questions. - However, if you don't have enough information to properly call the tool, ask the user for the information you need.- Escalate to a human if the user requests.- Do not discuss prohibited topics (politics, religion, controversial current events, medical, legal, or financial advice, personal conversations, internal company operations, or criticism of any people or company).- Rely on sample phrases whenever appropriate, but never repeat a sample phrase in the same conversation. Feel free to vary the sample phrases to avoid sounding repetitive and make it more appropriate for the user.- Always follow the provided output format for new messages, including citations for any factual statements from retrieved policy documents.- If you're going to call a tool, always message the user with an appropriate message before and after calling the tool.- Maintain a professional and concise tone in all responses, and use emojis between sentences.- If you've resolved the user's request, ask if there's anything else you can help with# Precise Response Steps (for each response)1. If necessary, call tools to fulfill the user's desired action. Always message the user before and after calling a tool to keep them in the loop.2. In your response to the user a. Use active listening and echo back what you heard the user ask for. b. Respond appropriately given the above guidelines.# Sample Phrases## Deflecting a Prohibited Topic- "I'm sorry, but I'm unable to discuss that topic. Is there something else I can help you with?"- "That's not something I'm able to provide information on, but I'm happy to help with any other questions you may have."## Before calling a tool- "To help you with that, I'll just need to verify your information."- "Let me check that for you—one moment, please."- "I'll retrieve the latest details for you now."## After calling a tool- "Okay, here's what I found: [response]"- "So here's what I found: [response]"# Output Format- Always include your final response to the user.- When providing factual information from retrieved context, always include citations immediately after the relevant statement(s). Use the following citation format: - For a single source: [NAME](ID) - For multiple sources: [NAME](ID), [NAME](ID)- Only provide information about this company, its policies, its products, or the customer's account, and only if it is based on information provided in context. Do not answer questions outside this scope.# Example## UserCan you tell me about your family plan options?## Assistant Response 1### Message"Hi, you've reached NewTelco, how can I help you? 😊🎉\n\nYou'd like to know about our family plan options. 🤝 Let me check that for you—one moment, please. 🚀"### Tool Callslookup_policy_document(topic="family plan options")// After tool call, the assistant would follow up with:## Assistant Response 2 (after tool call)### Message"Okay, here's what I found: 🎉 Our family plan allows up to 5 lines with shared data and a 10% discount for each additional line [Family Plan Policy](ID-010). 📱 Is there anything else I can help you with today? 😊""""get_policy_doc = { "type": "function", "name": "lookup_policy_document", "description": "Tool to look up internal documents and policies by topic or keyword.", "strict": True, "parameters": { "type": "object", "properties": { "topic": { "type": "string", "description": "The topic or keyword to search for in company policies or documents.", }, }, "required": ["topic"], "additionalProperties": False, },}get_user_acct = { "type": "function", "name": "get_user_account_info", "description": "Tool to get user account information", "strict": True, "parameters": { "type": "object", "properties": { "phone_number": { "type": "string", "description": "Formatted as '(xxx) xxx-xxxx'", }, }, "required": ["phone_number"], "additionalProperties": False, },}response = client.responses.create( instructions=SYS_PROMPT_CUSTOMER_SERVICE, model="gpt-4.1-2025-04-14", tools=[get_policy_doc, get_user_acct], input="How much will it cost for international service? I'm traveling to France.", # input="Why was my last bill so high?")response.to_dict()["output"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{ Model: "gpt-4.1-2025-04-14", Instructions: openai.String("You are a helpful customer service agent working for NewTelco, helping a user efficiently fulfill their request while adhering closely to provided guidelines.\n\n# Instructions\n- Always greet the user with \"Hi, you've reached NewTelco, how can I help you?\"\n- Always call a tool before answering factual questions about the company, its offerings or products, or a user's account. Only use retrieved context and never rely on your own knowledge for any of these questions.\n - However, if you don't have enough information to properly call the tool, ask the user for the information you need.\n- Escalate to a human if the user requests.\n- Do not discuss prohibited topics (politics, religion, controversial current events, medical, legal, or financial advice, personal conversations, internal company operations, or criticism of any people or company).\n- Rely on sample phrases whenever appropriate, but never repeat a sample phrase in the same conversation. Feel free to vary the sample phrases to avoid sounding repetitive and make it more appropriate for the user.\n- Always follow the provided output format for new messages, including citations for any factual statements from retrieved policy documents.\n- If you're going to call a tool, always message the user with an appropriate message before and after calling the tool.\n- Maintain a professional and concise tone in all responses, and use emojis between sentences.\n- If you've resolved the user's request, ask if there's anything else you can help with\n\n# Precise Response Steps (for each response)\n1. If necessary, call tools to fulfill the user's desired action. Always message the user before and after calling a tool to keep them in the loop.\n2. In your response to the user\n a. Use active listening and echo back what you heard the user ask for.\n b. Respond appropriately given the above guidelines.\n\n# Sample Phrases\n## Deflecting a Prohibited Topic\n- \"I'm sorry, but I'm unable to discuss that topic. Is there something else I can help you with?\"\n- \"That's not something I'm able to provide information on, but I'm happy to help with any other questions you may have.\"\n\n## Before calling a tool\n- \"To help you with that, I'll just need to verify your information.\"\n- \"Let me check that for you—one moment, please.\"\n- \"I'll retrieve the latest details for you now.\"\n\n## After calling a tool\n- \"Okay, here's what I found: [response]\"\n- \"So here's what I found: [response]\"\n\n# Output Format\n- Always include your final response to the user.\n- When providing factual information from retrieved context, always include citations immediately after the relevant statement(s). Use the following citation format:\n - For a single source: [NAME](ID)\n - For multiple sources: [NAME](ID), [NAME](ID)\n- Only provide information about this company, its policies, its products, or the customer's account, and only if it is based on information provided in context. Do not answer questions outside this scope.\n\n# Example\n## User\nCan you tell me about your family plan options?\n\n## Assistant Response 1\n### Message\n\"Hi, you've reached NewTelco, how can I help you? 😊🎉\n\nYou'd like to know about our family plan options. 🤝 Let me check that for you—one moment, please. 🚀\"\n\n### Tool Calls\nlookup_policy_document(topic=\"family plan options\")\n\n// After tool call, the assistant would follow up with:\n\n## Assistant Response 2 (after tool call)\n### Message\n\"Okay, here's what I found: 🎉 Our family plan allows up to 5 lines with shared data and a 10% discount for each additional line [Family Plan Policy](ID-010). 📱 Is there anything else I can help you with today? 😊\"\n"), Input: responses.ResponseNewParamsInputUnion{ OfString: openai.String("How much will it cost for international service? I'm traveling to France."), }, Tools: []responses.ToolUnionParam{ { OfFunction: &responses.FunctionToolParam{ Name: "lookup_policy_document", Description: openai.String("Tool to look up internal documents and policies by topic or keyword."), Parameters: map[string]any{ "type": "object", "properties": map[string]any{ "topic": map[string]any{ "type": "string", "description": "The topic or keyword to search for in company policies or documents.", }, }, "required": []any{ "topic", }, "additionalProperties": false, }, Strict: openai.Bool(true), }, }, { OfFunction: &responses.FunctionToolParam{ Name: "get_user_account_info", Description: openai.String("Tool to get user account information"), Parameters: map[string]any{ "type": "object", "properties": map[string]any{ "phone_number": map[string]any{ "type": "string", "description": "Formatted as '(xxx) xxx-xxxx'", }, }, "required": []any{ "phone_number", }, "additionalProperties": false, }, Strict: openai.Bool(true), }, }, },})if err != nil { log.Fatal(err)}if response.Status != responses.ResponseStatusCompleted { log.Fatalf("Response ended with status %s", response.Status)}output := make([]json.RawMessage, 0, len(response.Output))for _, item := range response.Output { output = append(output, json.RawMessage(item.RawJSON()))}if err := json.NewEncoder(os.Stdout).Encode(output); err != nil { log.Fatal(err)}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112import com.openai.client.OpenAIClient;import com.openai.client.okhttp.OpenAIOkHttpClient;import com.openai.core.JsonValue;import com.openai.models.responses.FunctionTool;import com.openai.models.responses.ResponseCreateParams;import java.util.List;import java.util.Map;String customerServiceInstructions = """ You are a helpful customer service agent working for NewTelco, helping a user efficiently fulfill their request while adhering closely to provided guidelines. # Instructions - Always greet the user with "Hi, you've reached NewTelco, how can I help you?" - Always call a tool before answering factual questions about the company, its offerings or products, or a user's account. Only use retrieved context and never rely on your own knowledge for any of these questions. - However, if you don't have enough information to properly call the tool, ask the user for the information you need. - Escalate to a human if the user requests. - Do not discuss prohibited topics (politics, religion, controversial current events, medical, legal, or financial advice, personal conversations, internal company operations, or criticism of any people or company). - Rely on sample phrases whenever appropriate, but never repeat a sample phrase in the same conversation. Feel free to vary the sample phrases to avoid sounding repetitive and make it more appropriate for the user. - Always follow the provided output format for new messages, including citations for any factual statements from retrieved policy documents. - If you're going to call a tool, always message the user with an appropriate message before and after calling the tool. - Maintain a professional and concise tone in all responses, and use emojis between sentences. - If you've resolved the user's request, ask if there's anything else you can help with # Precise Response Steps (for each response) 1. If necessary, call tools to fulfill the user's desired action. Always message the user before and after calling a tool to keep them in the loop. 2. In your response to the user a. Use active listening and echo back what you heard the user ask for. b. Respond appropriately given the above guidelines. # Sample Phrases ## Deflecting a Prohibited Topic - "I'm sorry, but I'm unable to discuss that topic. Is there something else I can help you with?" - "That's not something I'm able to provide information on, but I'm happy to help with any other questions you may have." ## Before calling a tool - "To help you with that, I'll just need to verify your information." - "Let me check that for you—one moment, please." - "I'll retrieve the latest details for you now." ## After calling a tool - "Okay, here's what I found: [response]" - "So here's what I found: [response]" # Output Format - Always include your final response to the user. - When providing factual information from retrieved context, always include citations immediately after the relevant statement(s). Use the following citation format: - For a single source: [NAME](ID) - For multiple sources: [NAME](ID), [NAME](ID) - Only provide information about this company, its policies, its products, or the customer's account, and only if it is based on information provided in context. Do not answer questions outside this scope. # Example ## User Can you tell me about your family plan options? ## Assistant Response 1 ### Message "Hi, you've reached NewTelco, how can I help you? 😊🎉 You'd like to know about our family plan options. 🤝 Let me check that for you—one moment, please. 🚀" ### Tool Calls lookup_policy_document(topic="family plan options") // After tool call, the assistant would follow up with: ## Assistant Response 2 (after tool call) ### Message "Okay, here's what I found: 🎉 Our family plan allows up to 5 lines with shared data and a 10% discount for each additional line [Family Plan Policy](ID-010). 📱 Is there anything else I can help you with today? 😊" """;ResponseCreateParams params = ResponseCreateParams.builder() .model("gpt-4.1-2025-04-14") .instructions(customerServiceInstructions) .input("How much will it cost for international service? I'm traveling to France.") .addTool( customerServiceTool( "lookup_policy_document", "Tool to look up internal documents and policies by topic or keyword.", "topic", "The topic or keyword to search for in company policies or documents.")) .addTool( customerServiceTool( "get_user_account_info", "Tool to get user account information", "phone_number", "Formatted as '(xxx) xxx-xxxx'")) .build();client.responses().create(params).output().forEach(System.out::println);private static FunctionTool customerServiceTool( String name, String description, String parameter, String parameterDescription) { return FunctionTool.builder() .name(name) .description(description) .strict(true) .parameters( FunctionTool.Parameters.builder() .putAdditionalProperty("type", JsonValue.from("object")) .putAdditionalProperty( "properties", JsonValue.from( Map.of( parameter, Map.of("type", "string", "description", parameterDescription)))) .putAdditionalProperty("required", JsonValue.from(List.of(parameter))) .putAdditionalProperty("additionalProperties", JsonValue.from(false)) .build()) .build();}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89using System.ClientModel.Primitives;using OpenAI.Responses;#pragma warning disable OPENAI001 string instructions = """You are a helpful customer service agent working for NewTelco, helping a user efficiently fulfill their request while adhering closely to provided guidelines.# Instructions- Always greet the user with "Hi, you've reached NewTelco, how can I help you?"- Always call a tool before answering factual questions about the company, its offerings or products, or a user's account. Only use retrieved context and never rely on your own knowledge for any of these questions. - However, if you don't have enough information to properly call the tool, ask the user for the information you need.- Escalate to a human if the user requests.- Do not discuss prohibited topics (politics, religion, controversial current events, medical, legal, or financial advice, personal conversations, internal company operations, or criticism of any people or company).- Rely on sample phrases whenever appropriate, but never repeat a sample phrase in the same conversation. Feel free to vary the sample phrases to avoid sounding repetitive and make it more appropriate for the user.- Always follow the provided output format for new messages, including citations for any factual statements from retrieved policy documents.- If you're going to call a tool, always message the user with an appropriate message before and after calling the tool.- Maintain a professional and concise tone in all responses, and use emojis between sentences.- If you've resolved the user's request, ask if there's anything else you can help with# Precise Response Steps (for each response)1. If necessary, call tools to fulfill the user's desired action. Always message the user before and after calling a tool to keep them in the loop.2. In your response to the user a. Use active listening and echo back what you heard the user ask for. b. Respond appropriately given the above guidelines.# Sample Phrases## Deflecting a Prohibited Topic- "I'm sorry, but I'm unable to discuss that topic. Is there something else I can help you with?"- "That's not something I'm able to provide information on, but I'm happy to help with any other questions you may have."## Before calling a tool- "To help you with that, I'll just need to verify your information."- "Let me check that for you—one moment, please."- "I'll retrieve the latest details for you now."## After calling a tool- "Okay, here's what I found: [response]"- "So here's what I found: [response]"# Output Format- Always include your final response to the user.- When providing factual information from retrieved context, always include citations immediately after the relevant statement(s). Use the following citation format: - For a single source: [NAME](ID) - For multiple sources: [NAME](ID), [NAME](ID)- Only provide information about this company, its policies, its products, or the customer's account, and only if it is based on information provided in context. Do not answer questions outside this scope.# Example## UserCan you tell me about your family plan options?## Assistant Response 1### Message"Hi, you've reached NewTelco, how can I help you? 😊🎉You'd like to know about our family plan options. 🤝 Let me check that for you—one moment, please. 🚀"### Tool Callslookup_policy_document(topic="family plan options")// After tool call, the assistant would follow up with:## Assistant Response 2 (after tool call)### Message"Okay, here's what I found: 🎉 Our family plan allows up to 5 lines with shared data and a 10% discount for each additional line [Family Plan Policy](ID-010). 📱 Is there anything else I can help you with today? 😊""""; CreateResponseOptions options = new("gpt-4.1-2025-04-14", [ResponseItem.CreateUserMessageItem("How much will it cost for international service? I'm traveling to France.")]) { Instructions = instructions }; options.Tools.Add(ResponseTool.CreateFunctionTool( "lookup_policy_document", BinaryData.FromString("""{"type": "object", "properties": {"topic": {"type": "string", "description": "The topic or keyword to search for in company policies or documents."}}, "required": ["topic"], "additionalProperties": false}"""), strictModeEnabled: true, functionDescription: """Tool to look up internal documents and policies by topic or keyword.""")); options.Tools.Add(ResponseTool.CreateFunctionTool( "get_user_account_info", BinaryData.FromString("""{"type": "object", "properties": {"phone_number": {"type": "string", "description": "Formatted as '(xxx) xxx-xxxx'"}}, "required": ["phone_number"], "additionalProperties": false}"""), strictModeEnabled: true, functionDescription: """Tool to get user account information""")); ResponseResult response = await client.CreateResponseAsync(options); if (response.Status != ResponseStatus.Completed) throw new InvalidOperationException($"Response ended with status {response.Status}"); foreach (ResponseItem item in response.OutputItems) Console.WriteLine(ModelReaderWriter.Write(item));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42require "json"require "openai"client = OpenAI::Client.newresponse = client.responses.create( model: "gpt-4.1", instructions: "You are a customer service assistant. Confirm the customer's goal, use only supplied account facts, and clearly explain the next action.", input: "A customer says a replacement order still has not shipped. Draft a concise response.", tools: [ { type: :function, name: "lookup_policy_document", description: "Look up company policies and documents by topic.", parameters: { type: "object", properties: { topic: { type: "string" } }, required: ["topic"], additionalProperties: false }, strict: true }, { type: :function, name: "get_user_account_info", description: "Get user account information.", parameters: { type: "object", properties: { phone_number: { type: "string", description: "Formatted as '(xxx) xxx-xxxx'" } }, required: ["phone_number"], additionalProperties: false }, strict: true } ])puts(JSON.pretty_generate(response.output.map(&:to_h)))
Example output from the Python SDK:
[{'id': 'msg_67fe92d431548191b7ca6cd604b4784b06efc5beb16b3c5e', 'content': [{'annotations': [], 'text': "Hi, you've reached NewTelco, how can I help you? 🌍✈️\n\nYou'd like to know the cost of international service while traveling to France. 🇫🇷 Let me check the latest details for you—one moment, please. 🕑", 'type': 'output_text'}], 'role': 'assistant', 'status': 'completed', 'type': 'message'}, {'arguments': '{"topic":"international service cost France"}', 'call_id': 'call_cF63DLeyhNhwfdyME3ZHd0yo', 'name': 'lookup_policy_document', 'type': 'function_call', 'id': 'fc_67fe92d5d6888191b6cd7cf57f707e4606efc5beb16b3c5e', 'status': 'completed'}]
5. General Advice
Prompt Structure
For reference, here is a good starting point for structuring your prompts.
# Role and Objective# Instructions## Sub-categories for more detailed instructions# Reasoning Steps# Output Format# Examples## Example 1# Context# Final instructions and prompt to think step by step
Add or remove sections to suit your needs, and experiment to determine what’s optimal for your usage.
Delimiters
Here are some general guidelines for selecting the best delimiters for your prompt. Please refer to the Long Context section for special considerations for that context type.
Markdown: We recommend starting here, and using markdown titles for major sections and subsections (including deeper hierarchy, to H4+). Use inline backticks or backtick blocks to precisely wrap code, and standard numbered or bulleted lists as needed.
XML: These also perform well, and we have improved adherence to information in XML with this model. XML is convenient to precisely wrap a section including start and end, add metadata to the tags for additional context, and enable nesting. Here is an example of using XML tags to nest examples in an example section, with inputs and outputs for each:
JSON is highly structured and well understood by the model particularly in coding contexts. However it can be more verbose, and require character escaping that can add overhead.
Guidance specifically for adding a large number of documents or files to input context:
XML performed well in our long context testing.
Example: <doc id='1' title='The Fox'>The quick brown fox jumps over the lazy dog</doc>
This format, proposed by Lee et al. (ref), also performed well in our long context testing.
Example: ID: 1 | TITLE: The Fox | CONTENT: The quick brown fox jumps over the lazy dog
JSON performed particularly poorly.
Example: [{'id': 1, 'title': 'The Fox', 'content': 'The quick brown fox jumped over the lazy dog'}]
The model is trained to robustly understand structure in a variety of formats. Generally, use your judgement and think about what will provide clear information and “stand out” to the model. For example, if you’re retrieving documents that contain lots of XML, an XML-based delimiter will likely be less effective.
Caveats
In some isolated cases we have observed the model being resistant to producing very long, repetitive outputs, for example, analyzing hundreds of items one by one. If this is necessary for your use case, instruct the model strongly to output this information in full, and consider breaking down the problem or using a more concise approach.
We have seen some rare instances of parallel tool calls being incorrect. We advise testing this, and considering setting the parallel_tool_calls param to false if you’re seeing issues.
Appendix: Generating and Applying File Diffs
Developers have provided us feedback that accurate and well-formed diff generation is a critical capability to power coding-related tasks. To this end, the GPT-4.1 family features substantially improved diff capabilities relative to previous GPT models. Moreover, while GPT-4.1 has strong performance generating diffs of any format given clear instructions and examples, we open-source here one recommended diff format, on which the model has been extensively trained. We hope that in particular for developers just starting out, that this will take much of the guesswork out of creating diffs yourself.
Apply Patch
See the example below for a prompt that applies our recommended tool call correctly.
Define the patch tool
TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70constapplyPatchToolDescription=`This is a custom utility that makes it more convenient to add, remove, move, or edit code files. apply_patch effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the apply_patch command, you should pass a message of the following structure as "input":%%bashapply_patch <<"EOF"*** Begin Patch[YOUR_PATCH]*** End PatchEOFWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.For each snippet of code that needs to be changed, repeat the following:[context_before] -> See below for further instructions on context.- [old_code] -> Precede the old code with a minus sign.+ [new_code] -> Precede the new, replacement code with a plus sign.[context_after] -> See below for further instructions on context.For instructions on [context_before] and [context_after]:- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines.- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:@@ class BaseClass[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple @@ statements to jump to the right context. For instance:@@ class BaseClass@@ def method():[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.%%bashapply_patch <<"EOF"*** Begin Patch*** Update File: pygorithm/searching/binary_search.py@@ class BaseClass@@ def search():- pass+ raise NotImplementedError()@@ class Subclass@@ def search():- pass+ raise NotImplementedError()*** End PatchEOF`;consttool= { name: "apply_patch", description: applyPatchToolDescription, parameters: { type: "object", properties: { input: { type: "string", description: "The apply_patch command to execute.", }, }, required: ["input"], },};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70APPLY_PATCH_TOOL_DESC = """This is a custom utility that makes it more convenient to add, remove, move, or edit code files. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input":%%bashapply_patch <<"EOF"*** Begin Patch[YOUR_PATCH]*** End PatchEOFWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.For each snippet of code that needs to be changed, repeat the following:[context_before] -> See below for further instructions on context.- [old_code] -> Precede the old code with a minus sign.+ [new_code] -> Precede the new, replacement code with a plus sign.[context_after] -> See below for further instructions on context.For instructions on [context_before] and [context_after]:- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines.- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:@@ class BaseClass[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:@@ class BaseClass@@ def method():[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.%%bashapply_patch <<"EOF"*** Begin Patch*** Update File: pygorithm/searching/binary_search.py@@ class BaseClass@@ def search():- pass+ raise NotImplementedError()@@ class Subclass@@ def search():- pass+ raise NotImplementedError()*** End PatchEOF"""APPLY_PATCH_TOOL = { "name": "apply_patch", "description": APPLY_PATCH_TOOL_DESC, "parameters": { "type": "object", "properties": { "input": { "type": "string", "description": " The apply_patch command that you wish to execute.", } }, "required": ["input"], },}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79package mainimport ( "encoding/json" "os")const applyPatchToolDescription = `This is a custom utility that makes it more convenient to add, remove, move, or edit code files. apply_patch effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the apply_patch command, you should pass a message of the following structure as "input":%%bashapply_patch <<"EOF"*** Begin Patch[YOUR_PATCH]*** End PatchEOFWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.For each snippet of code that needs to be changed, repeat the following:[context_before] -> See below for further instructions on context.- [old_code] -> Precede the old code with a minus sign.+ [new_code] -> Precede the new, replacement code with a plus sign.[context_after] -> See below for further instructions on context.For instructions on [context_before] and [context_after]:- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines.- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:@@ class BaseClass[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple @@ statements to jump to the right context. For instance:@@ class BaseClass@@ def method():[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.%%bashapply_patch <<"EOF"*** Begin Patch*** Update File: pygorithm/searching/binary_search.py@@ class BaseClass@@ def search():- pass+ raise NotImplementedError()@@ class Subclass@@ def search():- pass+ raise NotImplementedError()*** End PatchEOF`func main() { tool := map[string]any{ "name": "apply_patch", "description": applyPatchToolDescription, "parameters": map[string]any{ "type": "object", "properties": map[string]any{ "input": map[string]any{"type": "string", "description": "The apply_patch command to execute."}, }, "required": []string{"input"}, }, } if err := json.NewEncoder(os.Stdout).Encode(tool); err != nil { panic(err) }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23import com.fasterxml.jackson.databind.ObjectMapper;import java.util.List;import java.util.Map;String description = "This is a custom utility that makes it more convenient to add, remove, move, or edit code files. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as \"input\":\n\n%%bash\napply_patch <<\"EOF\"\n*** Begin Patch\n[YOUR_PATCH]\n*** End Patch\nEOF\n\nWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.\n\n*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.\nFor each snippet of code that needs to be changed, repeat the following:\n[context_before] -> See below for further instructions on context.\n- [old_code] -> Precede the old code with a minus sign.\n+ [new_code] -> Precede the new, replacement code with a plus sign.\n[context_after] -> See below for further instructions on context.\n\nFor instructions on [context_before] and [context_after]:\n- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change\u2019s [context_after] lines in the second change\u2019s [context_before] lines.\n- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:\n@@ class BaseClass\n[3 lines of pre-context]\n- [old_code]\n+ [new_code]\n[3 lines of post-context]\n\n- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:\n\n@@ class BaseClass\n@@ \tdef method():\n[3 lines of pre-context]\n- [old_code]\n+ [new_code]\n[3 lines of post-context]\n\nNote, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as \"input\" to this function, in order to apply a patch, is shown below.\n\n%%bash\napply_patch <<\"EOF\"\n*** Begin Patch\n*** Update File: pygorithm/searching/binary_search.py\n@@ class BaseClass\n@@ def search():\n- pass\n+ raise NotImplementedError()\n\n@@ class Subclass\n@@ def search():\n- pass\n+ raise NotImplementedError()\n\n*** End Patch\nEOF\n";var tool = Map.of( "name", "apply_patch", "description", description, "parameters", Map.of( "type", "object", "properties", Map.of( "input", Map.of("type", "string", "description", "The apply_patch command to execute.")), "required", List.of("input")));System.out.println(new ObjectMapper().writeValueAsString(tool));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68using System.ClientModel.Primitives;using OpenAI.Responses;#pragma warning disable OPENAI001 string description = """This is a custom utility that makes it more convenient to add, remove, move, or edit code files. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input":%%bashapply_patch <<"EOF"*** Begin Patch[YOUR_PATCH]*** End PatchEOFWhere [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.For each snippet of code that needs to be changed, repeat the following:[context_before] -> See below for further instructions on context.- [old_code] -> Precede the old code with a minus sign.+ [new_code] -> Precede the new, replacement code with a plus sign.[context_after] -> See below for further instructions on context.For instructions on [context_before] and [context_after]:- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines.- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:@@ class BaseClass[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:@@ class BaseClass@@ def method():[3 lines of pre-context]- [old_code]+ [new_code][3 lines of post-context]Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.%%bashapply_patch <<"EOF"*** Begin Patch*** Update File: pygorithm/searching/binary_search.py@@ class BaseClass@@ def search():- pass+ raise NotImplementedError()@@ class Subclass@@ def search():- pass+ raise NotImplementedError()*** End PatchEOF"""; ResponseTool tool = ResponseTool.CreateFunctionTool("apply_patch", functionDescription: description, functionParameters: BinaryData.FromObjectAsJson(new { type = "object", properties = new { input = new { type = "string", description = " The apply_patch command that you wish to execute." } }, required = (string[])["input"] }), strictModeEnabled: false);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75require "json"APPLY_PATCH_TOOL_DESC = <<~PROMPT This is a custom utility that makes it more convenient to add, remove, move, or edit code files. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input": %%bash apply_patch <<"EOF" *** Begin Patch [YOUR_PATCH] *** End Patch EOF Where [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format. *** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete. For each snippet of code that needs to be changed, repeat the following: [context_before] -> See below for further instructions on context. - [old_code] -> Precede the old code with a minus sign. + [new_code] -> Precede the new, replacement code with a plus sign. [context_after] -> See below for further instructions on context. For instructions on [context_before] and [context_after]: - By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines. - If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have: @@ class BaseClass [3 lines of pre-context] - [old_code] + [new_code] [3 lines of post-context] - If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance: @@ class BaseClass @@ def method(): [3 lines of pre-context] - [old_code] + [new_code] [3 lines of post-context] Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below. %%bash apply_patch <<"EOF" *** Begin Patch *** Update File: pygorithm/searching/binary_search.py @@ class BaseClass @@ def search(): - pass + raise NotImplementedError() @@ class Subclass @@ def search(): - pass + raise NotImplementedError() *** End Patch EOFPROMPTtool = { name: "apply_patch", description: APPLY_PATCH_TOOL_DESC, parameters: { type: "object", properties: { input: { type: "string", description: "The apply_patch command to execute." } }, required: ["input"] }}puts(JSON.generate(tool))
Reference Implementation: apply_patch.py
This is the Python apply_patch.py reference published in the GPT-4.1 prompting guide, which identifies it as part of the model-training toolchain. It is retained as a historical reference, not an OpenAI SDK feature. The SDK language used to send a request does not require a matching implementation of this command.
Save the program as apply_patch.py and make it available as apply_patch in the shell where the model executes commands. It reads the patch text from standard input, including the *** Begin Patch and *** End Patch markers. The shell command and heredoc shown in the tool description wrap that input; they are not part of the patch text.
Run this historical utility only in an isolated working directory with trusted patch input. It does not confine file paths to that directory, and reported parsing errors do not produce a nonzero exit status.
Historical Python patch reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525#!/usr/bin/env python3"""A self-contained **pure-Python 3.9+** utility for applying human-readable“pseudo-diff” patch files to a collection of text files."""from__future__import annotationsimport pathlibfrom collections.abc import Callablefrom dataclasses import dataclass, fieldfrom enum import Enum# --------------------------------------------------------------------------- ## Domain objects# --------------------------------------------------------------------------- #classActionType(str, Enum):ADD="add"DELETE="delete"UPDATE="update"@dataclassclassFileChange:type: ActionType old_content: str|None=None new_content: str|None=None move_path: str|None=None@dataclassclassCommit: changes: dict[str, FileChange] = field(default_factory=dict)# --------------------------------------------------------------------------- ## Exceptions# --------------------------------------------------------------------------- #classDiffError(ValueError):"""Any problem detected while parsing or applying a patch."""# --------------------------------------------------------------------------- ## Helper dataclasses used while parsing patches# --------------------------------------------------------------------------- #@dataclassclassChunk: orig_index: int=-1 del_lines: list[str] = field(default_factory=list) ins_lines: list[str] = field(default_factory=list)@dataclassclassPatchAction:type: ActionType new_file: str|None=None chunks: list[Chunk] = field(default_factory=list) move_path: str|None=None@dataclassclassPatch: actions: dict[str, PatchAction] = field(default_factory=dict)# --------------------------------------------------------------------------- ## Patch text parser# --------------------------------------------------------------------------- #@dataclassclassParser: current_files: dict[str, str] lines: list[str] index: int=0 patch: Patch = field(default_factory=Patch) fuzz: int=0# ------------- low-level helpers -------------------------------------- #def_cur_line(self) -> str:ifself.index >=len(self.lines):raise DiffError("Unexpected end of input while parsing patch")returnself.lines[self.index]@staticmethoddef_norm(line: str) -> str:"""Strip CR so comparisons work for both LF and CRLF input."""return line.rstrip("\r")# ------------- scanning convenience ----------------------------------- #defis_done(self, prefixes: tuple[str, ...] |None=None) -> bool:ifself.index >=len(self.lines):returnTrueif ( prefixesandlen(prefixes) >0andself._norm(self._cur_line()).startswith(prefixes) ):returnTruereturnFalsedefstartswith(self, prefix: str| tuple[str, ...]) -> bool:returnself._norm(self._cur_line()).startswith(prefix)defread_str(self, prefix: str) -> str:""" Consume the current line if it starts with *prefix* and return the text **after** the prefix. Raises if prefix is empty. """if prefix =="":raiseValueError("read_str() requires a non-empty prefix")ifself._norm(self._cur_line()).startswith(prefix): text =self._cur_line()[len(prefix) :]self.index +=1return textreturn""defread_line(self) -> str:"""Return the current raw line and advance.""" line =self._cur_line()self.index +=1return line# ------------- public entry point -------------------------------------- #defparse(self) -> None:whilenotself.is_done(("*** End Patch",)):# ---------- UPDATE ---------- # path =self.read_str("*** Update File: ")if path:if path inself.patch.actions:raise DiffError(f"Duplicate update for file: {path}") move_to =self.read_str("*** Move to: ")if path notinself.current_files:raise DiffError(f"Update File Error - missing file: {path}") text =self.current_files[path] action =self._parse_update_file(text) action.move_path = move_to orNoneself.patch.actions[path] = actioncontinue# ---------- DELETE ---------- # path =self.read_str("*** Delete File: ")if path:if path inself.patch.actions:raise DiffError(f"Duplicate delete for file: {path}")if path notinself.current_files:raise DiffError(f"Delete File Error - missing file: {path}")self.patch.actions[path] = PatchAction(type=ActionType.DELETE)continue# ---------- ADD ---------- # path =self.read_str("*** Add File: ")if path:if path inself.patch.actions:raise DiffError(f"Duplicate add for file: {path}")if path inself.current_files:raise DiffError(f"Add File Error - file already exists: {path}")self.patch.actions[path] =self._parse_add_file()continueraise DiffError(f"Unknown line while parsing: {self._cur_line()}")ifnotself.startswith("*** End Patch"):raise DiffError("Missing *** End Patch sentinel")self.index +=1# consume sentinel# ------------- section parsers ---------------------------------------- #def_parse_update_file(self, text: str) -> PatchAction: action = PatchAction(type=ActionType.UPDATE) lines = text.split("\n") index =0whilenotself.is_done( ("*** End Patch","*** Update File:","*** Delete File:","*** Add File:","*** End of File", ) ): def_str =self.read_str("@@ ") section_str =""ifnot def_str andself._norm(self._cur_line()) =="@@": section_str =self.read_line()ifnot (def_str or section_str or index ==0):raise DiffError(f"Invalid line in update section:\n{self._cur_line()}")if def_str.strip(): found =Falseif def_str notin lines[:index]:for i, s inenumerate(lines[index:], index):if s == def_str: index = i +1 found =Truebreakifnot found and def_str.strip() notin [ s.strip() for s in lines[:index] ]:for i, s inenumerate(lines[index:], index):if s.strip() == def_str.strip(): index = i +1self.fuzz +=1 found =Truebreak next_ctx, chunks, end_idx, eof = peek_next_section(self.lines, self.index) new_index, fuzz = find_context(lines, next_ctx, index, eof)if new_index ==-1: ctx_txt ="\n".join(next_ctx)raise DiffError(f"Invalid {'EOF 'if eof else''}context at {index}:\n{ctx_txt}" )self.fuzz += fuzzfor ch in chunks: ch.orig_index += new_index action.chunks.append(ch) index = new_index +len(next_ctx)self.index = end_idxreturn actiondef_parse_add_file(self) -> PatchAction: lines: list[str] = []whilenotself.is_done( ("*** End Patch", "*** Update File:", "*** Delete File:", "*** Add File:") ): s =self.read_line()ifnot s.startswith("+"):raise DiffError(f"Invalid Add File line (missing '+'): {s}") lines.append(s[1:]) # strip leading '+'return PatchAction(type=ActionType.ADD, new_file="\n".join(lines))# --------------------------------------------------------------------------- ## Helper functions# --------------------------------------------------------------------------- #deffind_context_core( lines: list[str], context: list[str], start: int) -> tuple[int, int]:ifnot context:return start, 0for i inrange(start, len(lines)):if lines[i : i +len(context)] == context:return i, 0for i inrange(start, len(lines)):if [s.rstrip() for s in lines[i : i +len(context)]] == [ s.rstrip() for s in context ]:return i, 1for i inrange(start, len(lines)):if [s.strip() for s in lines[i : i +len(context)]] == [ s.strip() for s in context ]:return i, 100return-1, 0deffind_context( lines: list[str], context: list[str], start: int, eof: bool) -> tuple[int, int]:if eof: new_index, fuzz = find_context_core(lines, context, len(lines) -len(context))if new_index !=-1:return new_index, fuzz new_index, fuzz = find_context_core(lines, context, start)return new_index, fuzz +10_000return find_context_core(lines, context, start)defpeek_next_section( lines: list[str], index: int) -> tuple[list[str], list[Chunk], int, bool]: old: list[str] = [] del_lines: list[str] = [] ins_lines: list[str] = [] chunks: list[Chunk] = [] mode ="keep" orig_index = indexwhile index <len(lines): s = lines[index]if s.startswith( ("@@","*** End Patch","*** Update File:","*** Delete File:","*** Add File:","*** End of File", ) ):breakif s =="***":breakif s.startswith("***"):raise DiffError(f"Invalid Line: {s}") index +=1 last_mode = modeif s =="": s =" "if s[0] =="+": mode ="add"elif s[0] =="-": mode ="delete"elif s[0] ==" ": mode ="keep"else:raise DiffError(f"Invalid Line: {s}") s = s[1:]if mode =="keep"and last_mode != mode:if ins_lines or del_lines: chunks.append( Chunk(orig_index=len(old) -len(del_lines),del_lines=del_lines,ins_lines=ins_lines, ) ) del_lines, ins_lines = [], []if mode =="delete": del_lines.append(s) old.append(s)elif mode =="add": ins_lines.append(s)elif mode =="keep": old.append(s)if ins_lines or del_lines: chunks.append( Chunk(orig_index=len(old) -len(del_lines),del_lines=del_lines,ins_lines=ins_lines, ) )if index <len(lines) and lines[index] =="*** End of File": index +=1return old, chunks, index, Trueif index == orig_index:raise DiffError("Nothing in this section")return old, chunks, index, False# --------------------------------------------------------------------------- ## Patch → Commit and Commit application# --------------------------------------------------------------------------- #def_get_updated_file(text: str, action: PatchAction, path: str) -> str:if action.type isnot ActionType.UPDATE:raise DiffError("_get_updated_file called with non-update action") orig_lines = text.split("\n") dest_lines: list[str] = [] orig_index =0for chunk in action.chunks:if chunk.orig_index >len(orig_lines):raise DiffError(f"{path}: chunk.orig_index {chunk.orig_index} exceeds file length" )if orig_index > chunk.orig_index:raise DiffError(f"{path}: overlapping chunks at {orig_index} > {chunk.orig_index}" ) dest_lines.extend(orig_lines[orig_index : chunk.orig_index]) orig_index = chunk.orig_index dest_lines.extend(chunk.ins_lines) orig_index +=len(chunk.del_lines) dest_lines.extend(orig_lines[orig_index:])return"\n".join(dest_lines)defpatch_to_commit(patch: Patch, orig: dict[str, str]) -> Commit: commit = Commit()for path, action in patch.actions.items():if action.type is ActionType.DELETE: commit.changes[path] = FileChange(type=ActionType.DELETE, old_content=orig[path] )elif action.type is ActionType.ADD:if action.new_file isNone:raise DiffError("ADD action without file content") commit.changes[path] = FileChange(type=ActionType.ADD, new_content=action.new_file )elif action.type is ActionType.UPDATE: new_content = _get_updated_file(orig[path], action, path) commit.changes[path] = FileChange(type=ActionType.UPDATE,old_content=orig[path],new_content=new_content,move_path=action.move_path, )return commit# --------------------------------------------------------------------------- ## User-facing helpers# --------------------------------------------------------------------------- #deftext_to_patch(text: str, orig: dict[str, str]) -> tuple[Patch, int]: lines = text.splitlines() # preserves blank lines, no strip()if (len(lines) <2ornot Parser._norm(lines[0]).startswith("*** Begin Patch")or Parser._norm(lines[-1]) !="*** End Patch" ):raise DiffError("Invalid patch text - missing sentinels") parser = Parser(current_files=orig, lines=lines, index=1) parser.parse()return parser.patch, parser.fuzzdefidentify_files_needed(text: str) -> list[str]: lines = text.splitlines()return [ line[len("*** Update File: ") :]for line in linesif line.startswith("*** Update File: ") ] + [ line[len("*** Delete File: ") :]for line in linesif line.startswith("*** Delete File: ") ]defidentify_files_added(text: str) -> list[str]: lines = text.splitlines()return [ line[len("*** Add File: ") :]for line in linesif line.startswith("*** Add File: ") ]# --------------------------------------------------------------------------- ## File-system helpers# --------------------------------------------------------------------------- #defload_files(paths: list[str], open_fn: Callable[[str], str]) -> dict[str, str]:return {path: open_fn(path) for path in paths}defapply_commit( commit: Commit, write_fn: Callable[[str, str], None], remove_fn: Callable[[str], None],) -> None:for path, change in commit.changes.items():if change.type is ActionType.DELETE: remove_fn(path)elif change.type is ActionType.ADD:if change.new_content isNone:raise DiffError(f"ADD change for {path} has no content") write_fn(path, change.new_content)elif change.type is ActionType.UPDATE:if change.new_content isNone:raise DiffError(f"UPDATE change for {path} has no new content") target = change.move_path or path write_fn(target, change.new_content)if change.move_path: remove_fn(path)defprocess_patch( text: str, open_fn: Callable[[str], str], write_fn: Callable[[str, str], None], remove_fn: Callable[[str], None],) -> str:ifnot text.startswith("*** Begin Patch"):raise DiffError("Patch text must start with *** Begin Patch") paths = identify_files_needed(text) orig = load_files(paths, open_fn) patch, _fuzz = text_to_patch(text, orig) commit = patch_to_commit(patch, orig) apply_commit(commit, write_fn, remove_fn)return"Done!"# --------------------------------------------------------------------------- ## Default FS helpers# --------------------------------------------------------------------------- #defopen_file(path: str) -> str:withopen(path, "rt", encoding="utf-8") as fh:return fh.read()defwrite_file(path: str, content: str) -> None: target = pathlib.Path(path) target.parent.mkdir(parents=True, exist_ok=True)with target.open("wt", encoding="utf-8") as fh: fh.write(content)defremove_file(path: str) -> None: pathlib.Path(path).unlink(missing_ok=True)# --------------------------------------------------------------------------- ## CLI entry-point# --------------------------------------------------------------------------- #defmain() -> None:import sys patch_text = sys.stdin.read()ifnot patch_text:print("Please pass patch text through stdin", file=sys.stderr)returntry: result = process_patch(patch_text, open_file, write_file, remove_file)except DiffError as exc:print(exc, file=sys.stderr)returnprint(result)if__name__=="__main__": main()
Other Effective Diff Formats
If you want to try using a different diff format, we found in testing that the SEARCH/REPLACE diff format used in Aider’s polyglot benchmark, as well as a pseudo-XML format with no internal escaping, both had high success rates.
These diff formats share two key aspects: (1) they do not use line numbers, and (2) they provide both the exact code to be replaced, and the exact code with which to replace it, with clear delimiters between the two.