Hugging Face, End to EndLesson 15 of 75

Module 2 · Transformers, from characters to output

The chat template is not optional

In this lesson

  • 9 min read
  • 8 reading sections
  • Knowledge check
  • Lesson 6 of 9 in this chapter

This chapter’s goalTrace a piece of text from characters through a tokenizer to logits and back to text with the transformers library, and say which of the Auto class, the tokenizer, the padding side, the chat template or the generation config is at fault when a model loads without error and produces nonsense.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableWatch4:51
Full audiobookNot availableNot available
Two prompts the model sees very differentlyTyped by handUser: and Assistant: written as plain textNone of the special tokens the model wastrained onNothing marks where the assistant’s turnbeginsThe model often invents a second user turnRewritten by hand for every model familyRendered by apply_chat_templateThe exact Jinja string fromtokenizer_config.jsonThe role markers the model saw thousands oftimesadd_generation_prompt opens the assistant turnThe end marker matches the configuredeos_token_idUnchanged when you swap the model underneathNeither raises an error and both produce fluent text, which is why this is the commonest quality bugin open models. The left column is a string that resembles the training format; the right is thetraining format, read out of the repository the weights came from.
Neither raises an error and both produce fluent text, which is why this is the commonest quality bug in open models. The left column is a string that resembles the training format; the right is the training format, read out of the repository the weights came from.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 10Free · no account needed
Lesson steps

The most common quality bug in open models

Somebody downloads an instruct model, writes this, and concludes it is weak:

Python
prompt = "User: Summarise this in two lines.\n\nAssistant:"

That is not how the model was trained. During instruction tuning it saw a specific string (specific role markers, specific special tokens, specific newlines) thousands of times. Give it something that looks similar but is not identical and you are outside the distribution it learned. Output gets vaguer, it ignores system instructions, it fails to stop, it starts inventing a second user turn.

The correct format is not a convention you can guess. It is stored in the repository, in tokenizer_config.json, as a Jinja string named chat_template.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘The chat template is not optional’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/chat-templates (Accessed: 29 November 2026).