DocumentaciónHerramientas
Construir
Herramientas
Describe tus funciones al modelo. Él decide cuándo llamar a una, tu código la ejecuta y le devuelve el resultado.
En esta página
El ciclo completo
Describes la función
Su nombre, lo que hace y sus parámetros, en JSON Schema.
El modelo pide la llamada
En lugar de un texto, devuelve tool_calls con los argumentos.
Tu código la ejecuta
El modelo no ejecuta nada por sí mismo. Tú decides lo que ocurre.
El modelo responde
Con el resultado devuelto en un mensaje tool, escribe su respuesta.
Ejemplo
Una función que indica si una factura está pagada. La respuesta mostrada es la de la primera llamada, cuando el modelo pide la función.
import json
tools = [
{
"type": "function",
"function": {
"name": "invoice_status",
"description": "Payment status of an invoice.",
"parameters": {
"type": "object",
"properties": {"number": {"type": "string"}},
"required": ["number"],
},
},
}
]
messages = [
{
"role": "user",
"content": "Has invoice 2026-114 been paid?",
}
]
first = client.chat.completions.create(
model="flash", messages=messages, tools=tools
)
call = first.choices[0].message.tool_calls[0]
args = json.loads(call.function.arguments)
# Run the function, then return its result with the call id.
result = {
"number": args["number"],
"status": "paid",
"paid_on": "2026-10-02",
}
messages.append(first.choices[0].message)
messages.append(
{
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
}
)
final = client.chat.completions.create(
model="flash", messages=messages, tools=tools
)
print(final.choices[0].message.content)const tools = [
{
type: "function",
function: {
name: "invoice_status",
description: "Payment status of an invoice.",
parameters: {
type: "object",
properties: { number: { type: "string" } },
required: ["number"],
},
},
},
]
const messages = [
{ role: "user", content: "Has invoice 2026-114 been paid?" },
]
const first = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
const call = first.choices[0].message.tool_calls[0]
const args = JSON.parse(call.function.arguments)
// Run the function, then return its result with the call id.
const result = {
number: args.number,
status: "paid",
paid_on: "2026-10-02",
}
messages.push(first.choices[0].message)
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
})
const final = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
console.log(final.choices[0].message.content)import type {
ChatCompletionMessageParam,
ChatCompletionTool,
} from "openai/resources"
const tools: ChatCompletionTool[] = [
{
type: "function",
function: {
name: "invoice_status",
description: "Payment status of an invoice.",
parameters: {
type: "object",
properties: { number: { type: "string" } },
required: ["number"],
},
},
},
]
const messages: ChatCompletionMessageParam[] = [
{ role: "user", content: "Has invoice 2026-114 been paid?" },
]
const first = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
const call = first.choices[0].message.tool_calls?.[0]
if (call?.type !== "function")
throw new Error("no function call")
const args = JSON.parse(call.function.arguments) as {
number: string
}
// Run the function, then return its result with the call id.
const result = {
number: args.number,
status: "paid",
paid_on: "2026-10-02",
}
messages.push(first.choices[0].message)
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
})
const final = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
console.log(final.choices[0].message.content)Respuesta
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_7f3a",
"type": "function",
"function": { "name": "invoice_status", "arguments": "{\"number\": \"2026-114\"}" }
}
]
}Conviene saber
- Una lista de herramientas vacía se elimina junto con tool_choice, como si no hubieras enviado ninguna.
- parallel_tool_calls permite al modelo pedir varias funciones a la vez.
- Comprueba los argumentos antes de actuar. El modelo puede equivocarse, tu código decide.