tools parameter in the
request body of the
chat completions API. This
API is OpenAI-compatible, so you can use the OpenAI SDK without changes. Note
that you can’t use the responses API
for function calling.
To view the list of models that support function calling, see the
Supported models page. Function calling only works for models that
support it.
When to use function calling
Pass atools parameter in your chat completions request when you want the
model to call your code instead of only returning text. Typical uses include:
- Fetching data: Declare tools that read from APIs or databases (for example, weather, prices, or search). The model requests the data; your code runs the call and returns the result for the model to use in its reply.
- Performing actions: Declare tools that change state or trigger work (for example, update an app, start a workflow, or call another system). The model chooses the tool and arguments; your code executes the action.
How function calling works
Function calling follows this loop:- You declare tools in the chat completions API request. Each tool describes what the function does and which arguments it accepts.
- The model returns a response that includes
tool_callswhen it wants to use one. - Your code runs the function.
- For data-fetching tools, you send the result back in a follow-up request.
Declare tools
The first step is to declare the tools your model can use. The following example declares aget_weather tool and references the tool in the request:
tools property:
type: The type of tool. Currently, we only supportfunction.function: The function definition.name: The name that the model uses when calling the function.description: A description that helps the model decide when to call the function.parameters: A JSON Schema for the arguments.type: Must beobject.properties: A list of all argument names and types.required: A list of arguments the model must supply to the function.
Control tool use
Use thetool_choice parameter to control tool use. It accepts the following
values:
-
none: Don’t call any tool. -
auto(default): Let the model decide whether to call a tool or reply with a message. -
required: Require at least one tool call. -
A specific function: Force the model to always call a specific function, for
example:
Handle the model response
If the model chooses a tool, the response includestool_calls:
tool_calls and run the matching function in your code:
Return results to the model
For tools that fetch data, send another request with the conversation so far, including:- Prior messages
- The assistant message that returned
tool_calls - A
toolmessage for each result
tool message’s tool_call_id to the id of the matching tool call
in that assistant message.
tool_call_id must match an id from the preceding assistant
message. The API returns an HTTP 400 response if a tool_calls argument isn’t
JSON, a tool_call_id doesn’t match, or you omit any required tool reply.
Run the example
This walks through the sameget_weather example from above as a single,
runnable script.
If you haven’t already, create an API
key and
store it securely.
Then, send a request with the tools parameter to a model that supports
function calling:
- Python
- curl
Install the OpenAI SDK:Create a program to send a request specifying the available Run it and the
get_weather()
function:function-calling.py
get_weather() function should print the argument received:Troubleshooting
If your function calling request fails, try the following:- Confirm that function calling is supported by the model.
- Check if streaming is enabled. Depending on the model, streaming may interfere
with function calling. If you see incomplete or malformed tool-call output
while streaming, set
stream: falsefor that request.
Next steps
Find more information in:- The chat completions API reference documentation.
- OpenAI’s guide to Function calling.