Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 9 min read Announcement mediumAdept

Chat Templates: An End to the Silent Performance Killer

Chat models are trained with incompatible formats for flattening role/content message lists into a single string, and feeding a model a different format than it was trained on causes severe but silent performance degradation. Hugging Face tokenizers now carry a `chat_template` attribute holding a Jinja template that renders conversations into the model's exact training format, applied via `tokenizer.apply_chat_template()`. Jinja was chosen over a simpler per-role prefix/suffix scheme because it can express every known format. ChatML with `<|im_start|>`/`<|im_end|>` tokens is recommended for new models, and model owners should set explicit templates rather than relying on fragile class-level defaults kept only for backward compatibility.

Assumes: Hugging Face Transformers tokenizerschat LLM fine-tuning or inference

Related posts

  • Tool Use, Unified

    Hugging Face mediumAdept

    Hugging Face Transformers extends Jinja chat templates to cover tool use, giving a single portable API for passing tools to Mistral, Cohere, NousResearch and Llama models. Tools are defined as JSON schema, or as Python functions whose type hints and docstrings are converted automatically via get_json_schema. Tool calls go into the chat as a tool_calls field on assistant messages, and results are added as separate tool-role messages. A full walkthrough with Hermes-2-Pro-Llama-3-8B shows the loop end to end. Parsing the model's emitted tool-call text is still model-specific and needs a little json.loads or regex work.

  • The 4 Things Qwen-3’s Chat Template Teaches Us

    Hugging Face mediumAdept

    Qwen-3's Jinja chat template differs from Qwen-2.5 and QwQ in four ways. An enable_thinking flag prefills an empty <think></think> block so reasoning can be skipped, where QwQ forces it on every turn. A rolling checkpoint keeps <think> blocks only for assistant turns after the latest non-tool user message, which preserves the active plan across multi-step tool calls and prunes stale reasoning. The template also serializes tool-call arguments only when they aren't already strings, avoiding double-escaping, and drops the default system prompt.

  • From OpenAI to Open LLMs with Messages API on Hugging Face

    Hugging Face mediumAdept

    Text Generation Inference 1.4.0 adds a Messages API compatible with OpenAI's Chat Completion API, so open LLMs on Inference Endpoints can be called from existing OpenAI client code by changing the base URL and token. The guide deploys Nous-Hermes-2-Mixtral-8x7B-DPO via huggingface_hub with TGI token-limit settings and then calls it from the OpenAI Python and JavaScript clients. It also plugs the endpoint into LangChain (ChatOpenAI) and LlamaIndex (OpenAILike) RAG pipelines. Limitations: no function calling, only models with a chat_template, and a subset of parameters (stream, max_tokens, temperature, top_p, seed, logprobs, frequency_penalty).

  • Hosting your Models and Datasets on Hugging Face Spaces using Streamlit

    Hugging Face mediumAdept

    Streamlit apps can showcase Hugging Face models and datasets and be hosted on Hugging Face Spaces. The walkthrough recreates "Write with Transformer" using Streamlit widgets such as text_area, sidebar sliders and number_input to collect generation hyperparameters like temperature, top-k and top-p. It then visualizes a poetry dataset, loaded with 🤗 Datasets streaming, using st.dataframe, st.bar_chart and st.pyplot for seaborn/matplotlib plots. Deployment means dragging files into a Space, listing dependencies in requirements.txt, and matching the Streamlit version.

  • Hugging Face x LangChain : A new partner package

    Hugging Face mediumAdept

    langchain-huggingface is a new Python partner package jointly maintained by Hugging Face and LangChain, replacing community-written integrations that had drifted out of date. It provides HuggingFacePipeline for running transformers models locally, HuggingFaceEndpoint for the serverless Inference API or deployed TGI endpoints via InferenceClient, and ChatHuggingFace, which turns message lists into model-specific prompts with tokenizer.apply_chat_template. For embeddings it offers HuggingFaceEmbeddings (local sentence-transformers) and HuggingFaceEndpointEmbeddings (Hub or TEI instances), each shown with short code examples.

The quest for software engineering knowledge.

Look:
Layout: