-
Notifications
You must be signed in to change notification settings - Fork 100
feat(tools): Add GPT-5.1 ApplyPatch tool #1166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
9d2442a
feat(tools): add ApplyPatchTool implementing OpenAI cookbook apply_pa…
enyst c5a0836
chore(example): enable native tool calling (Responses API) for GPT-5.…
enyst fc86fa7
ApplyPatch: Responses API name-only tool spec; accept 'patch' alias; …
enyst 3c429e1
ApplyPatch: accept 'patch' and expose minimal schema for Responses; f…
enyst 772ce28
Responses: do not echo assistant function_call or reasoning items in …
enyst f08a048
Responses: include assistant function_call items but omit reasoning p…
enyst 9ae743f
Docs(dev): ApplyPatch + OpenAI Responses integration notes
enyst 02e67d3
Responses serialization: restore reasoning passthrough and keep assis…
enyst a00128b
Tests: add Responses pairing test for function_call and function_call…
enyst 35e3be3
Examples+Docs: add FileEditor GPT-5.1 example and update ApplyPatch R…
enyst e910dbf
ApplyPatch: include text output so Responses gets function_call_outpu…
enyst 6cd9639
Tests: add ApplyPatchExecutor tests for create/append/delete and path…
enyst e772cbb
Docs: update ApplyPatch Responses notes with paired tool output examp…
enyst 0149040
Remove mistakenly committed FACTS.txt test artifact.
enyst e8978ad
ApplyPatch: drop patch_text alias; use canonical 'patch' name only ac…
enyst 73f7f81
Docs: update ApplyPatch notes to canonicalize on 'patch' only; remove…
enyst fab4f3b
Revert openhands.sdk.llm.message to main branch version (comments-onl…
enyst db5b177
Example: simplify tool registration; add ApplyPatch like other tools …
enyst 74f0d15
ApplyPatch: align create() return type to Sequence; add docstrings an…
enyst a7eed40
Merge branch 'main' into feat/apply-patch-tool-gpt5-1
enyst d1ce8f3
Delete docs/dev/apply_patch_responses_notes.md
enyst 3078a95
Update examples/01_standalone_sdk/28_apply_patch_with_gpt5_1.py
enyst 8691cec
Restore telemetry.py from main
enyst 1f7e0af
Add apply_patch executor fuzz and error-path tests
enyst f4c54d2
Harden apply_patch missing-delete handling to DiffError
enyst dd6c7df
Clarify delete-missing-file test comment
enyst 9dff45e
Add apply_patch multi-hunk and multi-file success tests
enyst 99213f1
Enrich apply_patch gpt-5.1 example with multi-file/multi-hunk flow
enyst e59b3c4
Delete examples/01_standalone_sdk/29_file_editor_with_gpt5_1.py
enyst 76c21e3
test: clarify apply_patch tmp workspace comment
enyst 7fa1aab
test: move apply_patch executor tests into subpackage
enyst 6c89fd4
feat(preset): use apply_patch by default for GPT-5 models
enyst 5d99105
Merge branch 'main' into feat/apply-patch-tool-gpt5-1
enyst 53f0301
Update allowed-model-stubs.json
enyst 24da42f
Merge branch 'main' into feat/apply-patch-tool-gpt5-1
enyst 9362ad9
Update .github/run-eval/allowed-model-stubs.json
enyst bd4d338
Update examples/01_standalone_sdk/28_apply_patch_with_gpt5_1.py
enyst a0e1070
chore(split): keep only ApplyPatch tool, LLM Responses formatter, and…
enyst adcb46d
Merge branch 'main' into feat/apply-patch-tool-gpt5-1
enyst be8e7c7
Delete examples/01_standalone_sdk/28_apply_patch_with_gpt5_1.py
enyst adffe6a
Merge branch 'main' into feat/apply-patch-tool-gpt5-1
enyst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """Example: Using ApplyPatch tool with GPT-5.1 models via direct OpenAI API. | ||
|
|
||
| This demonstrates adding a new ApplyPatch tool to the agent and guiding the | ||
| model to create, modify, and delete a FACTS.txt file using 'apply_patch' text. | ||
|
|
||
| Notes: | ||
| - Works with any GPT-5.1 family model (names start with "gpt-5.1"). | ||
| - Uses direct OpenAI API through LiteLLM's LLM wrapper with no base_url. | ||
| - Requires OPENAI_API_KEY in the environment (or LLM_API_KEY fallback). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| from pydantic import SecretStr | ||
|
|
||
| from openhands.sdk import LLM, Agent, Conversation, get_logger | ||
| from openhands.sdk.tool import Tool | ||
| from openhands.tools.apply_patch import ApplyPatchTool | ||
| from openhands.tools.task_tracker import TaskTrackerTool | ||
|
|
||
| # from openhands.tools.preset.default import register_default_tools | ||
| from openhands.tools.terminal import TerminalTool | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| api_key = os.getenv("OPENAI_API_KEY") or os.getenv("LLM_API_KEY") | ||
| assert api_key, "Set OPENAI_API_KEY (or LLM_API_KEY) in your environment." | ||
|
|
||
| # Choose a GPT-5.1 model; mini is cost-effective for examples | ||
| default_model = "openai/gpt-5.1-codex-mini" | ||
| model = os.getenv("LLM_MODEL", default_model) | ||
| assert model.startswith("openai/gpt-5.1"), "Model must be an openai gpt-5.1 variant" | ||
|
|
||
| # Force Chat Completions path by using a non-Responses model alias if needed | ||
| if model.startswith("openai/gpt-5.1"): | ||
| # Litellm treats 'openai/gpt-5.1' via Responses; to avoid the Responses tool-output | ||
| # coupling for this example, we can strip the provider prefix for chat path. | ||
| # However, leave as-is to try Responses first; if it errors, instruct user below. | ||
| pass | ||
|
|
||
| llm = LLM( | ||
| model=model, | ||
| api_key=SecretStr(api_key), | ||
| native_tool_calling=True, # enable native tool calling (Responses API) | ||
enyst marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| reasoning_summary=None, # avoid OpenAI org verification requirement | ||
| log_completions=True, # enable telemetry to log input/output payloads | ||
| ) | ||
|
|
||
| # Explicitly register tool classes so Tool(name=...) can resolve | ||
| # They self-register into the global registry on import | ||
| _ = (TerminalTool, TaskTrackerTool, ApplyPatchTool) | ||
|
|
||
| agent = Agent( | ||
| llm=llm, | ||
| tools=[ | ||
| Tool(name="terminal"), | ||
| Tool(name="task_tracker"), | ||
| Tool(name="apply_patch"), | ||
| ], | ||
| system_prompt_kwargs={"cli_mode": True}, | ||
| ) | ||
|
|
||
| conversation = Conversation(agent=agent, workspace=os.getcwd()) | ||
|
|
||
| # Compose instructions guiding the model to use the new tool | ||
| prompt = ( | ||
| "Use the ApplyPatch tool to: " | ||
| "1) create a FACTS.txt with a single line 'OpenHands SDK integrates tools.'; " | ||
| "2) modify FACTS.txt by appending a second line 'ApplyPatch works.'; " | ||
| "3) delete FACTS.txt. " | ||
| "Only use the apply_patch format between '*** Begin Patch' and '*** End Patch' " | ||
| "when calling the tool." | ||
| ) | ||
|
|
||
| conversation.send_message(prompt) | ||
| conversation.run() | ||
|
|
||
| print("Conversation finished.") | ||
| print(f"EXAMPLE_COST: {llm.metrics.accumulated_cost}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Example: Using FileEditor tool with GPT-5.1 models via direct OpenAI API. | ||
|
|
||
| This mirrors the ApplyPatch example but uses FileEditor to create/modify/delete | ||
| FACTS.txt. Useful for comparing Responses input/output behavior and logs. | ||
|
|
||
| Requirements: | ||
| - OPENAI_API_KEY in the environment (or LLM_API_KEY) | ||
| - Model: any openai/gpt-5.1* variant; default uses openai/gpt-5.1-codex-mini | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| from pydantic import SecretStr | ||
|
|
||
| from openhands.sdk import LLM, Agent, Conversation, get_logger | ||
| from openhands.sdk.tool import Tool | ||
| from openhands.tools.file_editor import FileEditorTool | ||
| from openhands.tools.task_tracker import TaskTrackerTool | ||
| from openhands.tools.terminal import TerminalTool | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| api_key = os.getenv("OPENAI_API_KEY") or os.getenv("LLM_API_KEY") | ||
| assert api_key, "Set OPENAI_API_KEY (or LLM_API_KEY) in your environment." | ||
|
|
||
| model = os.getenv("LLM_MODEL", "openai/gpt-5.1-codex-mini") | ||
| assert model.startswith("openai/gpt-5.1"), "Model must be an openai gpt-5.1 variant" | ||
|
|
||
| llm = LLM( | ||
| model=model, | ||
| api_key=SecretStr(api_key), | ||
| native_tool_calling=True, | ||
| reasoning_summary=None, | ||
| log_completions=True, | ||
| ) | ||
|
|
||
| # Ensure registration | ||
| _ = (TerminalTool, TaskTrackerTool, FileEditorTool) | ||
|
|
||
| agent = Agent( | ||
| llm=llm, | ||
| tools=[Tool(name="terminal"), Tool(name="task_tracker"), Tool(name="file_editor")], | ||
| system_prompt_kwargs={"cli_mode": True}, | ||
| ) | ||
|
|
||
| conversation = Conversation(agent=agent, workspace=os.getcwd()) | ||
|
|
||
| prompt = ( | ||
| "You must use tools to perform all actions. Do not merely describe actions. " | ||
| "Use the FileEditor tool to: " | ||
| "1) create a FACTS.txt with a single line 'OpenHands SDK integrates tools.'; " | ||
| "2) modify FACTS.txt by appending a second line 'FileEditor works.'; " | ||
| "3) delete FACTS.txt using the terminal tool with: rm FACTS.txt." | ||
| ) | ||
|
|
||
| conversation.send_message(prompt) | ||
| conversation.run() | ||
|
|
||
| print("Conversation finished.") | ||
| print(f"EXAMPLE_COST: {llm.metrics.accumulated_cost}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from .definition import ApplyPatchTool | ||
|
|
||
|
|
||
| __all__ = ["ApplyPatchTool"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.