Ollama Tool Calling
Ollama tool calling lets a model ask your code to run a function, such as fetching the weather or querying a database. The model never runs code itself; it returns the function name and arguments, and you decide whether to execute it.
How the Tool Loop Works
- You send messages plus a list of tool definitions.
- The model replies with tool_calls instead of (or as well as) text.
- Your code runs the function with the given arguments.
- You append a message with role "tool" containing the result.
- You call chat again; the model writes the final answer.
Tool Calling in Python
The Python library can build the tool schema from a normal function with type hints and a docstring. Use a model that supports tools (look for the "tools" tag, e.g. llama3.1, qwen3).
from ollama import chat
def get_temperature(city: str) -> str:
"""Get the current temperature for a city.
Args:
city: Name of the city
"""
return {"Lahore": "31°C", "London": "12°C"}.get(city, "unknown")
messages = [{"role": "user", "content": "How warm is it in Lahore right now?"}]
resp = chat(model="qwen3", messages=messages, tools=[get_temperature])
for call in resp.message.tool_calls or []:
result = get_temperature(**call.function.arguments)
messages.append(resp.message)
messages.append({"role": "tool", "content": result, "tool_name": call.function.name})
final = chat(model="qwen3", messages=messages)
print(final.message.content)It's currently 31°C in Lahore. (Sample response.)
What the Model Returns
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{ "function": { "name": "get_temperature", "arguments": { "city": "Lahore" } } }
]
}(arguments arrive as an object, ready to pass to your function)
Safety Rules
- Validate arguments before using them; treat them like user input.
- Only expose tools that are safe for the model to trigger.
- Ask for human confirmation before destructive actions (delete, pay, send email).
- Limit the number of loop iterations to avoid endless tool calls.
Frequently asked questions
Which Ollama models support tool calling?
Models tagged "tools" on ollama.com, such as llama3.1, qwen3 and mistral variants at the time of writing.
Does Ollama execute the function for me?
No. It only returns the requested call. Your code runs it and sends back the result.
Can I use tool calling via the OpenAI-compatible endpoint?
Yes, /v1/chat/completions accepts the OpenAI tools format.