function calling

Enables GPT to call developer-defined functions with structured JSON arguments. Use tools array in modern API versions.

Syntax

openai-api
{ tools: [{ type: "function", function: { name, description, parameters } }] }

Example

openai-api
const response = await client.chat.completions.create({
  model: "gpt-4o",
  tools: [{
    type: "function",
    function: {
      name: "search_docs",
      description: "Search documentation",
      parameters: { type: "object", properties: { query: { type: "string" } }, required: ["query"] }
    }
  }],
  messages: [{ role: "user", content: "Find docs on useState" }]
});