Drop content=None from messages in apply_chat_template#45422
Open
qgallouedec wants to merge 6 commits intomainfrom
Open
Drop content=None from messages in apply_chat_template#45422qgallouedec wants to merge 6 commits intomainfrom
content=None from messages in apply_chat_template#45422qgallouedec wants to merge 6 commits intomainfrom
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Rocketknight1
approved these changes
Apr 24, 2026
Member
Rocketknight1
left a comment
There was a problem hiding this comment.
Sorry for losing track of this one! I think it's a good fix, and a good path to standardization, but Deepseek-R1 is a very major model, even if it's a little out of date now that V4 exists, and this change would be very breaking for it. I think we should patch its template on the Hub first, then merge this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
apply_chat_template, drop thecontentkey from messages when its value isNonebefore passing to the Jinja template.Why this is a bug fix, not a breaking change
content=Nonemeans "there is no content", it is semantically identical to the key being absent. No caller setscontent=Noneexpecting the literal string"None"to appear in the output, or the output to be different than if the key were absent.Yet today, several templates crash or misbehave:
"None": GLM-5.1, GLM4MOE, MiniMax-M2.5, MiniMax-M2.7After this change, out of the 20 tool-calling models tested:
"None"→ correct output)content=Nonebut crashes on absent key, which IMO is itself a template bug to fix on the Hub repo).Why not just leave it to template authors?
Because
content=Noneis a real-world input (it's what the OpenAI API returns for tool-call-only messages)msg_dict = {'content': None, 'refusal': None, 'role': 'assistant', 'annotations': [], 'audio': None, 'function_call': None, 'tool_calls': [{'id': 'call_Im08OArU1YY2mKi7ntPxdm22', 'function': {'arguments': '{"city":"Paris"}', 'name': 'get_weather'}, 'type': 'function'}]}and expecting every template author to handle it correctly is a losing game — 8 out of 20 already don't. One normalization line in
apply_chat_templatefixes all of them at once and prevents future templates from hitting the same issue.Part of the broader discussion in #45419.