@@ -561,7 +561,7 @@ def write_tokenizer(tokenizer_path: str, save_dir: str, instruct: bool = False):
561561 {%- if not loop.last %}
562562 {{- ",\n " }}
563563 {%- else %}
564- {{- "\n " }}
564+ {{- ", \n " }}
565565 {%- endif -%}
566566 {%- endfor %}
567567 {{- "}) => any;\n \n " }}
@@ -696,6 +696,15 @@ def write_tokenizer(tokenizer_path: str, save_dir: str, instruct: bool = False):
696696 {%- endif %}
697697 {%- endif %}
698698 {%- if "tool_calls" in message %}
699+ {#- We need very careful handling here - we want to drop the tool call analysis message if the model #}
700+ {#- has output a later <|final|> message, but otherwise we want to retain it. This is the only case #}
701+ {#- when we render CoT/analysis messages in inference. #}
702+ {%- set future_final_message = namespace(found=false) %}
703+ {%- for future_message in loop_messages[loop.index:] %}
704+ {%- if future_message.role == 'assistant' and "tool_calls" not in future_message %}
705+ {%- set future_final_message.found = true %}
706+ {%- endif %}
707+ {%- endfor %}
699708 {#- We assume max 1 tool call per message, and so we infer the tool call name #}
700709 {#- in "tool" messages from the most recent assistant tool call name #}
701710 {%- set tool_call = message.tool_calls[0] %}
@@ -704,9 +713,9 @@ def write_tokenizer(tokenizer_path: str, save_dir: str, instruct: bool = False):
704713 {%- endif %}
705714 {%- if message.content and message.thinking %}
706715 {{- raise_exception("Cannot pass both content and thinking in an assistant message with tool calls! Put the analysis message in one or the other, but not both.") }}
707- {%- elif message.content %}
716+ {%- elif message.content and not future_final_message.found %}
708717 {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}
709- {%- elif message.thinking %}
718+ {%- elif message.thinking and not future_final_message.found %}
710719 {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
711720 {%- endif %}
712721 {{- "<|start|>assistant to=" }}
0 commit comments