You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
apply_chat_template(tokenize=True) crashes on assistant messages with tool calls and no content #45290
An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
My own task or dataset (give details below)
Reproduction
ProcessorMixin.apply_chat_template raises KeyError: 'content' when tokenize=True and the conversation contains an assistant message with tool_calls but no content key.
KeyError: 'content'
File "processing_utils.py", line 1807, in apply_chat_template
visuals = [content for content in message["content"] if content["type"] in ["image", "video"]]
~~~~~~~^^^^^^^^^^^
visuals= [contentforcontentinmessage["content"] ifcontent["type"] in ["image", "video"]]
where it's assumed that all turns contain a content key. However in the codebase, AFAICT, content is always assumed to be optional for assistant turns with tool calls.
Possible fix
Guard the access with .get("content") or skip messages without content:
formessageinconversation:
content=message.get("content") or []
visuals= [cforcincontentifisinstance(c, dict) andc.get("type") in ["image", "video"]]
Activity
zucchini-nlp commentedon Apr 7, 2026
Yep, if content is optional I don't mind safe accessing it. @Rocketknight1 do you agree that the "content" is optional?
fix: KeyError when apply_chat_template encounters messages without co…
fix: KeyError when apply_chat_template encounters messages without co…
fix: KeyError when apply_chat_template encounters messages without co…
fix: KeyError when apply_chat_template encounters messages without co…
fix: KeyError when apply_chat_template encounters messages without co…
Rocketknight1 commentedon Apr 9, 2026
@zucchini-nlp yes! Some models definitely have templates with messages that can have
tool_callsbut nocontentfix(processing): guard message content access in apply_chat_template