tools parameter in the
request body. The inference API is OpenAI-compatible, so you can
use the OpenAI SDK without changes.
Function calling is enabled by default, but its availability
is model-dependent and will produce valid output only if the model is
pretrained to return tool-use responses. Function calling is supported on the
/v1/chat/completions endpoint only; it does not apply to /v1/responses,
which is used for image and video generation models. See Supported
models below.When to use function calling
You should use function calling when you want your LLM to:- Fetch data: Such as fetch weather data, stock prices, or news updates from a database. The model will call a function to get information, and then incorporate that data into its final response.
- Perform actions: Such as modify application states, invoke workflows, or call upon other AI systems. The model will call another tool to perform an action, effectively handing off the request after it determines what the user wants.
How function calling works
When you send an inference request to a model that supports function calling, you can specify which functions are available to the model using thetools
body parameter.
The tools parameter provides information that allows the LLM to understand:
- What each function can do
- How to call each function (the arguments it accepts/requires)
get_weather():
tools property:
type: Currently this is alwaysfunctionfunction: Definition of the functionname: The function name used by the LLM to call it. Must be at most 64 characters; the default character set is[a-zA-Z0-9_-], though some model parsers widen it to allow dotted or namespaced names (for example,my-tool.v2)description: A function description that helps the LLM understand when to use itparameters: Definition of the function parameterstype: Defines this as an object containing parametersproperties: Lists all possible function arguments and their typesrequired: Specifies which function arguments are required
tool_choice parameter:
-
none: The model won’t call any tool. -
auto(default): The model decides whether to call a tool or respond with a message. -
required: The model must call one or more tools. -
A specific-function object: Forces the model to call that named function,
for example:
tools. In this case, we expect the model to call
get_weather() and incorporate that information into its final response. So,
the initial completion response from above includes a tool_calls
parameter like this:
tool_calls body and execute the function as
appropriate. For example:
tool role. Append the assistant message that
contained the tool_calls, then one message per result with the tool role
and the matching tool_call_id:
tool message’s tool_call_id must match the id of a tool call in the
preceding assistant message. A request with a non-JSON tool_calls argument,
an unmatched tool_call_id, or only a partial set of tool replies is rejected
with a 400 error.
If the function is designed to perform an action, then you don’t need to
call the model again.
For detail about how to execute the function and feed the results back to the
model, see the OpenAI docs about handling function
calls.
The OpenAI function calling spec is compatible with multiple agent frameworks,
such as AutoGen,
CrewAI, and more.
Supported models
Function calling is model-dependent and will produce valid output only if the model is pretrained to return tool-use responses. Streaming (stream: true)
with function calling also varies by model. If you see incomplete or
malformed tool-call output while streaming, set stream: false for that
request.
Function calling only applies to text-generation models called through
/v1/chat/completions. It isn’t applicable to /v1/responses, since that
endpoint is used exclusively for image and video generation models, which
don’t accept a tools parameter.
See the supported models page for the current list of
models available on shared and dedicated endpoints, and which ones support
function calling.
Quickstart
This walks through the sameget_weather example from above as a single,
runnable script.
If you haven’t already, create an API
key in the Modular Console and export it:
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:tool_calls response and
send the function results back to the LLM as input, see the OpenAI docs about
handling function
calls.