[OpenAI] Drop empty assistant turns for mistral_common tokenizers (#35915)

mistral_common rejects an assistant turn carrying neither content nor tool calls, while other chat templates ignore it, so an OpenAI-compatible request that works elsewhere failed on Mistral models. Drop those turns before templating; turns with tool_calls, multimodal content, or real text are untouched, and a trailing assistant message is already consumed upstream as the continue_final_message prefix.
This commit is contained in:
Alison Shao
2026-08-23 20:07:17 -07:00
committed by GitHub
parent 6ca872a11f
commit 0c1e9bda57
2 changed files with 125 additions and 0 deletions
@@ -628,9 +628,48 @@ def patch_mistral_common_tokenizer(tokenizer):
adapted.append(msg)
return adapted
def _assistant_content_is_empty(content):
if content is None:
return True
if isinstance(content, str):
return not content.strip()
if isinstance(content, list):
return all(
isinstance(part, dict)
and part.get("type") in ("text", "input_text")
and not str(part.get("text") or "").strip()
for part in content
)
return False
def _drop_empty_assistant_messages(messages):
"""Drop assistant turns with neither content nor tool calls, which
mistral_common rejects while other chat templates ignore them. A trailing
assistant turn is consumed upstream as the continue_final_message prefix,
so this cannot drop a prefill.
"""
if not isinstance(messages, (list, tuple)):
return messages
kept = []
for msg in messages:
if isinstance(msg, (list, tuple)):
kept.append(_drop_empty_assistant_messages(msg))
continue
if (
isinstance(msg, dict)
and msg.get("role") == "assistant"
and not msg.get("tool_calls")
and _assistant_content_is_empty(msg.get("content"))
):
continue
kept.append(msg)
return kept
def _safe_apply_chat_template(messages, **kwargs):
kwargs.pop("add_generation_prompt", None)
messages = _adapt_placeholder_messages_for_mistral_common(messages)
messages = _drop_empty_assistant_messages(messages)
return tokenizer._orig_apply_chat_template(messages, **kwargs)
tokenizer.apply_chat_template = _safe_apply_chat_template