Chapter 07 / 12 min read
The Tool-Calling Conversation: Preserving the Full Round Trip
Inspect ChatResponse, preserve assistant tool calls, append tool observations and retain final context.
The tool-calling conversation: preserving the full round trip
A common misunderstanding is that the model executes a Python function when it returns a tool call. It does not. The model proposes an action. The application executes it and returns the observation.
1. Start with a conversation
The project used a system instruction to keep the model focused on arithmetic and tool use:
messages = [
{
"role": "system",
"content": (
"Rule 1: You are a mathematical assistant. "
"Rule 2: Use the available tools for calculations. "
"Rule 3: Never invent missing numerical values. "
"Rule 4: If a required value is missing, ask the user "
"for clarification. "
"Rule 5: Only call a tool when all required arguments "
"are available."
),
},
{
"role": "user",
"content": (
"What is 250 multiplied by 40? "
"Use the multiplication tool to calculate the answer."
),
},
]
The system message describes behavior. The user message contains the request. The message list is the conversation context supplied to the model.
2. Ask Ollama for a response
response = send_message(messages=messages)
print(type(response.message))
print(response.message.model_dump(exclude_none=True))
The response message is an Ollama model object. The model_dump(exclude_none=True) call converts the Pydantic-backed response model into a Python dictionary while omitting fields whose values are None.
That is useful because the conversation history is represented as serializable message dictionaries. We preserve the full assistant message, including the tool_calls array:
messages.append(
response.message.model_dump(exclude_none=True)
)
Do not preserve only the function name and discard the assistant message. The model's tool-call request is part of the conversation protocol.
3. Execute each requested tool
import json
from tool_executor import execute_tool
if response.message.tool_calls:
for tool_call in response.message.tool_calls:
tool_name = tool_call.function.name
tool_arguments = tool_call.function.arguments
result = execute_tool(
tool_name=tool_name,
tool_arguments=tool_arguments,
)
messages.append({
"role": "tool",
"tool_name": tool_name,
"content": json.dumps(result),
})
else:
print(response.message.content)
The tool message carries the actual result back into the conversation. The role identifies it as tool output, tool_name identifies the function, and content contains the serialized result.
Why include tool_name when the function name may also appear in the JSON content? The message structure already has dedicated fields: the role and tool name identify the source of the observation, while content carries the result payload. Keep this metadata in its proper fields instead of hiding it inside an arbitrary text string.
4. Ask the model to continue
After appending the tool result, call Ollama again:
second_response = send_message(messages=messages)
print(second_response.message.content)
messages.append(
second_response.message.model_dump(exclude_none=True)
)
The observed final answer was:
The result of multiplying 250 by 40 is 10,000.
Now the model has seen the user request, its own tool-call message and the tool's result. It can formulate a natural-language answer and retain that answer as part of the conversation history.
5. Why append the final assistant message?
A context-management issue appears if we store only the tool result: the model's final assistant response is then missing from the conversation history.
The correct pattern is to append both:
- The assistant message that requested the tool.
- The tool message containing the execution result.
- The assistant message that communicates the final answer.
This allows later turns to refer to the prior exchange.
6. Handle multiple tool calls
The model may return more than one tool call in one response. In one run, the model requested multiplication and division; in another, it requested multiplication, division and addition.
The application therefore iterates over the tool-call collection:
for tool_call in response.message.tool_calls:
tool_name = tool_call.function.name
tool_arguments = tool_call.function.arguments
result = execute_tool(tool_name, tool_arguments)
messages.append({
"role": "tool",
"tool_name": tool_name,
"content": json.dumps(result),
})
The recorded three-operation run produced:
multiply_numbers(250, 40) -> 10000.0
divide_numbers(10000, 5) -> 2000.0
calculate_addition(2000, 100) -> 2100.0
A caution: the fact that a model returns multiple tool calls does not guarantee that those calls are independent or safe to run concurrently. If one operation depends on another's output, the application must not execute them as if they were independent. That becomes the central issue in the planning chapters.