Repository navigation
Omit empty text from message content parts - #699
Conversation
`contentParts(for:layout:)` always appended a text part, so an assistant
tool call without text became `[{"type": "text", "text": ""}]`. Chat
templates test `message.content` for truth: the empty string a text
model receives is false, but a list holding an empty part is true.
Qwen3-VL's template then renders the call as `\n\n<tool_call>` instead
of the `\n<tool_call>` the model generated. The cache no longer matches
the prompt, and a rewind is refused because the model carries M-RoPE
state, so every tool follow-up pass prefilled the whole conversation.
Empty text now gives no part. An empty list is false, as the empty
string is, so the re-rendered prompt extends the cache again.
Checked against the chat templates of every registered model that uses
content parts (Qwen2-VL, Qwen2.5-VL, Qwen3-VL, Qwen3.5, Qwen3.8,
Muse-Glimmer, Gemma 3, Gemma 4, LFM2-VL, Ministral 3, FastVLM, GLM-OCR):
only the Qwen3-VL template changes output, and it now renders the same
as with string content.
| var parts: [[String: String]] = [] | ||
|
|
||
| func appendText(_ text: String) { | ||
| guard !text.isEmpty else { return } |
There was a problem hiding this comment.
I think this isn't safe for all models. Consider:
https://huggingface.co/mlx-community/Ministral-3-3B-Instruct-2512-4bit/blob/main/chat_template.jinja
{#- User messages supports text content or text and image chunks. #}
{%- if message['role'] == 'user' %}
{%- if message['content'] is string %}
{{- '[INST]' + message['content'] + '[/INST]' }}
{%- elif message['content'] | length > 0 %}
...
{%- else %}
{{- raise_exception('User message must have a string or a list of chunks in content') }}
{%- endif %}Though curiously the upstream template differs:
https://huggingface.co/mistralai/Ministral-3-3B-Instruct-2512/blob/main/chat_template.jinja
There is a similar rule for assistant messages:
{#- Assistant messages supports text content. #}
{%- elif message['role'] == 'assistant' %}
{%- if (message['content'] is none or message['content'] == '' or message['content']|length == 0) and (message['tool_calls'] is not defined or message['tool_calls'] is none or message['tool_calls']|length == 0) %}
{{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}but nothing for user messages -- it looks like a lot has changed in the template since the mlx quantization was made.
Anyway, some possibilities:
- make this be opt-in or opt-out, Mistral3MessageGenerator can implement
- this is just one I (claude really) found as being affected, so it make require a bunch of tests to make sure we have good coverage
- only omit when the result wouldn't be empty -- might require looking at the layout or post-filtering
- the PR was concerned about tool calls, so maybe limit it to that?
The seems to be an arbitrary rule on the mistral side, so none of these stand out as best, but maybe item 3 is the narrowest.
There was a problem hiding this comment.
Thanks, I’ve limited the omission to assistant messages with nonempty tool-call lists, as suggested. Other empty messages keep their text part.
davidkoski
left a comment
There was a problem hiding this comment.
Looks good but see my comment/question on mistral3. Thanks!
davidkoski
left a comment
There was a problem hiding this comment.
Changes look good, thank you!
Brings in ml-explore#622 (exact prefix rewinds in rotating KV caches), ml-explore#619 (streaming detokenizers), ml-explore#698, ml-explore#699, ml-explore#700, ml-explore#701, ml-explore#705, ml-explore#697 and ml-explore#695. Conflicts: AttentionUtils.swift keeps TurboKV telemetry next to upstream's SharedAttentionKVCache; KVCache.swift takes upstream's serialization and rotating-cache view/trim while keeping the fork's TurboQuant state, public cache fields and temporallyOrdered. Follow-ups for upstream behavior the fork had changed: the Qwen2VL audio placeholder no longer indexes content.count - 1 when a tool-call message has no text part, and Gemma4MessageGenerator uses content parts for every non-system role. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Proposed changes
contentParts(for:layout:)always appends a text part. An assistant tool call without text becomes[{"type": "text", "text": ""}]. Chat templates testmessage.contentfor truth: an empty string is false, but a list that holdsan empty part is true.
On Qwen3-VL this breaks KV cache reuse on every tool call:
\n\n<tool_call>. The model generated\n<tool_call>.RewindToCommonPrefixRulerefuses the rewind because the model carries M-RoPE state, so the decision is.rebuild.So each tool follow-up pass prefills the whole conversation again.
This change omits empty text: it gives no part. An empty list is false, as an empty string is, so the re-rendered prompt extends the cache again.
The next user turn reused its prefix before the fix too; the fix leaves it
working. The two runs generate different replies, so the counts differ.
Notes
SmolVLM2MessageGeneratorbuilds its own content and does not usecontentParts. Its template readsmessage['content'][0], so it must always get a part.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.