Sub topic 3
Declaring built-in tools in a request
This chapter assumes you can call a chat or responses API with an API key, read JSON request and response bodies, and have used function calling at least once. It covers how to write the tools list for built-in tools, what the required fields are, and the errors that appear most often.
What a declaration is
A built-in tool is a tool the provider runs for you. You turn it on by listing it in the request. For a function tool, you describe the function and your own code runs it. For a built-in tool, you name a tool type, and the provider already knows what it does.
That difference shows up in the shape of the declaration:
- A function tool carries a name, a description, and a JSON Schema for its arguments. The model reads these to decide how to call it.
- A built-in tool carries a
typestring that selects the tool, plus optional settings. You write no schema, because the provider defines the tool's inputs.
Both kinds can sit in the same list.
tools = [
{"type": "web_search"},
{
"type": "function",
"name": "get_order_status",
"description": "Look up an order by its ID.",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
},
},
]The first entry is a built-in tool. The second is a function tool, whose type is "function".
Declaring tools with the Responses API
In the OpenAI Responses API, tools is a list on client.responses.create. Each built-in tool has its own type and its own settings.
Web search
The smallest declaration is the type alone.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "web_search"}],
input="What are the most recent stable releases of Python?",
)
print(response.output_text)The provider decides whether to search, runs the search, and folds the results into one final answer. Your code makes one call and gets one response.
Code interpreter
The code interpreter needs a container setting, because the code runs in a sandbox that must live somewhere. Passing {"type": "auto"} asks the provider to create a container for this request.
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "code_interpreter", "container": {"type": "auto"}}],
instructions="When a question involves arithmetic, write and run code to answer it.",
input="What is the sum of the squares of the integers from 1 to 1000?",
)
print(response.output_text)Note that container is a required part of this declaration. Leaving it out is an error in the current API, and it is a common one for people who copy the web search example and change only the type.
File search
File search points at documents you indexed earlier, so its declaration refers to stored data by ID.
vector_store = client.vector_stores.create(name="company-handbook")
with open("handbook.pdf", "rb") as f:
client.vector_stores.files.upload_and_poll(
vector_store_id=vector_store.id,
file=f,
)
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids": [vector_store.id],
"max_num_results": 3,
}
],
input="How many vacation days do new employees get?",
)
print(response.output_text)Two fields matter here. vector_store_ids is required and names the indexes to search. max_num_results is optional and caps how many chunks of text come back. Later chapters cover how to prepare the store and tune retrieval. Here, the point is that the declaration carries a reference, so the store must exist and finish indexing before the request.
Declaring a built-in tool with Anthropic
Anthropic's Messages API also takes a tools list, but a built-in tool there has a versioned type and a name.
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 3,
}
],
messages=[
{"role": "user", "content": "What are the latest developments in solid-state batteries?"}
],
)
for block in message.content:
if block.type == "text":
print(block.text)Three differences from the OpenAI form are worth noting:
- The
typeincludes a date,web_search_20250305. The date pins the tool's behavior to a version. When the provider releases a new version, you change the string on purpose. - A
namefield is required. It identifies the tool in the response. max_useslimits how many searches one request may perform.
Side-by-side comparison
| Part of the declaration | OpenAI Responses API | Anthropic Messages API |
|---|---|---|
| Where the list goes | `tools` argument | `tools` argument |
| How a built-in is selected | `type`, such as `web_search` | Versioned `type`, such as `web_search_20250305` |
| Name field | Not used | Required (`name`) |
| Per-request usage cap | Set through tool options or other limits | `max_uses` |
| Tool settings | Fields beside `type` | Fields beside `type` |
Check each provider's documentation for the current type strings. Names and versions change, and a type that worked last quarter may be renamed or retired.
How a declaration flows through a request
Validation happens before the model runs. That is why a bad declaration fails fast with an error, while a good declaration that the model chooses not to use returns a normal answer with no tool call.
Mixing built-in and function tools
You can list built-in tools and function tools together. The model sees all of them. The difference is who acts next:
- If the model calls a built-in tool, the provider runs it and continues. Your code sees only the final response.
- If the model calls a function tool, the response ends with a function call item. Your code must run the function and send the result back in a second request.
A request that mixes both can therefore end in either state. Write your handler to check the output for function calls before treating the response as final.
def needs_client_work(response):
return any(item.type == "function_call" for item in response.output)If needs_client_work returns True, run your function, append the result, and call the API again. If it returns False, the response is complete.
Common errors
- Wrong or retired type string. The API rejects the request with an invalid tool type error. Copy the string from current documentation, and pin versioned names in one constant.
- Missing required setting. Code interpreter without
container, or file search withoutvector_store_ids, fails validation. - Referencing a store that is not ready. If the vector store has files still processing, searches may return nothing. Use the polling helper shown above,
upload_and_poll, before sending the request. - Model not supporting the tool. Not every model supports every built-in tool. If a declaration is valid but rejected for a given model, try a supported model or check the availability table in the provider's docs.
- Assuming the tool will run. Declaring a tool makes it available. It does not force it. The model may answer without searching, which matters when you need fresh data. The chapter on steering tool use covers how to require a call.
- Using the wrong name in the wrong provider. An Anthropic declaration without
name, or an OpenAI declaration with an unneeded versioned type, will fail or be ignored.
Keeping declarations manageable
As requests grow, build the list from named pieces so each tool is easy to find and change.
WEB_SEARCH = {"type": "web_search"}
CODE = {"type": "code_interpreter", "container": {"type": "auto"}}
def file_search(store_id, limit=3):
return {
"type": "file_search",
"vector_store_ids": [store_id],
"max_num_results": limit,
}
def build_tools(store_id=None, allow_web=True):
tools = [CODE]
if allow_web:
tools.append(WEB_SEARCH)
if store_id:
tools.append(file_search(store_id))
return toolsThis keeps one place to update when a type string changes, and lets you turn tools on or off per request. Only declare the tools a request needs. Each extra tool adds to the prompt the model must read, and each one widens what the model can reach.
Exercise
You copy the web search declaration {"type": "code_interpreter"} into a request to run Python, and the API returns a validation error. What is missing, and why is it required?
Show answer
The declaration needs a container setting, for example "container": {"type": "auto"}. The code runs in a sandbox, and the provider needs to know which container to use or to create. With auto, the provider creates one for the request.
Exercise
A request lists both {"type": "web_search"} and a function tool. The response comes back with an item of type function_call. Who must act next, and what happens to the web search?
Show answer
Your code must act: run the function and send its result back in a follow-up request. Web search is unaffected. If the model used it, the provider already ran it on the server side. Only the function call is waiting on you.