Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI & ML · Const

Function Calling

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.

How it works

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.

Syntax

tools: [{ type: "function", name, description, parameters, strict }]

Parameters

NameTypeDescription
toolsarrayArray of callable function tool definitions supplied with an API request.
namestringFunction name selected by the model.
argumentsstringJSON-encoded arguments generated for the selected function.
parametersobjectJSON Schema describing the function's input arguments.
strictbooleanWhether strict mode is enforced for the function call.

Returns

A model response containing zero or more function calls; after tool outputs are submitted, the model returns a final response or additional calls.

Key points

  • Treat function calling as an application–model conversation rather than a single completion.
  • Declare available functions in the request's tools collection.
  • Run the selected operation in your own application using the generated arguments.
  • Send execution results back in a follow-up request so the model can incorporate them.
  • Use the current tools parameter to expose callable functions.
  • Design response processing for zero, one, or multiple function calls.

Common mistakes

  • Handle every response as potentially containing multiple tool calls; do not assume exactly one.
  • A model-generated call is only a request: your application must run the function and provide its output.

Example: Function calling with the Responses API

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.

When to use it

Fetching data

Retrieve current weather, search a knowledge base, or fetch records from an API before composing an answer.

Taking action

Submit forms, invoke APIs, modify application state, or hand off an agent workflow.

Interview questions

QWhat is the function-calling flow?

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.

QWhat belongs in a function definition?

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.

QWho executes a requested function and how is its result returned?

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.

  • tools
  • function_call
  • function_call_output
  • tool_calls
  • parallel_tool_calls
  • Structured Outputs

Source: Function calling. Checked 2026-10-15.