Repository: huggingface/transformers
File: src/transformers/models/gpt_oss/convert_gpt_oss_weights_to_hf.py (Python)

Code before:
    {%- elif message.role == 'tool' -%}
        {%- if last_tool_call.name is none %}
            {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
        {%- endif %}
        {{- "
functions." + last_tool_call.name }}
        {{- " to=assistant
commentary
" + message.content|tojson + "
" }}
    {%- elif message.role == 'user' -%}
        {{- "
user
" + message.content + "
" }}
    {%- endif -%}
{%- endfor -%}

{#- Generation prompt #}
{%- if add_generation_prompt -%}

assistant
{%- endif -%}"""

    converter = GptOssConverter(
        vocab_file=tokenizer_path,
        model_max_length=None,
        chat_template=chat_template,
    )
    tokenizer = converter.tokenizer
    tokenizer.save_pretrained(save_dir)

    print("Saving chat template...")
    chat_template_path = os.path.join(save_dir, "chat_template.json")
    with open(chat_template_path, "w") as f:
        json.dump({"chat_template": chat_template}, f, indent=2)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--input_dir",
        help="Location of `./original` subfolder of the GPT OSS model repo.",
    )
    parser.add_argument(
        "--output_dir",
        help="Location to write the converted HF model and tokenizer",
    )

    # Only specify this if you want to use the model with mxfp4 quantization
    # It means the model will be unpacked, and quantized using mxfp4 during inference if all the triton requirements are satisfied (triton >= 3.4.0)
    # Else we have a fallback to the full precision model (bfloat16)
    # If not specified, the model will be unpacked during conversion, and will be in fp8/bfloat16 during inference
    # Note: mxfp4 should bring an important speedup in inference time with blackwell gpus
    parser.add_argument(
        "--mxfp4",
        action="store_true",
        help="Whether to use the original model with mxfp4 quantization or default to the full precision model.",
    )

Review comment (suggestion):
We prefer raw `chat_template.jinja` in modern conversions!

Code after:
        {%- if last_tool_call.name is none %}
            {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
        {%- endif %}
        {{- "
functions." + last_tool_call.name }}
        {{- " to=assistant
commentary
" + message.content|tojson + "
" }}
    {%- elif message.role == 'user' -%}
        {{- "
user
" + message.content + "
" }}
    {%- endif -%}
{%- endfor -%}

{#- Generation prompt #}
{%- if add_generation_prompt -%}

assistant
{%- endif -%}"""

    converter = GptOssConverter(
        vocab_file=tokenizer_path,
        model_max_length=None,
        chat_template=chat_template,
    )
    tokenizer = converter.tokenizer
    tokenizer.save_pretrained(save_dir)

    print("Saving chat template...")
    chat_template_path = os.path.join(save_dir, "chat_template.jinja")
    with open(chat_template_path, "w", encoding="utf-8") as f:
        f.write(chat_template)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--input_dir",
        help="Location of `./original` subfolder of the GPT OSS model repo.",
    )
    parser.add_argument(
        "--output_dir",
        help="Location to write the converted HF model and tokenizer",
    )

    # Only specify this if you want to use the model with mxfp4 quantization
    # It means the model will be unpacked, and quantized using mxfp4 during inference if all the triton requirements are satisfied (triton >= 3.4.0)
    # Else we have a fallback to the full precision model (bfloat16)
    # If not specified, the model will be unpacked during conversion, and will be in fp8/bfloat16 during inference
    # Note: mxfp4 should bring an important speedup in inference time with blackwell gpus
    parser.add_argument(
        "--mxfp4",
        action="store_true",
        help="Whether to use the original model with mxfp4 quantization or default to the full precision model.",
    )