Tool calling example may have a glitch - seems to only record the result of the last tool call #242

Open
opened 2026-02-15 16:29:05 -05:00 by yindo · 1 comment
Owner

Originally created by @janspoerer on GitHub (Mar 18, 2025).

The tool calling example in /examples/tools.py seems to only record the last tool call.

Am I seeing this correctly? If so, please let me know; I'll make a PR.

if response.message.tool_calls:
  # There may be multiple tool calls in the response
  for tool in response.message.tool_calls:
    # Ensure the function is available, and then call it
    if function_to_call := available_functions.get(tool.function.name):
      print('Calling function:', tool.function.name)
      print('Arguments:', tool.function.arguments)
      output = function_to_call(**tool.function.arguments) ########### OVERWRITE PREVIOUS TOOL CALLS #############
      print('Function output:', output)
    else:
      print('Function', tool.function.name, 'not found')

# Only needed to chat with the model using the tool call results
if response.message.tool_calls:
  # Add the function response to messages for the model to use
  messages.append(response.message)
  messages.append({'role': 'tool', 'content': str(output), 'name': tool.function.name})

  # Get final response from model with function outputs
  final_response = chat('llama3.1', messages=messages)
  print('Final response:', final_response.message.content)
Originally created by @janspoerer on GitHub (Mar 18, 2025). The tool calling example in [/examples/tools.py](https://github.com/ollama/ollama-python/blob/main/examples/tools.py) seems to only record the last tool call. Am I seeing this correctly? If so, please let me know; I'll make a PR. ``` if response.message.tool_calls: # There may be multiple tool calls in the response for tool in response.message.tool_calls: # Ensure the function is available, and then call it if function_to_call := available_functions.get(tool.function.name): print('Calling function:', tool.function.name) print('Arguments:', tool.function.arguments) output = function_to_call(**tool.function.arguments) ########### OVERWRITE PREVIOUS TOOL CALLS ############# print('Function output:', output) else: print('Function', tool.function.name, 'not found') # Only needed to chat with the model using the tool call results if response.message.tool_calls: # Add the function response to messages for the model to use messages.append(response.message) messages.append({'role': 'tool', 'content': str(output), 'name': tool.function.name}) # Get final response from model with function outputs final_response = chat('llama3.1', messages=messages) print('Final response:', final_response.message.content) ```
Author
Owner

@xuzexin-hz commented on GitHub (May 2, 2025):

No problem. You can try: What is three plus one, then plus two? The result is 6. hahaha
I think the actual tool call should be like this:

Image

@xuzexin-hz commented on GitHub (May 2, 2025): No problem. You can try: What is three plus one, then plus two? The result is 6. hahaha I think the actual tool call should be like this: ![Image](https://github.com/user-attachments/assets/cc4758e3-a93d-4746-8cf4-8d1b8b2aeecf)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#242