Skip to content

HFSystemFormat Exception #1144

Closed as not planned
Closed as not planned

Description

@allyssonf

When using HFSystemFormat to define the format when loading a dataset an error is shown:

An error occurred while generating the dataset

HFSystemFormat function at the end applies a chat template to the message:

        ....

        tokenizer = AutoTokenizer.from_pretrained(self.model_name)

       ....

        tokenized_chat = tokenizer.apply_chat_template(
            messages, tokenize=False, add_generation_prompt=True
        )

What happens is that if self.model_name does not have a chat_template attribute in tokenizer_config.json (e.g. granite 34 b code instruct ) it will raise an exception...

This code shows the problem:

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-20b-code-base")

chat = [
   {"role": "user", "content": "Hello, how are you?"},
   {"role": "assistant", "content": "I'm doing great. How can I help you today?"},
   {"role": "user", "content": "I'd like to show off how chat templating works!"},
]

print(tokenizer.apply_chat_template(chat, tokenize=False))
ValueError: Cannot use apply_chat_template() because tokenizer.chat_template is not set and no template argument was passed! For information about writing templates and setting the tokenizer.chat_template attribute, please see the documentation at https://huggingface.co/docs/transformers/main/en/chat_templating

Is it possible to improve the error handling in this case?
I don't know if its possible to have a default chat template or be more explicit in the error so the user could take action...

Thanks.

Activity

  1. github-actions commented on Apr 26, 2025

    @github-actions

    This issue is stale because it has been open for 30 days with no activity.

  2. github-actions commented on May 10, 2025

    @github-actions

    This issue was closed because it has been inactive for 14 days since being marked as stale.

  3. wak327 commented on Oct 3, 2026

    @wak327
    Contributor

    @allyssonf I opened #1983 for this. HFSystemFormat now raises a clear error when it's created if the model's tokenizer has no chat template. The message suggests using a model with a chat template, passing one explicitly with chat_kwargs_dict={'chat_template': ...} (which already works), or using SystemFormat. cc @elronbandel @yoavkatz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions