Guides
Tool calls
Both models call tools in OpenAI’s format: you describe functions, the model asks for one, you run it and send back the result.
Checked 26 October 2026
The loop
- Send
tools: each one a function with a name, a description and JSON Schema parameters. - When the model wants one, the reply’s
finish_reasonistool_callsandmessage.tool_callslists them, with arguments as a JSON string. - Run each one, and add a
toolmessage with itstool_call_idand the result. - Send the conversation again. Repeat until
finish_reasonisstop.
import json
import os
from openai import OpenAI
client = OpenAI(
base_url="https://nokeep.ai/v1",
api_key=os.environ["NOKEEP_API_KEY"],
)
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Today's weather in a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
def get_weather(city):
return {"city": city, "high_c": 21, "sky": "clear"}
messages = [{"role": "user", "content": "Should I bring a jacket in Lisbon today?"}]
while True:
reply = client.chat.completions.create(model="deepseek-v4.1-flash-uncensored", messages=messages, tools=tools)
message = reply.choices[0].message
messages.append(message)
if not message.tool_calls:
print(message.content)
break
for call in message.tool_calls:
result = get_weather(**json.loads(call.function.arguments))
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})tool_choice
| Model | Takes |
|---|---|
deepseek-v4.1-flash-uncensored | auto, none, required, or a named function. Also parallel_tool_calls. |
glm-5.3-flash-uncensored | auto or none. It picks its own tools; anything else is 400 unsupported_tool_choice. |
Good to know
- Every
toolmessage needs thetool_call_idit answers, or the call is refused with400 invalid_messages. - Tool schemas travel with every request and count as input tokens.
- The models don’t run anything themselves. Your code runs the tools, so it decides what they can reach.