Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions trl/chat_template_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,40 @@ def clone_chat_template(
},
}

gemma4_schema = {
"type": "object",
"properties": {
"role": {"const": "assistant"},
"thinking": {"type": "string"},
"content": {"type": "string"},
"tool_calls": {
"x-regex-iterator": r"<\|tool_call>(.*?)<tool_call\|>",
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"const": "function"},
"function": {
"type": "object",
"x-regex": r"call\:(?P<name>\w+)(?P<arguments>\{.*\})",
"properties": {
"name": {
"type": "string",
},
"arguments": {
"type": "object",
"x-parser": "gemma4-tool-call",
"additionalProperties": {},
},
},
},
},
},
},
},
"x-regex": r"(\<\|channel\>thought\n(?P<thinking>.*?)\<channel\|\>)?(?P<content>(?:(?!\<\|tool_call\>).)+)?(?P<tool_calls>\<\|tool_call\>.*\<tool_call\|\>)?",
}


deepseekv3_chat_template = (_CHAT_TEMPLATES_DIR / "deepseekv3.jinja").read_text()

Expand All @@ -328,6 +362,8 @@ def clone_chat_template(

qwen3_5_chat_template_4b_and_above = (_CHAT_TEMPLATES_DIR / "qwen3_5_4b_and_above.jinja").read_text()

gemma4_chat_template = (_CHAT_TEMPLATES_DIR / "gemma4.jinja").read_text()


ProcessingClassT = TypeVar("ProcessingClassT", PreTrainedTokenizer, ProcessorMixin)

Expand Down Expand Up @@ -382,6 +418,8 @@ def add_response_schema(processing_class: ProcessingClassT) -> ProcessingClassT:
tokenizer.response_schema = qwen3_schema
elif chat_template in [qwen3_5_chat_template_2b_and_below, qwen3_5_chat_template_4b_and_above]:
tokenizer.response_schema = qwen3_5_schema
elif chat_template in [gemma4_chat_template]:
tokenizer.response_schema = gemma4_schema
else:
raise ValueError(
"Unrecognized chat template, failed to add response schema. Please manually set the response schema on "
Expand Down
Loading