A language model can choose a calculation without being responsible for executing it. With tool calling, an Ollama model requests a named function, the application computes its result, and the model uses that result in its reply.
A tool definition describes the function's arguments as a JSON schema. The returned tool_calls field carries the requested name and arguments, while the application retains control over which functions can run. Model output must never become an arbitrary command or executable code.
Python 3 and a running local Ollama server with qwen2.5:0.5b already installed are required. The Python standard library can handle both HTTP requests and JSON, so no client package or API key is needed for the local connection.
Steps to use tool calling with the Ollama API:
- Create tool_chat.py with the addition tool's argument schema.
$ cat > tool_chat.py <<'PY' import json from pathlib import Path from urllib.request import Request, urlopen model = "qwen2.5:0.5b" messages = [{ "role": "user", "content": "Use add_numbers to add 17 and 25. Answer with the result." }] tools = [{ "type": "function", "function": { "name": "add_numbers", "description": "Add two integers and return their sum.", "parameters": { "type": "object", "properties": {"a": {"type": "integer"}, "b": {"type": "integer"}}, "required": ["a", "b"], "additionalProperties": False } } }] PY
The model must support tools. qwen2.5:0.5b keeps the arithmetic task small; a model that answers directly instead of returning a tool call will stop at the missing-call check. The file creation replaces any existing tool_chat.py in the current directory.
Related: How to pull an Ollama model - Append the function that sends non-streaming chat requests.
$ cat >> tool_chat.py <<'PY' def chat(payload): request = Request( "http://127.0.0.1:11434/api/chat", data=json.dumps(payload).encode(), headers={"Content-Type": "application/json"}, method="POST" ) with urlopen(request, timeout=180) as response: body = json.load(response) if body.get("done") is not True or body.get("done_reason") != "stop": raise RuntimeError("Chat response did not finish normally") return body PY
stream: false returns one JSON object rather than a stream of fragments. HTTP errors and connection failures raise an exception instead of being treated as a reply.
Related: How to start the Ollama server - Restrict tool execution to add_numbers with two integer arguments.
$ cat >> tool_chat.py <<'PY' def execute_tool(name, arguments): if name != "add_numbers": raise ValueError("Unknown tool name") if not isinstance(arguments, dict) or set(arguments) != {"a", "b"}: raise ValueError("Expected only a and b") if any(type(value) is not int for value in arguments.values()): raise ValueError("Arguments must be integers") return arguments["a"] + arguments["b"] PY
The schema guides the model but does not enforce application permissions. The dispatcher rejects unknown names, missing or extra arguments, strings, and booleans before the calculation. It never evaluates model-supplied code.
- Append the first request and its tool-result message.
$ cat >> tool_chat.py <<'PY' first_request = { "model": model, "messages": messages, "tools": tools, "stream": False, "options": {"temperature": 0} } first = chat(first_request) assistant = first["message"] calls = assistant.get("tool_calls", []) if assistant.get("role") != "assistant" or len(calls) != 1: raise RuntimeError("Expected exactly one assistant tool call") function = calls[0]["function"] result = execute_tool(function["name"], function["arguments"]) tool_message = { "role": "tool", "tool_name": function["name"], "content": str(result) } history = messages + [assistant, tool_message] print(f"Tool: {function['name']} {json.dumps(function['arguments'], sort_keys=True)}") print(f"Result: {result}") PY
The history retains the entire returned assistant message, including tool_calls, before the tool message. The result comes from the returned arguments rather than an assumed answer.
- Append the follow-up request that returns the calculation to the model.
$ cat >> tool_chat.py <<'PY' final_request = { "model": model, "messages": history, "stream": False, "options": {"temperature": 0} } final = chat(final_request) reply = final["message"] if reply.get("role") != "assistant" or reply.get("tool_calls"): raise RuntimeError("Expected a final assistant answer") if not reply.get("content", "").strip(): raise RuntimeError("Assistant answer is empty") Path("tool-round-trip.json").write_text(json.dumps({ "first_request": first_request, "first_response": first, "result": result, "final_request": final_request, "final_response": final }, indent=2) + "\n") print(f"Answer: {reply['content']}") PY
The follow-up omits tools because only one calculation is needed. tool-round-trip.json retains both requests and responses and is replaced on each run.
- Run the completed tool-calling client.
$ python3 tool_chat.py Tool: add_numbers {"a": 17, "b": 25} Result: 42 Answer: The result of adding 17 and 25 is 42.
The wording of the answer can vary. A missing tool call, rejected arguments, or a failed request raises an error before a completed round trip is saved.
- Confirm the saved conversation contains the computed result and the final answer.
$ python3 - <<'PY' import json import re from pathlib import Path record = json.loads(Path("tool-round-trip.json").read_text()) assistant = record["first_response"]["message"] function = assistant["tool_calls"][0]["function"] assert function["name"] == "add_numbers" assert function["arguments"] == {"a": 17, "b": 25} total = function["arguments"]["a"] + function["arguments"]["b"] assert record["result"] == total history = record["final_request"]["messages"] assert history == record["first_request"]["messages"] + [ assistant, {"role": "tool", "tool_name": "add_numbers", "content": str(total)} ] final = record["final_response"] assert final["done"] is True and final["done_reason"] == "stop" answer = final["message"]["content"] assert str(total) in re.findall(r"\b\d+\b", answer) print(f"Tool result {total} is in the final answer: {answer}") PY Tool result 42 is in the final answer: The result of adding 17 and 25 is 42.