Chapter 06 / 14 min read
Tool Definitions and the Local Ollama Client
Pydantic-generated tool schemas, Qwen, Ollama chat calls, message formats and debugging.
Tool definitions and the local Ollama client
We now have calculator functions, a function registry and Pydantic schemas. The next question is: how does the model know which tools exist and what arguments they accept?
The application must send tool definitions with the conversation. The definitions describe the available operations; they do not themselves execute Python.
1. Construct the model-facing tool definitions
The first manual example described a multiplication tool as a dictionary:
multiply_numbers = {
"name": "multiply_numbers",
"description": "Multiplies two numbers and returns the result.",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "number",
"description": "The first number to multiply."
},
"b": {
"type": "number",
"description": "The second number to multiply."
}
},
"required": ["a", "b"]
}
}
This is readable, but it duplicates the contract already expressed in Pydantic. The project then replaced the hand-written parameter section with the generated schema:
from models.schemas import MultiplyInput
MULTIPLY_NUMBERS_TOOL = {
"name": "multiply_numbers",
"description": "Multiplies two numbers and returns the result.",
"parameters": MultiplyInput.model_json_schema(),
}
This is better because the field types, required properties and descriptions come from one source.
2. Define all calculator tools
from models.schemas import (
AdditionInput,
DivideInput,
MultiplyInput,
)
MULTIPLY_NUMBERS_TOOL = {
"name": "multiply_numbers",
"description": "Multiplies two numbers and returns the result.",
"parameters": MultiplyInput.model_json_schema(),
}
ADDITION_TOOL = {
"name": "calculate_addition",
"description": "Adds two numbers and returns the result.",
"parameters": AdditionInput.model_json_schema(),
}
DIVIDE_NUMBERS_TOOL = {
"name": "divide_numbers",
"description": "Divides two numbers and returns the result.",
"parameters": DivideInput.model_json_schema(),
}
TOOL_DEFINITIONS = [
MULTIPLY_NUMBERS_TOOL,
ADDITION_TOOL,
DIVIDE_NUMBERS_TOOL,
]
The original test printed each tool's name, description and generated parameter schema. For multiplication, the generated schema showed a and b as required number properties, with additionalProperties: false.
3. Why name and description matter
The name is the identifier the application will use to look up the registered function. The description explains what the operation does. The parameters explain what data the model must provide.
A clear name and description reduce ambiguity when multiple tools are available. The model chooses among the definitions; the Python application still checks that the returned name exists in its own registry.
4. Configure the Ollama model in one place
The client module introduced a central model constant:
import ollama
from tool_definitions import TOOL_DEFINITIONS
MODEL_NAME = "qwen2.5:7b"
Why keep the model name in one place? If we later switch from qwen2.5:7b to another local model, we should not have to search through the project and edit several functions.
The local Ollama environment used these models:
NAME SIZE
qwen2.5:7b 4.7 GB
llama3.2:latest 2.0 GB
5. Write a reusable send_message function
The client evolved into:
def send_message(
messages: list,
model: str = MODEL_NAME,
tools: list = TOOL_DEFINITIONS,
):
"""
Send messages and available tool definitions to Ollama.
"""
try:
response = ollama.chat(
model=model,
messages=messages,
tools=tools,
)
return response
except Exception as exc:
return {"error": str(exc)}
The function accepts the conversation history, model name and tool definitions. The defaults make the common path concise while allowing callers to override the model or tools.
One small correction from the early draft is worth noting: the first version accepted a model parameter but called Ollama with MODEL_NAME anyway. The corrected version passes model=model, so the argument actually controls the model used.
6. The message-format debugging exercise
An early test called the client with a list of strings:
response = send_message(
messages=["what is python in one sentence?"]
)
This produced an error:
dictionary update sequence element #0 has length 1; 2 is required
The issue was the message shape. Ollama expects messages to be structured with a role and content:
response = send_message(
messages=[
{
"role": "user",
"content": "what is python in one sentence?",
}
]
)
The corrected call returned an Ollama ChatResponse object. The exercise inspected:
print(type(response))
print(response.model)
print(response.message)
print(response.message.tool_calls)
The result was not a plain string. It was a structured response object containing model metadata and a message object.
7. A tool call is a structured response
When the user asked:
What is 250 multiplied by 40? Use the multiplication tool to calculate the answer.
The model returned a response whose message contained a tool call. The observed data looked like:
Message(
role='assistant',
content='',
tool_calls=[
ToolCall(
function=Function(
name='multiply_numbers',
arguments={'a': 250, 'b': 40}
)
)
]
)
At one point, the model returned an empty function name because the test call did not pass the intended tool definitions. This was a useful debugging lesson: inspect the actual tool-call object and verify that the client is sending the definitions you think it is sending.
8. What this layer is responsible for
The Ollama client should be a small integration boundary. It knows how to call the model. It should not contain the calculator implementation or decide whether an arbitrary tool name is safe to run.
The responsibilities remain separated:
ollama_client.py: send messages and tool definitions to Ollama.tool_definitions.py: describe available tools to the model.tool_registry.py: map names to Python functions.models/schemas.py: define argument contracts.tool_executor.py: validate and execute a requested tool.
That separation makes it easier to debug a failure: is it a model response issue, a schema issue, a lookup issue or an execution issue?