You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/agents.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -311,6 +311,8 @@ Supplying a list of tools doesn't always mean the LLM will use a tool. You can f
311
311
3.`none`, which requires the LLM to _not_ use a tool.
312
312
4. Setting a specific string e.g. `my_tool`, which requires the LLM to use that specific tool.
313
313
314
+
When you are using OpenAI Responses tool search, named tool choices are more limited: you cannot target bare namespace names or deferred-only tools with `tool_choice`, and `tool_choice="tool_search"` does not target [`ToolSearchTool`][agents.tool.ToolSearchTool]. In those cases, prefer `auto` or `required`. See [Hosted tool search](tools.md#hosted-tool-search) for the Responses-specific constraints.
315
+
314
316
```python
315
317
from agents import Agent, Runner, function_tool, ModelSettings
Copy file name to clipboardExpand all lines: docs/context.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,6 +124,8 @@ plus additional fields specific to the current tool call:
124
124
-`tool_name` – the name of the tool being invoked
125
125
-`tool_call_id` – a unique identifier for this tool call
126
126
-`tool_arguments` – the raw argument string passed to the tool
127
+
-`tool_namespace` – the Responses namespace for the tool call, when the tool was loaded through `tool_namespace()` or another namespaced surface
128
+
-`qualified_tool_name` – the tool name qualified with the namespace when one is available
127
129
128
130
Use `ToolContext` when you need tool-level metadata during execution.
129
131
For general context sharing between agents and tools, `RunContextWrapper` remains sufficient. Because `ToolContext` extends `RunContextWrapper`, it can also expose `.tool_input` when a nested `Agent.as_tool()` run supplied structured input.
Copy file name to clipboardExpand all lines: docs/mcp.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -102,6 +102,8 @@ asyncio.run(main())
102
102
103
103
The hosted server exposes its tools automatically; you do not add it to `mcp_servers`.
104
104
105
+
If you want hosted tool search to load a hosted MCP server lazily, set `tool_config["defer_loading"] = True` and add [`ToolSearchTool`][agents.tool.ToolSearchTool] to the agent. This is supported only on OpenAI Responses models. See [Tools](tools.md#hosted-tool-search) for the complete tool-search setup and constraints.
106
+
105
107
### Streaming hosted MCP results
106
108
107
109
Hosted tools support streaming results in exactly the same way as function tools. Use `Runner.run_streamed` to
Copy file name to clipboardExpand all lines: docs/models/index.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,16 @@ For lower latency, using `reasoning.effort="none"` with `gpt-5.4` is recommended
73
73
74
74
If you pass a non–GPT-5 model name without custom `model_settings`, the SDK reverts to generic `ModelSettings` compatible with any model.
75
75
76
+
### Responses-only tool search features
77
+
78
+
The following tool features are supported only with OpenAI Responses models:
79
+
80
+
-[`ToolSearchTool`][agents.tool.ToolSearchTool]
81
+
-[`tool_namespace()`][agents.tool.tool_namespace]
82
+
-`@function_tool(defer_loading=True)` and other deferred-loading Responses tool surfaces
83
+
84
+
These features are rejected on Chat Completions models and on non-Responses backends. When you use deferred-loading tools, add `ToolSearchTool()` to the agent and let the model load tools through `auto` or `required` tool choice instead of forcing bare namespace names or deferred-only function names. See [Tools](../tools.md#hosted-tool-search) for the setup details and current constraints.
85
+
76
86
### Responses WebSocket transport
77
87
78
88
By default, OpenAI Responses API requests use HTTP transport. You can opt in to websocket transport when using OpenAI-backed models.
Copy file name to clipboardExpand all lines: docs/results.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,12 +63,15 @@ Unlike the JavaScript SDK, Python does not expose a separate `output` property f
63
63
64
64
-[`MessageOutputItem`][agents.items.MessageOutputItem] for assistant messages
65
65
-[`ReasoningItem`][agents.items.ReasoningItem] for reasoning items
66
+
-[`ToolSearchCallItem`][agents.items.ToolSearchCallItem] and [`ToolSearchOutputItem`][agents.items.ToolSearchOutputItem] for Responses tool search requests and loaded tool-search results
66
67
-[`ToolCallItem`][agents.items.ToolCallItem] and [`ToolCallOutputItem`][agents.items.ToolCallOutputItem] for tool calls and their results
67
68
-[`ToolApprovalItem`][agents.items.ToolApprovalItem] for tool calls that paused for approval
68
69
-[`HandoffCallItem`][agents.items.HandoffCallItem] and [`HandoffOutputItem`][agents.items.HandoffOutputItem] for handoff requests and completed transfers
69
70
70
71
Choose `new_items` over `to_input_list()` whenever you need agent associations, tool outputs, handoff boundaries, or approval boundaries.
71
72
73
+
When you use hosted tool search, inspect `ToolSearchCallItem.raw_item` to see the search request the model emitted, and `ToolSearchOutputItem.raw_item` to see which namespaces, functions, or hosted MCP servers were loaded for that turn.
Copy file name to clipboardExpand all lines: docs/streaming.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,6 +65,8 @@ For a full pause/resume walkthrough, see the [human-in-the-loop guide](human_in_
65
65
-`handoff_requested`
66
66
-`handoff_occured`
67
67
-`tool_called`
68
+
-`tool_search_called`
69
+
-`tool_search_output_created`
68
70
-`tool_output`
69
71
-`reasoning_item_created`
70
72
-`mcp_approval_requested`
@@ -73,6 +75,8 @@ For a full pause/resume walkthrough, see the [human-in-the-loop guide](human_in_
73
75
74
76
`handoff_occured` is intentionally misspelled for backward compatibility.
75
77
78
+
When you use hosted tool search, `tool_search_called` is emitted when the model issues a tool-search request and `tool_search_output_created` is emitted when the Responses API returns the loaded subset.
79
+
76
80
For example, this will ignore raw events and stream updates to the user.
| Defer large tool surfaces until runtime with tool search |[Hosted tool search](#hosted-tool-search)|
18
19
| Run tools in your own process or environment |[Local runtime tools](#local-runtime-tools)|
19
20
| Wrap Python functions as tools |[Function tools](#function-tools)|
20
21
| Let one agent call another without a handoff |[Agents as tools](#agents-as-tools)|
@@ -29,6 +30,7 @@ OpenAI offers a few built-in tools when using the [`OpenAIResponsesModel`][agent
29
30
- The [`CodeInterpreterTool`][agents.tool.CodeInterpreterTool] lets the LLM execute code in a sandboxed environment.
30
31
- The [`HostedMCPTool`][agents.tool.HostedMCPTool] exposes a remote MCP server's tools to the model.
31
32
- The [`ImageGenerationTool`][agents.tool.ImageGenerationTool] generates images from a prompt.
33
+
- The [`ToolSearchTool`][agents.tool.ToolSearchTool] lets the model load deferred tools, namespaces, or hosted MCP servers on demand.
32
34
33
35
Advanced hosted search options:
34
36
@@ -54,6 +56,69 @@ async def main():
54
56
print(result.final_output)
55
57
```
56
58
59
+
### Hosted tool search
60
+
61
+
Tool search lets OpenAI Responses models defer large tool surfaces until runtime, so the model loads only the subset it needs for the current turn. This is useful when you have many function tools, namespace groups, or hosted MCP servers and want to reduce tool-schema tokens without exposing every tool up front.
62
+
63
+
Start with hosted tool search when the candidate tools are already known when you build the agent. If your application needs to decide what to load dynamically, the Responses API also supports client-executed tool search, but the standard `Runner` does not auto-execute that mode.
64
+
65
+
```python
66
+
from typing import Annotated
67
+
68
+
from agents import Agent, Runner, ToolSearchTool, function_tool, tool_namespace
69
+
70
+
71
+
@function_tool(defer_loading=True)
72
+
defget_customer_profile(
73
+
customer_id: Annotated[str, "The customer ID to look up."],
74
+
) -> str:
75
+
"""Fetch a CRM customer profile."""
76
+
returnf"profile for {customer_id}"
77
+
78
+
79
+
@function_tool(defer_loading=True)
80
+
deflist_open_orders(
81
+
customer_id: Annotated[str, "The customer ID to look up."],
82
+
) -> str:
83
+
"""List open orders for a customer."""
84
+
returnf"open orders for {customer_id}"
85
+
86
+
87
+
crm_tools = tool_namespace(
88
+
name="crm",
89
+
description="CRM tools for customer lookups.",
90
+
tools=[get_customer_profile, list_open_orders],
91
+
)
92
+
93
+
94
+
agent = Agent(
95
+
name="Operations assistant",
96
+
model="gpt-5.4",
97
+
instructions="Load the crm namespace before using CRM tools.",
98
+
tools=[*crm_tools, ToolSearchTool()],
99
+
)
100
+
101
+
result =await Runner.run(agent, "Look up customer_42 and list their open orders.")
102
+
print(result.final_output)
103
+
```
104
+
105
+
What to know:
106
+
107
+
- Hosted tool search is available only with OpenAI Responses models. The current Python SDK support depends on `openai>=2.25.0`.
108
+
- Add exactly one `ToolSearchTool()` when you configure deferred-loading surfaces on an agent.
109
+
- Searchable surfaces include `@function_tool(defer_loading=True)`, `tool_namespace(name=..., description=..., tools=[...])`, and `HostedMCPTool(tool_config={..., "defer_loading": True})`.
110
+
- Deferred-loading function tools must be paired with `ToolSearchTool()`. Namespace-only setups may also use `ToolSearchTool()` to let the model load the right group on demand.
111
+
-`tool_namespace()` groups `FunctionTool` instances under a shared namespace name and description. This is usually the best fit when you have many related tools, such as `crm`, `billing`, or `shipping`.
112
+
- OpenAI's official best-practice guidance is [Use namespaces where possible](https://developers.openai.com/api/docs/guides/tools-tool-search#use-namespaces-where-possible).
113
+
- Prefer namespaces or hosted MCP servers over many individually deferred functions when possible. They usually give the model a better high-level search surface and better token savings.
114
+
- Namespaces can mix immediate and deferred tools. Tools without `defer_loading=True` remain callable immediately, while deferred tools in the same namespace are loaded through tool search.
115
+
- As a rule of thumb, keep each namespace fairly small, ideally fewer than 10 functions.
116
+
- Named `tool_choice` cannot target bare namespace names or deferred-only tools. Prefer `auto`, `required`, or a real top-level callable tool name.
117
+
-`ToolSearchTool(execution="client")` is for manual Responses orchestration. If the model emits a client-executed `tool_search_call`, the standard `Runner` raises instead of executing it for you.
118
+
- Tool search activity appears in [`RunResult.new_items`](results.md#new-items) and in [`RunItemStreamEvent`](streaming.md#run-item-event-names) with dedicated item and event types.
119
+
- See `examples/tools/tool_search.py` for complete runnable examples covering both namespaced loading and top-level deferred tools.
120
+
- Official platform guide: [Tool search](https://developers.openai.com/api/docs/guides/tools-tool-search).
121
+
57
122
### Hosted container shell + skills
58
123
59
124
`ShellTool` also supports OpenAI-hosted container execution. Use this mode when you want the model to run shell commands in a managed container instead of your local runtime.
@@ -168,6 +233,8 @@ You can use any Python function as a tool. The Agents SDK will setup the tool au
168
233
169
234
We use Python's `inspect` module to extract the function signature, along with [`griffe`](https://mkdocstrings.github.io/griffe/) to parse docstrings and `pydantic` for schema creation.
170
235
236
+
When you are using OpenAI Responses models, `@function_tool(defer_loading=True)` hides a function tool until `ToolSearchTool()` loads it. You can also group related function tools with [`tool_namespace()`][agents.tool.tool_namespace]. See [Hosted tool search](#hosted-tool-search) for the full setup and constraints.
0 commit comments