Skip to content

Commit eb68131

Browse files
fix(core): select built-in shell and apply_patch tools by their own type (#4952)
1 parent ae5803f commit eb68131

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎src/agents/models/openai_responses.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,14 @@ def convert_tool_choice(
18871887
return {
18881888
"type": "code_interpreter",
18891889
}
1890+
elif tool_choice == "shell" and cls._has_shell_tool(tools):
1891+
return {
1892+
"type": "shell",
1893+
}
1894+
elif tool_choice == "apply_patch" and cls._has_apply_patch_tool(tools):
1895+
return {
1896+
"type": "apply_patch",
1897+
}
18901898
elif tool_choice == "mcp":
18911899
# Note that this is still here for backwards compatibility,
18921900
# but migrating to MCPToolChoice is recommended.
@@ -1999,6 +2007,14 @@ def _validate_named_function_tool_choice(
19992007
"the tool via ToolSearchTool() first."
20002008
)
20012009

2010+
@classmethod
2011+
def _has_shell_tool(cls, tools: Sequence[Tool] | None) -> bool:
2012+
return any(isinstance(tool, ShellTool) for tool in tools or ())
2013+
2014+
@classmethod
2015+
def _has_apply_patch_tool(cls, tools: Sequence[Tool] | None) -> bool:
2016+
return any(isinstance(tool, ApplyPatchTool) for tool in tools or ())
2017+
20022018
@classmethod
20032019
def _has_computer_tool(cls, tools: Sequence[Tool] | None) -> bool:
20042020
return any(isinstance(tool, ComputerTool) for tool in tools or ())

‎tests/models/test_openai_responses.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
from agents import (
2222
Agent,
23+
ApplyPatchTool,
2324
AsyncComputer,
2425
Computer,
2526
ComputerTool,
2627
ImageGenerationTool,
2728
ModelSettings,
2829
ModelTracing,
2930
Runner,
31+
ShellTool,
3032
Tool,
3133
ToolSearchTool,
3234
WebSearchTool,
@@ -2223,6 +2225,50 @@ def __init__(self):
22232225
]
22242226

22252227

2228+
@pytest.mark.allow_call_model_methods
2229+
@pytest.mark.asyncio
2230+
@pytest.mark.parametrize("tool_choice", ["shell", "apply_patch"])
2231+
async def test_builtin_tool_choice_selects_builtin_among_multiple_tools(tool_choice: str) -> None:
2232+
called_kwargs: dict[str, Any] = {}
2233+
2234+
class DummyResponses:
2235+
async def create(self, **kwargs):
2236+
nonlocal called_kwargs
2237+
called_kwargs = kwargs
2238+
return get_response_obj([])
2239+
2240+
class DummyResponsesClient:
2241+
def __init__(self):
2242+
self.responses = DummyResponses()
2243+
2244+
builtin_tool: Tool = (
2245+
ShellTool(executor=lambda request: "ok")
2246+
if tool_choice == "shell"
2247+
else ApplyPatchTool(editor=cast(Any, object()))
2248+
)
2249+
model = OpenAIResponsesModel(
2250+
model="gpt-5.4",
2251+
openai_client=DummyResponsesClient(), # type: ignore[arg-type]
2252+
)
2253+
2254+
await model.get_response(
2255+
system_instructions=None,
2256+
input="hi",
2257+
model_settings=ModelSettings(tool_choice=tool_choice),
2258+
tools=[
2259+
function_tool(lambda: "ok", name_override="lookup_account"),
2260+
builtin_tool,
2261+
function_tool(lambda: "ok", name_override="summarize"),
2262+
],
2263+
output_schema=None,
2264+
handoffs=[handoff(Agent(name="Target"))],
2265+
tracing=ModelTracing.DISABLED,
2266+
)
2267+
2268+
assert called_kwargs["tool_choice"] == {"type": tool_choice}
2269+
assert tool_choice in [dict(tool).get("type") for tool in called_kwargs["tools"]]
2270+
2271+
22262272
@pytest.mark.allow_call_model_methods
22272273
@pytest.mark.asyncio
22282274
async def test_websocket_model_reuses_connection_and_sends_response_create_frames(monkeypatch):

‎tests/models/test_openai_responses_converter.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from agents import (
3535
Agent,
3636
AgentOutputSchema,
37+
ApplyPatchTool,
3738
Computer,
3839
ComputerTool,
3940
FileSearchTool,
@@ -169,6 +170,16 @@ def test_convert_tool_choice_allows_function_named_computer_without_computer_too
169170
}
170171

171172

173+
def test_convert_tool_choice_builtin_shell_and_apply_patch() -> None:
174+
shell_tool = ShellTool(executor=lambda request: "ok")
175+
apply_patch_tool = ApplyPatchTool(editor=cast(Any, object()))
176+
177+
assert Converter.convert_tool_choice("shell", tools=[shell_tool]) == {"type": "shell"}
178+
assert Converter.convert_tool_choice("apply_patch", tools=[apply_patch_tool]) == {
179+
"type": "apply_patch"
180+
}
181+
182+
172183
def test_convert_tool_choice_allows_function_named_tool_search() -> None:
173184
tool = function_tool(lambda city: city, name_override="tool_search")
174185

0 commit comments

Comments
 (0)