[GH-ISSUE #13334] ministral template doesn't handle multiple tool calls #8807

Closed
opened 2026-04-12 21:35:23 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @rick-github on GitHub (Dec 5, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/13334

Originally assigned to: @pdevine on GitHub.

What is the issue?

This line in the template:

{{- if and (le (len (slice $.Messages $index)) 2) $.Tools }}[AVAILABLE_TOOLS]{{ $.Tools }}[/AVAILABLE_TOOLS]

adds tools to the user message, when the user message is last or second to last message in the message list. So for a completion call with

[
  system message (S)
  user message (U1)
  assistant message (A1)
  user message (U2)
]

the tools get attached to U2 and the model will generate a message (A2) with one or more tool calls. In the process of completing the tools calls, the client will update the message list with A2 and the results of the tool call(s) with a tool message.

[ S U1 A1 U2 A2 T1 ]

Since U2 is no longer the second to last or last message, the tools are not attached in subsequent calls. If the model thinks it needs to make another tool call in order to satisfy the instruction in U2, it cannot. As a concrete example:

[
    {
        "role": "user",
        "content": "what is 2.3 ^ 5.6 and 5.6 ^ 2.3? Now add them together."
    },
    {
        "role": "assistant",
        "tool_calls": [
            "function=Function(name='power', arguments={'x': 2.3, 'y': 5.6})",
            "function=Function(name='power', arguments={'x': 5.6, 'y': 2.3})"
        ]
    },
    {
        "role": "tool",
        "content": "106.0903498817264"
    },
    {
        "role": "tool",
        "content": "52.581438372010965"
    },
    {
        "role": "assistant",
        "content": "Here are the calculations:\n\n- \\( 2.3^{5.6} \\approx 106.0903 \\)\n- \\( 5.6^{2.3} \\approx 52.5814 \\)\n\nAdding them together:\n**106.0903 + 52.5814 = 158.6717**\n\nThe final result is **158.6717**."
    }
]

In contrast, if the user message has tools attached:

[
    {
        "role": "user",
        "content": "what is 2.3 ^ 5.6 and 5.6 ^ 2.3? Now add them together."
    },
    {
        "role": "assistant",
        "tool_calls": [
            "function=Function(name='power', arguments={'x': 2.3, 'y': 5.6})",
            "function=Function(name='power', arguments={'x': 5.6, 'y': 2.3})"
        ]
    },
    {
        "role": "tool",
        "content": "106.0903498817264"
    },
    {
        "role": "tool",
        "content": "52.581438372010965"
    },
    {
        "role": "assistant",
        "tool_calls": [
            "function=Function(name='add', arguments={'l': [106.0903498817264, 52.581438372010965]})"
        ]
    },
    {
        "role": "tool",
        "content": "158.67178825373736"
    },
    {
        "role": "assistant",
        "content": "The result of \\(2.3^{5.6}\\) is approximately **106.09**.\n\nThe result of \\(5.6^{2.3}\\) is approximately **52.58**.\n\nTheir sum is **158.67**."
    }
]

Suggested change to enable ministral to handle multiple tool calls:

--- Modelfile.orig	2025-12-05 02:11:39.391039872 +0100
+++ Modelfile	2025-12-05 02:11:32.070341544 +0100
@@ -2,10 +2,14 @@
 # To build a new Modelfile based on this, replace FROM with:
 FROM ministral-3:14b
 
-TEMPLATE """{{- range $index, $_ := .Messages }}
+TEMPLATE """{{- $lastUserIndex := -1 }}
+{{- range $index, $_ := .Messages }}
+{{- if eq .Role "user" }}{{ $lastUserIndex = $index }}{{ end }}
+{{- end }}
+{{- range $index, $_ := .Messages }}
 {{- if eq .Role "system" }}[SYSTEM_PROMPT]{{ .Content }}[/SYSTEM_PROMPT]
 {{- else if eq .Role "user" }}
-{{- if and (le (len (slice $.Messages $index)) 2) $.Tools }}[AVAILABLE_TOOLS]{{ $.Tools }}[/AVAILABLE_TOOLS]
+{{- if and (eq $lastUserIndex $index) $.Tools }}[AVAILABLE_TOOLS]{{ $.Tools }}[/AVAILABLE_TOOLS]
 {{- end }}[INST]{{ .Content }}[/INST]
 {{- else if eq .Role "assistant" }}
 {{- if .Content }}{{ .Content }}

Relevant log output


OS

Linux, Docker

GPU

Nvidia

CPU

Intel

Ollama version

0.13.1

Originally created by @rick-github on GitHub (Dec 5, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/13334 Originally assigned to: @pdevine on GitHub. ### What is the issue? This line in the template: ``` {{- if and (le (len (slice $.Messages $index)) 2) $.Tools }}[AVAILABLE_TOOLS]{{ $.Tools }}[/AVAILABLE_TOOLS] ``` adds tools to the user message, when the user message is last or second to last message in the message list. So for a completion call with ``` [ system message (S) user message (U1) assistant message (A1) user message (U2) ] ``` the tools get attached to U2 and the model will generate a message (A2) with one or more tool calls. In the process of completing the tools calls, the client will update the message list with A2 and the results of the tool call(s) with a `tool` message. ``` [ S U1 A1 U2 A2 T1 ] ``` Since U2 is no longer the second to last or last message, the tools are not attached in subsequent calls. If the model thinks it needs to make another tool call in order to satisfy the instruction in U2, it cannot. As a concrete example: ```json [ { "role": "user", "content": "what is 2.3 ^ 5.6 and 5.6 ^ 2.3? Now add them together." }, { "role": "assistant", "tool_calls": [ "function=Function(name='power', arguments={'x': 2.3, 'y': 5.6})", "function=Function(name='power', arguments={'x': 5.6, 'y': 2.3})" ] }, { "role": "tool", "content": "106.0903498817264" }, { "role": "tool", "content": "52.581438372010965" }, { "role": "assistant", "content": "Here are the calculations:\n\n- \\( 2.3^{5.6} \\approx 106.0903 \\)\n- \\( 5.6^{2.3} \\approx 52.5814 \\)\n\nAdding them together:\n**106.0903 + 52.5814 = 158.6717**\n\nThe final result is **158.6717**." } ] ``` In contrast, if the user message has tools attached: ```json [ { "role": "user", "content": "what is 2.3 ^ 5.6 and 5.6 ^ 2.3? Now add them together." }, { "role": "assistant", "tool_calls": [ "function=Function(name='power', arguments={'x': 2.3, 'y': 5.6})", "function=Function(name='power', arguments={'x': 5.6, 'y': 2.3})" ] }, { "role": "tool", "content": "106.0903498817264" }, { "role": "tool", "content": "52.581438372010965" }, { "role": "assistant", "tool_calls": [ "function=Function(name='add', arguments={'l': [106.0903498817264, 52.581438372010965]})" ] }, { "role": "tool", "content": "158.67178825373736" }, { "role": "assistant", "content": "The result of \\(2.3^{5.6}\\) is approximately **106.09**.\n\nThe result of \\(5.6^{2.3}\\) is approximately **52.58**.\n\nTheir sum is **158.67**." } ] ``` Suggested change to enable ministral to handle multiple tool calls: ```diff --- Modelfile.orig 2025-12-05 02:11:39.391039872 +0100 +++ Modelfile 2025-12-05 02:11:32.070341544 +0100 @@ -2,10 +2,14 @@ # To build a new Modelfile based on this, replace FROM with: FROM ministral-3:14b -TEMPLATE """{{- range $index, $_ := .Messages }} +TEMPLATE """{{- $lastUserIndex := -1 }} +{{- range $index, $_ := .Messages }} +{{- if eq .Role "user" }}{{ $lastUserIndex = $index }}{{ end }} +{{- end }} +{{- range $index, $_ := .Messages }} {{- if eq .Role "system" }}[SYSTEM_PROMPT]{{ .Content }}[/SYSTEM_PROMPT] {{- else if eq .Role "user" }} -{{- if and (le (len (slice $.Messages $index)) 2) $.Tools }}[AVAILABLE_TOOLS]{{ $.Tools }}[/AVAILABLE_TOOLS] +{{- if and (eq $lastUserIndex $index) $.Tools }}[AVAILABLE_TOOLS]{{ $.Tools }}[/AVAILABLE_TOOLS] {{- end }}[INST]{{ .Content }}[/INST] {{- else if eq .Role "assistant" }} {{- if .Content }}{{ .Content }} ``` ### Relevant log output ```shell ``` ### OS Linux, Docker ### GPU Nvidia ### CPU Intel ### Ollama version 0.13.1
GiteaMirror added the toolsbug labels 2026-04-12 21:35:23 -05:00
Author
Owner

@rick-github commented on GitHub (Dec 5, 2025):

magistral has the same issue.

$ ./ollama-run.py magistral What is 2.3 ^ 5.6 and 5.6 ^ 2.3?  Now multiply them together. --tool power,multiply --system "You MUST use the provided tools to answer the following questions."
[ToolCall(function=Function(name='power', arguments={'x': 2.3, 'y': 5.6}))]
[ToolCall(function=Function(name='power', arguments={'x': 5.6, 'y': 2.3}))]

The values of 2.3^5.6 and 5.6^2.3 are approximately 106.090 and 52.581 respectively. When multiplied together, these numbers result in a product which is 5547.282.
$ ./ollama-run.py magistral:fix What is 2.3 ^ 5.6 and 5.6 ^ 2.3?  Now multiply them together. --tool power,multiply --system "You MUST use the provided tools to answer the following questions."
Let's break down the calculation into steps:

First, I'll calculate \(2.3^{5.6}\) using a calculator or a computational tool.

Then, I'll calculate \(5.6^{2.3}\) in the same manner.

Finally, I'll multiply these two results together to get the desired outcome.

Let's proceed with the calculations:
[ToolCall(function=Function(name='power', arguments={'x': 2.3, 'y': 5.6}))]

Now I'll calculate 5.6 to the power of 2.3.
[ToolCall(function=Function(name='power', arguments={'x': 5.6, 'y': 2.3}))]

Now that I have both results, I'll multiply them together: 106.0903498817264 and 52.581438372010965.
[ToolCall(function=Function(name='multiply', arguments={'x': 106.0903498817264, 'y': 52.581438372010965}))]

The final result of multiplying \(2.3^{5.6}\) and \(5.6^{2.3}\) is approximately 5578.3832.
<!-- gh-comment-id:3615046706 --> @rick-github commented on GitHub (Dec 5, 2025): [magistral](https://ollama.com/library/magistral) has the same issue. ```console $ ./ollama-run.py magistral What is 2.3 ^ 5.6 and 5.6 ^ 2.3? Now multiply them together. --tool power,multiply --system "You MUST use the provided tools to answer the following questions." [ToolCall(function=Function(name='power', arguments={'x': 2.3, 'y': 5.6}))] [ToolCall(function=Function(name='power', arguments={'x': 5.6, 'y': 2.3}))] The values of 2.3^5.6 and 5.6^2.3 are approximately 106.090 and 52.581 respectively. When multiplied together, these numbers result in a product which is 5547.282. ``` ```console $ ./ollama-run.py magistral:fix What is 2.3 ^ 5.6 and 5.6 ^ 2.3? Now multiply them together. --tool power,multiply --system "You MUST use the provided tools to answer the following questions." Let's break down the calculation into steps: First, I'll calculate \(2.3^{5.6}\) using a calculator or a computational tool. Then, I'll calculate \(5.6^{2.3}\) in the same manner. Finally, I'll multiply these two results together to get the desired outcome. Let's proceed with the calculations: [ToolCall(function=Function(name='power', arguments={'x': 2.3, 'y': 5.6}))] Now I'll calculate 5.6 to the power of 2.3. [ToolCall(function=Function(name='power', arguments={'x': 5.6, 'y': 2.3}))] Now that I have both results, I'll multiply them together: 106.0903498817264 and 52.581438372010965. [ToolCall(function=Function(name='multiply', arguments={'x': 106.0903498817264, 'y': 52.581438372010965}))] The final result of multiplying \(2.3^{5.6}\) and \(5.6^{2.3}\) is approximately 5578.3832. ```
Author
Owner

@pdevine commented on GitHub (Dec 10, 2025):

This should be fixed now. Thanks @rick-github !

<!-- gh-comment-id:3635126183 --> @pdevine commented on GitHub (Dec 10, 2025): This should be fixed now. Thanks @rick-github !
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#8807