Function calling
Let Merius models call your functions. Define tools in the OpenAI format, receive tool_calls, run them, and feed the results back.
Give the model a set of functions it can call. You describe each function with a JSON schema; when the model decides to use one, it returns a structured tool call with arguments instead of plain text. You run the function and send the result back. Tools follow the OpenAI format, so existing tool definitions work unchanged.
Define the tools
Pass a tools array. Each tool has a name, a short description the model uses to decide when to
call it, and a JSON-schema definition of its parameters:
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, e.g. Berlin"}
},
"required": ["city"],
},
},
}
]
resp = client.chat.completions.create(
model="z-ai/glm-5.1",
messages=[{"role": "user", "content": "What is the weather in Berlin?"}],
tools=tools,
)const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "Get the current weather for a city.",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "City name, e.g. Berlin" },
},
required: ["city"],
},
},
},
];
const resp = await client.chat.completions.create({
model: "z-ai/glm-5.1",
messages: [{ role: "user", content: "What is the weather in Berlin?" }],
tools,
});Read the tool call
If the model decides to call a function, finish_reason is tool_calls and the call appears under
choices[0].message.tool_calls, with the function name and a JSON string of arguments:
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"Berlin\"}"
}
}
]Return the result
Run the function yourself, then append a message with role "tool", the matching tool_call_id,
and the result as content. Call the model again with the extended message list; it uses the result
to write the final answer.
messages.append(resp.choices[0].message) # the assistant tool call
messages.append({
"role": "tool",
"tool_call_id": "call_abc123",
"content": "18°C, clear",
})
final = client.chat.completions.create(model="z-ai/glm-5.1", messages=messages, tools=tools)
print(final.choices[0].message.content)Tool support depends on the model. The Models endpoint lists which models advertise tool use; sending tools to a model that does not support them returns a clear error.