From ca04b97c93614ae51d2b79e2323aa731fbaed697 Mon Sep 17 00:00:00 2001 From: Rick Yuan <76869524+RIckYuan999@users.noreply.github.com> Date: Thu, 8 May 2025 17:47:19 +0800 Subject: [PATCH] [Bugfix] Fix tool call template validation for Mistral models (#17644) Signed-off-by: Rick Yuan Signed-off-by: RIck Yuan Co-authored-by: Aaron Pham --- examples/tool_chat_template_mistral3.jinja | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/tool_chat_template_mistral3.jinja b/examples/tool_chat_template_mistral3.jinja index 2b2f94d7ef52e..7c4249ec44c56 100644 --- a/examples/tool_chat_template_mistral3.jinja +++ b/examples/tool_chat_template_mistral3.jinja @@ -29,7 +29,14 @@ {%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %} -{%- for message in loop_messages | rejectattr("role", "equalto", "tool") | rejectattr("role", "equalto", "tool_results") | selectattr("tool_calls", "undefined") %} +{%- set filtered_messages = [] %} +{%- for message in loop_messages %} + {%- if message["role"] not in ["tool", "tool_results"] and not message.get("tool_calls") %} + {%- set filtered_messages = filtered_messages + [message] %} + {%- endif %} +{%- endfor %} + +{%- for message in filtered_messages %} {%- if (message["role"] == "user") != (loop.index0 % 2 == 0) %} {{- raise_exception("After the optional system message, conversation roles must alternate user/assistant/user/assistant/...") }} {%- endif %} @@ -116,4 +123,4 @@ {%- else %} {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }} {%- endif %} -{%- endfor %} \ No newline at end of file +{%- endfor %}