Fetching data
Retrieve current weather, search a knowledge base, or fetch records from an API before composing an answer.
AI & ML · Const
Function calling lets OpenAI models request application code or external services through declared function tools. Your application executes the requested calls, returns their outputs, and lets the model continue or answer.
Function calling lets an OpenAI model request application code or an external service through a declared function schema. The application receives one or more calls, validates and executes their JSON arguments, then submits each result so the model can continue with a final answer or additional calls.
tools: [{ type: "function", name, description, parameters, strict }]| Name | Type | Description |
|---|---|---|
tools | array | Array of callable function tool definitions supplied with an API request. |
name | string | Function name selected by the model. |
arguments | string | JSON-encoded arguments generated for the selected function. |
parameters | object | JSON Schema describing the function's input arguments. |
strict | boolean | Whether strict mode is enforced for the function call. |
A model response containing zero or more function calls; after tool outputs are submitted, the model returns a final response or additional calls.
import OpenAI from "openai";
const client = new OpenAI();
const tools = [{
type: "function",
name: "get_weather",
description: "Get the current weather for a location",
parameters: {
type: "object",
properties: { location: { type: "string" } },
required: ["location"],
additionalProperties: false
},
strict: true
}];
let response = await client.responses.create({
model: "gpt-4.1",
input: "What is the weather in Paris?",
tools
});
for (const item of response.output) {
if (item.type === "function_call") {
const args = JSON.parse(item.arguments);
const result = await getWeather(args.location);
response = await client.responses.create({
model: "gpt-4.1",
previous_response_id: response.id,
input: [{ type: "function_call_output", call_id: item.call_id, output: JSON.stringify(result) }]
});
}
}Defines a function tool, lets the model choose it, executes the application-side implementation, and sends the result back for a final response.
Retrieve current weather, search a knowledge base, or fetch records from an API before composing an answer.
Submit forms, invoke APIs, modify application state, or hand off an agent workflow.
Function calling is a multi-step exchange: provide tools, receive a tool call, execute it in application code, submit the tool output, and receive a final response or more calls. The model selects calls from the available schemas.
Follow-up: How should a loop handle a response containing several calls? Iterate over all function-call items, execute each safely, submit each corresponding output, and continue until the model returns a non-call response.
A function definition uses a function type, a name, a usage description, a JSON Schema parameter definition, and a strictness setting. The schema tells the model which arguments the application expects.
Follow-up: Why include a detailed description? It guides when and how the model should use the function, improving tool selection and argument generation.
The model does not execute application code itself. Your application parses the model's JSON-encoded arguments, performs the operation, and sends the result back using the tool call's identifier.
Follow-up: What should production code do before execution? Validate arguments against the expected schema and apply normal authorization, error handling, and side-effect controls.
toolsfunction_callfunction_call_outputtool_callsparallel_tool_callsStructured Outputs