Function calling
Function calling enables dynamic workflows by allowing the model to select and suggest function calls based on user input, which helps in building agentic workflows. By defining a set of functions, or tools, you provide context that lets the model recommend and fill in function arguments as needed.
How function calling works
Function calling enables adaptive workflows that leverage real-time data and structured outputs, creating more dynamic and responsive model interactions.
- Submit a Query with tools: Start by submitting a user query along with available tools defined in JSON Schema. This schema specifies parameters for each function.
- The model processes and suggests: The model interprets the query, assesses intent, and decides if it will respond conversationally or suggest function calls. If a function is called, it fills in the arguments based on the schema.
- Receive a model response: You’ll get a response from the model, which may include a function call suggestion. Execute the function with the provided arguments and return the result to the model for further interaction.
Supported models
Qwen3-32BQwen3.8Qwen3.8-MaxDeepSeek-V3.1DeepSeek-V4-flashDeepSeek-V4-proDeepSeek-V4.1-flashgemma-4-31B-itLlama-4-Maverick-17B-128E-InstructMeta-Llama-3.3-70B-InstructMiniMax-M2.5MiniMax-M2.7MiniMax-M3Kimi-K2.7-CodeKimi-K3gpt-oss-120bcoderMAGPiEGLM-5.2GLM-5.2-FastGLM-5.3GLM-5.3-FastGLM-5.3-Flash
To get better quality in tool calling requests with gpt-oss-120b, it is recommended to set the reasoning_effort to high
Meta recommends using Llama 70B-Instruct for applications that combine conversation and tool calling. Llama 8B-Instruct cannot reliably maintain a conversation alongside tool-calling definitions. It can be used for zero-shot tool calling, but tool instructions should be removed for regular conversations.
Example usage
The examples below describe each step of using function calling with an end-to-end example after the last step.
Step 1: Define the function schema
Define a JSON schema for your function. You will need to specify:
- The name of the function.
- A description of what it does.
- The parameters, their data types, and descriptions.
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Gets the current weather information for a given city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "Name of the city to get weather information for."
}
},
"required": ["city"]
}
}
}
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Gets the current weather information for a given city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "Name of the city to get weather information for."
}
},
"required": ["city"]
}
}
}
Step 2: Configure function calling in your request
When sending a request, include the function definition in the tools parameter and set tool_choice to the following:
auto: allows the model to choose between generating a message or calling a function. This is the default tool choice when the field is not specified.required: This forces the model to generate a function call. The model will then always select one or more function(s) to call.- To enforce a specific function call, set
tool_choice = {"type": "function", "function": {"name": "get_weather"}}. This ensures the model will only use the specified function.
The following code block shows a fake weather lookup that returns a random temperature between 20°C and 50°C. For accurate and real-time weather data, use a proper weather API.
import openai
import random
import json
# Initialize the client with the SCX.ai base URL and API key
client = openai.OpenAI(
base_url="https://api.scx.ai/v1",
api_key="your-scx-api-key"
)
MODEL = 'Meta-Llama-3.3-70B-Instruct'
def get_weather(city: str) -> dict:
"""
Fake weather lookup: returns a random temperature between 20°C and 50°C.
"""
temp = random.randint(20, 50)
return {
"city": city,
"temperature_celsius": temp
}
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of an location, the user shoud supply a location first",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["city"]
},
}
},
]
messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]
completion = client.chat.completions.create(
model=MODEL,
messages=messages,
tools=tools
)
print(completion)
import openai
import random
import json
# Initialize the client with the SCX.ai base URL and API key
client = openai.OpenAI(
base_url="https://api.scx.ai/v1",
api_key="your-scx-api-key"
)
MODEL = 'Meta-Llama-3.3-70B-Instruct'
def get_weather(city: str) -> dict:
"""
Fake weather lookup: returns a random temperature between 20°C and 50°C.
"""
temp = random.randint(20, 50)
return {
"city": city,
"temperature_celsius": temp
}
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of an location, the user shoud supply a location first",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["city"]
},
}
},
]
messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]
completion = client.chat.completions.create(
model=MODEL,
messages=messages,
tools=tools
)
print(completion)
Step 3: Handle tool calls
If the model chooses to call a function, you will find tool_calls in the response. Extract the function call details and execute the corresponding function with the provided parameters.
tool_call = completion.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
result = get_weather(args["city"])
tool_call = completion.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
result = get_weather(args["city"])
Step 4: Provide function results back to the model
Once you have computed the result, pass it back to the model to continue the conversation or confirm the output.
messages.append(completion.choices[0].message) # append model's function call message
messages.append({ # append result message
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result)
})
completion_2 = client.chat.completions.create(
model=MODEL,
messages=messages,
)
print(completion_2.choices[0].message.content)
messages.append(completion.choices[0].message) # append model's function call message
messages.append({ # append result message
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result)
})
completion_2 = client.chat.completions.create(
model=MODEL,
messages=messages,
)
print(completion_2.choices[0].message.content)
Step 5: Example output
An example output is shown below.
The current weather in Paris is 25 degrees Celsius.
The current weather in Paris is 25 degrees Celsius.
End-to-end example
The following code block shows a fake weather lookup that returns a random temperature between 20°C and 50°C. For accurate and real-time weather data, use a proper weather API.
import openai
import random
import json
# Define the OpenAI client
client = openai.OpenAI(
base_url="https://api.scx.ai/v1",
api_key="your-scx-api-key"
)
MODEL = 'Meta-Llama-3.3-70B-Instruct'
def get_weather(city: str) -> dict:
"""
Fake weather lookup: returns a random temperature between 20°C and 50°C.
"""
temp = random.randint(20, 50)
return {
"city": city,
"temperature_celsius": temp
}
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of an location, the user shoud supply a location first",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["city"]
},
}
},
]
messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]
completion = client.chat.completions.create(
model=MODEL,
messages=messages,
tools=tools
)
print(completion)
tool_call = completion.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
result = get_weather(args["city"])
messages.append({ # append model's function call message
"role": "assistant",
"content": None,
"tool_calls": [tool_call]
})
messages.append({ # append result message
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
completion_2 = client.chat.completions.create(
model=MODEL,
messages=messages,
)
print(completion_2.choices[0].message.content)
import openai
import random
import json
# Define the OpenAI client
client = openai.OpenAI(
base_url="https://api.scx.ai/v1",
api_key="your-scx-api-key"
)
MODEL = 'Meta-Llama-3.3-70B-Instruct'
def get_weather(city: str) -> dict:
"""
Fake weather lookup: returns a random temperature between 20°C and 50°C.
"""
temp = random.randint(20, 50)
return {
"city": city,
"temperature_celsius": temp
}
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of an location, the user shoud supply a location first",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["city"]
},
}
},
]
messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]
completion = client.chat.completions.create(
model=MODEL,
messages=messages,
tools=tools
)
print(completion)
tool_call = completion.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
result = get_weather(args["city"])
messages.append({ # append model's function call message
"role": "assistant",
"content": None,
"tool_calls": [tool_call]
})
messages.append({ # append result message
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
completion_2 = client.chat.completions.create(
model=MODEL,
messages=messages,
)
print(completion_2.choices[0].message.content)