# Chat template utilities

## add_response_schema[[trl.chat_template_utils.add_response_schema]]

#### trl.chat_template_utils.add_response_schema[[trl.chat_template_utils.add_response_schema]]

[Source](https://github.com/huggingface/trl/blob/v0.26.1/trl/chat_template_utils.py#L143)

Adds the appropriate response schema to the given tokenizer based on its chat template.

At the time of initial implementation, most tokenizers do not have built-in support for response schemas. While
waiting for broader adoption, we provide this utility function to manually set the response schema for known chat
templates.

Examples:

```python
>>> from trl.chat_template_utils import add_response_schema
>>> from transformers import AutoTokenizer

>>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
>>> tokenizer = add_response_schema(tokenizer)
>>> assistant_text = '\n{"name": "multiply", "arguments": {"a": 3, "b": 4}}\n'
>>> tokenizer.parse_response(assistant_text)
{'role': 'assistant', 'content': '', 'tool_calls': [{'type': 'function', 'function': {'name': 'multiply', 'arguments': {'a': 3, 'b': 4}}}]}
```

**Parameters:**

tokenizer (`PreTrainedTokenizer`) : Tokenizer to which the response schema will be added.

**Returns:**

``PreTrainedTokenizer``

Tokenizer with the added response schema.

## is_chat_template_prefix_preserving[[trl.chat_template_utils.is_chat_template_prefix_preserving]]

#### trl.chat_template_utils.is_chat_template_prefix_preserving[[trl.chat_template_utils.is_chat_template_prefix_preserving]]

[Source](https://github.com/huggingface/trl/blob/v0.26.1/trl/chat_template_utils.py#L183)

Check whether the chat template preserves prefixes when applied.

**Parameters:**

tokenizer (`PreTrainedTokenizer`) : Tokenizer instance to check.

**Returns:**

``bool``

`True` if the chat template preserves prefixes, `False` otherwise.

## get_training_chat_template[[trl.chat_template_utils.get_training_chat_template]]

#### trl.chat_template_utils.get_training_chat_template[[trl.chat_template_utils.get_training_chat_template]]

[Source](https://github.com/huggingface/trl/blob/v0.26.1/trl/chat_template_utils.py#L306)

Get a prefix-preserving chat template for training, if needed.

If the tokenizer's template isn't prefix-preserving, returns a training-compatible template (currently only Qwen3
supported). Otherwise, returns `None`.

Example:

```python
>>> from trl.chat_template_utils import get_training_chat_template
>>> from transformers import AutoTokenizer

>>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
>>> messages1 = [
...     {"role": "user", "content": "What color is the sky?"},
...     {"role": "assistant", "content": "It is blue."},
... ]
>>> messages2 = [
...     {"role": "user", "content": "What color is the sky?"},
...     {"role": "assistant", "content": "It is blue."},
...     {"role": "user", "content": "And at night?"},
... ]
>>> tokenizer.apply_chat_template(messages1, tokenize=False)
'user\nWhat color is the sky?\nassistant\n\n\n\n\nIt is blue.\n'

>>> tokenizer.apply_chat_template(messages2, tokenize=False)
'user\nWhat color is the sky?\nassistant\nIt is blue.\nuser\nAnd at night?\n'

>>> #                                                                       ^ think tags missing
>>> chat_template = get_training_chat_template(tokenizer)
>>> tokenizer.apply_chat_template(messages1, tokenize=False, chat_template=chat_template)
'user\nWhat color is the sky?\nassistant\n\n\n\n\nIt is blue.\n'

>>> tokenizer.apply_chat_template(messages2, tokenize=False, chat_template=chat_template)
'user\nWhat color is the sky?\nassistant\n\n\n\n\nIt is blue.\nuser\nAnd at night?\n'
```

**Parameters:**

tokenizer (`PreTrainedTokenizer`) : Tokenizer instance to check.

**Returns:**

``str` or `None``

Training-compatible chat template, or `None` if no patching is needed.

## parse_response[[trl.chat_template_utils.parse_response]]

#### trl.chat_template_utils.parse_response[[trl.chat_template_utils.parse_response]]

[Source](https://github.com/huggingface/trl/blob/v0.26.1/trl/chat_template_utils.py#L365)

Parse a token sequence into structured response dictionaries with fallback handling.

Attempts to parse the sequence using `tokenizer.parse_response()`. If parsing fails (e.g., due to malformed tool
calls like `{"type":"function"`), falls back to decoding as plain text.

Also removes incorrectly appended EOS tokens from tool call content when present.

Example:
```python
>>> from trl.chat_template_utils import parse_response, add_response_schema
>>> from transformers import AutoTokenizer

>>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
>>> tokenizer = add_response_schema(tokenizer)  # temporary until built-in support
>>> text = '\n{"name": "multiply", "arguments": {"a": 3, "b": 4}}\n'
>>> ids = tokenizer(text)["input_ids"]
>>> parse_response(tokenizer, ids)
{'role': 'assistant', 'content': '', 'tool_calls': [{'type': 'function', 'function': {'name': 'multiply', 'arguments': {'a': 3, 'b': 4}}}]}
```

**Parameters:**

tokenizer (`PreTrainedTokenizer`) : Tokenizer with a `parse_response()` method.

ids (`list[int]`) : List of token sequences.

**Returns:**

``dict``

Response dictionary.

