Skip to main content

ChatTemplate

Struct ChatTemplate 

Source
pub struct ChatTemplate { /* private fields */ }
Expand description

A compiled Hugging Face chat template, ready to render.

Construction compiles the Jinja source and installs the transformers-compatible globals/filters. render borrows &self, so a single instance is cheap to reuse and shareable across threads.

Implementations§

Source§

impl ChatTemplate

Source

pub fn from_str(source: &str) -> Result<Self, Error>

Compile from a raw Jinja chat-template string, using the default transformers-compatible settings (see ChatTemplateBuilder to customize).

An inherent from_str (not just the FromStr impl) so callers can write ChatTemplate::from_str(s) without importing the trait.

Source

pub fn from_tokenizer_config(config: &TokenizerConfig) -> Result<Self, Error>

Compile from a parsed tokenizer_config.json, resolving the default template and injecting the config’s special tokens (bos_token, …) into the render context.

For named-template selection (tool_use, rag) or custom options, use builder_from_config.

Source

pub fn from_template_and_config( template: &str, config: &TokenizerConfig, ) -> Result<Self, Error>

Compile from a standalone template string (the contents of a chat_template.jinja file), injecting special tokens from a separately-loaded TokenizerConfig.

Newer transformers ship the chat template as its own chat_template.jinja file rather than inside tokenizer_config.json (Gemma 3+, SmolLM3, …). The template body comes from the file; the special tokens (bos_token, …) still come from the config. Matching the transformers precedence, a standalone file overrides any inline chat_template field, so this ignores config.chat_template entirely.

Source

pub fn builder(source: &str) -> ChatTemplateBuilder

Start a builder to compile source with non-default options (clock, undefined policy…).

Source

pub fn builder_from_config( config: &TokenizerConfig, ) -> Result<ChatTemplateBuilder, Error>

Start a builder from a tokenizer_config.json, carrying its special tokens. Use template_name to pick a named sub-template.

Fails with Error::Config if the config has no chat_template field at all.

Source

pub fn from_hub(repo_id: &str) -> Result<Self, Error>

Fetch a Hub repo (default branch) and compile its default chat template, injecting the config’s special tokens. Requires the hub feature.

Loads tokenizer_config.json, plus a standalone chat_template.jinja if the repo ships one (Gemma 3+, SmolLM3, …); the standalone file takes precedence over an inline chat_template field, matching transformers.

Authentication uses hf-hub’s discovery (the HF_TOKEN env var or the cached huggingface-cli login token); gated repos need a token with access. For a pinned commit or branch, use from_hub_revision.

use hf_chat_template::{ChatTemplate, Message};
let tmpl = ChatTemplate::from_hub("Qwen/Qwen2.5-0.5B-Instruct")?;
let prompt = tmpl.render_messages(&[Message::user("Hi")], true)?;
Source

pub fn from_hub_revision(repo_id: &str, revision: &str) -> Result<Self, Error>

Like from_hub, but pins a specific revision (a branch, tag, or commit SHA). Requires the hub feature.

Source

pub fn render(&self, input: &RenderInput) -> Result<String, Error>

Render the typed input model to the final prompt string. Special tokens from the source config are injected first; any matching key in RenderInput::extra wins.

Source

pub fn render_messages( &self, messages: &[Message], add_generation_prompt: bool, ) -> Result<String, Error>

Convenience: render with only messages and the generation-prompt flag.

Source

pub fn render_value(&self, ctx: Value) -> Result<String, Error>

Render with an arbitrary minijinja context value — the low-level escape hatch for callers who need to pass template variables we don’t model with typed structs.

Unlike render, this does not inject the config’s special tokens; the caller’s context is used as-is.

Trait Implementations§

Source§

impl Debug for ChatTemplate

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for ChatTemplate

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Error>

Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V