Fix `chat_template` crash when assistant message omits the `content` key

#248
by qgallouedec HF Staff - opened

The current template crashes on assistant messages that have tool_calls but no content key:

UndefinedError: 'dict object' has no attribute 'content'

It only works today because callers pass content=None explicitly. Once https://github.com/huggingface/transformers/pull/45422 lands, transformers will drop content=None from messages before rendering (since None is semantically identical to absent, and is what the OpenAI API returns for tool-call-only messages). At that point this template will start failing for all users.

Repro

from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1")

messages = [
    {"role": "user", "content": "What's the weather in Paris?"},
    {"role": "assistant", "tool_calls": [{
        "type": "function",
        "function": {"name": "get_weather", "arguments": '{"city":"Paris"}'},
    }]},  # no `content` key
]

tok.apply_chat_template(messages, tokenize=False)
# UndefinedError: 'dict object' has no attribute 'content'

Fix

One-line change: message['content'] is nonemessage.get('content') is none, so the check handles both content=None and absent content.

qgallouedec changed pull request title from Update tokenizer_config.json to Fix `chat_template` crash when assistant message omits the `content` key
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment