Page cover

Function Calling

Function calling lets models use APIs or functions to perform tasks, get real-time data, and respond intelligently to user input.

Function (tool) calling allows models to invoke predefined functions and APIs so they can take actions, fetch real-time data, and interact with external systems. This is useful for assistants, chat applications, and agentic systems that must act on user input.

Tools or functions expose specific capabilities through a name, description, and parameter schema. When tools are included in a request, the model can decide when to call them.

Tool Calls

A tool call is produced when the model determines that using one of the available tools is required. Here are key parameters to pass when calling the model related to function/tool calling:

  • tools Defines the functions the model can call, including their names, descriptions, and parameter schemas.

  • tool_choice Controls whether the model should decide when to call a tool. The supported value is auto, which lets the model choose based on the prompt

Tool Call Outputs

After your system executes the tool with the model-provided arguments, the result is returned to the model to generate the final response.

  • tool_callsContains the list of tool calls returned by the model. Each entry includes the tool name, the arguments the model generated, and an ID used to link your tool output back to the call.

  • function.name The name of the tool or function the model selected.

  • function.arguments A JSON string with the arguments the model generated for the tool.

  • id Unique identifier for the tool call, used to match tool outputs to the corresponding request.


Example: Function Calling with Custom Python Functions

This example demonstrates how to use function calling with an OpenAI-compatible model to fetch real-time financial data using the yfinance library. The model can retrieve stock prices, CEO names, and business summaries for publicly traded companies by invoking custom Python functions.

Before running the example, make sure the following Python packages are installed:

  • httpx is an HTTP client for Python that provides asynchronous support.

  • The [http2] extra enables HTTP/2 support, which improves efficiency in communication with APIs.

Expand for Example Code

Initializing the Client - Optimizing with Httpx

  • http_client=httpx.Client(http2=True):

    • Configures the HTTP client with HTTP/2 support for faster, more efficient communication.

Custom functions for stock research

Register the functions as tools

Step 5: Creating the Initial Model Call (Function Calling)

By setting tool_choice="auto", the model will decide whether to call a tool and which to call, or answer directly.

At this stage, the model responds either with:

  • a function/tool call including arguments, or

  • a normal text response (if no function is needed).

Handling Tool Calls and Returning a Response

After the user prompt is sent, the model may respond with one or more tool calls. This step processes those tool calls, executes the corresponding functions locally, and sends the results back to the model to complete the conversation.

Example Output

Last updated