lhz7891444 commited on
Commit
930f638
·
verified ·
1 Parent(s): 12a3808

Remove the dead `<tool_sep>` branch from chat_template.jinja (fixes OpenBMB/MiniCPM#379)

Browse files

Remove the dead `<tool_sep>` branch from chat_template.jinja (fixes OpenBMB/MiniCPM#379)

## What

Two changes, nothing else:

1. Delete the `<tool_sep>` handling block inside the assistant branch (former lines 54–117). It split `content` on `<tool_sep>` and tried to interleave the tool-call XML between the text segments by accumulating `processed_content` inside `for` loops.
2. Change the trailing tool-call block guard from `{%- if message.tool_calls and not has_tool_sep %}` to `{%- if message.tool_calls %}`.

Diff: 66 lines removed, 1 line changed. Parameter rendering, reasoning handling, tool-response handling and the generation prompt are untouched.

## Why

**The branch never worked.** Jinja2 does not propagate `{% set %}` assignments made inside a `for` loop to the enclosing scope, so `processed_content` kept its initial value `content_parts[0]` — everything after the first `<tool_sep>` was silently dropped (see #379 for the minimal reproducer). `has_tool_sep` was never defined anywhere, so the trailing guard was always true and the tool-call XML was emitted by the fallback block regardless.

**The branch is unreachable in normal serving.** `<tool_sep>` is not a special token in the tokenizer, the SGLang `minicpm5_detector` neither produces nor consumes it, and the model does not emit it (0/10 samples across prompts designed to elicit "text, then tool call, then text"). Only a hand-crafted assistant message containing the literal string `<tool_sep>` reaches this code, and in that case it loses data.

Removing it is therefore a pure cleanup with one observable effect: an assistant `content` that happens to contain `<tool_sep>` is now preserved verbatim instead of being truncated.

Files changed (1) hide show
  1. chat_template.jinja +1 -66
chat_template.jinja CHANGED
@@ -51,71 +51,6 @@
51
  {%- endif %}
52
  {%- endif %}
53
 
54
- {%- if message.tool_calls %}
55
- {%- set content_parts = content.split('<tool_sep>') %}
56
- {%- set processed_content = content_parts[0] %}
57
- {%- set tool_calls_count = message.tool_calls|length %}
58
- {%- set tool_sep_count = content_parts|length - 1 %}
59
- {%- set min_count = [tool_calls_count, tool_sep_count]|min %}
60
-
61
- {%- for i in range(1, content_parts|length) %}
62
- {%- set tool_index = i - 1 %}
63
- {%- if tool_index < tool_calls_count %}
64
- {%- set tool_call = message.tool_calls[tool_index] %}
65
- {%- if tool_call.function %}
66
- {%- set tool_call = tool_call.function %}
67
- {%- endif %}
68
- {%- set single_tool_xml %}
69
- {{- '<function name="' ~ tool_call.name ~ '">' }}
70
- {%- if tool_call.arguments %}
71
- {%- set args_dict = tool_call.arguments %}
72
- {%- for param_name, param_value in args_dict.items() %}
73
- {{- '<param name="' ~ param_name ~ '">' }}
74
- {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
75
- {{- '<![CDATA[' + param_value + ']]>' }}
76
- {%- else %}
77
- {{- param_value }}
78
- {%- endif %}
79
- {{- '</param>' }}
80
- {%- endfor %}
81
- {%- endif %}
82
- {{- '</function>' }}
83
- {%- endset %}
84
- {%- set processed_content = processed_content + single_tool_xml + content_parts[i] %}
85
- {%- else %}
86
- {%- set processed_content = processed_content + content_parts[i] %}
87
- {%- endif %}
88
- {%- endfor %}
89
-
90
- {%- if tool_calls_count > tool_sep_count %}
91
- {%- for remaining_index in range(tool_sep_count, tool_calls_count) %}
92
- {%- set tool_call = message.tool_calls[remaining_index] %}
93
- {%- if tool_call.function %}
94
- {%- set tool_call = tool_call.function %}
95
- {%- endif %}
96
- {%- set remaining_tool_xml %}
97
- {{- '<function name="' ~ tool_call.name ~ '">' }}
98
- {%- if tool_call.arguments %}
99
- {%- set args_dict = tool_call.arguments %}
100
- {%- for param_name, param_value in args_dict.items() %}
101
- {{- '<param name="' ~ param_name ~ '">' }}
102
- {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
103
- {{- '<![CDATA[' + param_value + ']]>' }}
104
- {%- else %}
105
- {{- param_value }}
106
- {%- endif %}
107
- {{- '</param>' }}
108
- {%- endfor %}
109
- {%- endif %}
110
- {{- '</function>' }}
111
- {%- endset %}
112
- {%- set processed_content = processed_content + remaining_tool_xml %}
113
- {%- endfor %}
114
- {%- endif %}
115
-
116
- {%- set content = processed_content %}
117
- {%- endif %}
118
-
119
  {%- if reasoning_content %}
120
  {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
121
  {%- elif '<think>' not in content and '</think>' not in content %}
@@ -124,7 +59,7 @@
124
  {{- '<|im_start|>' + message.role + '\n' + content }}
125
  {%- endif %}
126
 
127
- {%- if message.tool_calls and not has_tool_sep %}
128
  {%- for tool_call in message.tool_calls %}
129
  {%- if (loop.first and content) or (not loop.first) %}
130
  {{- '\n' }}
 
51
  {%- endif %}
52
  {%- endif %}
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  {%- if reasoning_content %}
55
  {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
56
  {%- elif '<think>' not in content and '</think>' not in content %}
 
59
  {{- '<|im_start|>' + message.role + '\n' + content }}
60
  {%- endif %}
61
 
62
+ {%- if message.tool_calls %}
63
  {%- for tool_call in message.tool_calls %}
64
  {%- if (loop.first and content) or (not loop.first) %}
65
  {{- '\n' }}