Function calling lets a model choose structured tool calls that your application can execute. The model does not run the function itself. It returns the function name and arguments, your code runs the function, and you send the result back to the model.
Use function calling when the model needs live data, application actions, database lookups, or deterministic calculations.
Define tools with the OpenAI-compatible tools array:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["VENICE_API_KEY"],
base_url="https://api.venice.ai/api/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, such as San Francisco, CA",
}
},
"required": ["location"],
},
},
}
]
response = client.chat.completions.create(
model="zai-org-glm-5",
messages=[{"role": "user", "content": "What is the weather in San Francisco?"}],
tools=tools,
)
print(response.choices[0].message.tool_calls)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.VENICE_API_KEY,
baseURL: "https://api.venice.ai/api/v1",
});
const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "Get the current weather in a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and state, such as San Francisco, CA",
},
},
required: ["location"],
},
},
},
];
const response = await client.chat.completions.create({
model: "zai-org-glm-5",
messages: [{ role: "user", content: "What is the weather in San Francisco?" }],
tools,
});
console.log(response.choices[0].message.tool_calls);
curl https://api.venice.ai/api/v1/chat/completions \
-H "Authorization: Bearer $VENICE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "zai-org-glm-5",
"messages": [
{"role": "user", "content": "What is the weather in San Francisco?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, such as San Francisco, CA"
}
},
"required": ["location"]
}
}
}
]
}'
When the model chooses a tool, inspect message.tool_calls, parse the arguments, execute your application function, then send the result back as a tool message.
import json
message = response.choices[0].message
tool_call = message.tool_calls[0]
arguments = json.loads(tool_call.function.arguments)
weather = get_weather(arguments["location"])
follow_up = client.chat.completions.create(
model="zai-org-glm-5",
messages=[
{"role": "user", "content": "What is the weather in San Francisco?"},
message.model_dump(),
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(weather),
},
],
tools=tools,
)
print(follow_up.choices[0].message.content)
Choose a Model
Function calling support is model-specific. Use the Text Models page or the Models API to find models with supportsFunctionCalling.
Treat tool arguments as untrusted input. Validate arguments before using them in database queries, shell commands, payments, or other side-effecting operations.
Design Tips
- Keep tool names and descriptions short and literal.
- Use JSON Schema to make valid arguments easy for the model to produce.
- Prefer narrow tools with clear inputs over one broad tool with many optional behaviors.
- Return concise tool results so the final answer has enough context without wasting tokens.