Skip to content

Conversation

@kiankyars
Copy link
Contributor

This pull request is in regards to GH-2521.

I have added the templates for both qwen3 and qwen3 ollama.

Please let me know if there's anything else to add since I'd like to use these templates ASAP.

@kiankyars
Copy link
Contributor Author

@davedgd and @shimmyshimmer Just tagging you guys since I'm not sure if you get a notification from the issue itself.

@davedgd
Copy link
Contributor

davedgd commented May 15, 2025

@davedgd and @shimmyshimmer Just tagging you guys since I'm not sure if you get a notification from the issue itself.

Thank you @kiankyars — much appreciated!

@Datta0
Copy link
Collaborator

Datta0 commented May 15, 2025

Hey @kiankyars , thanks for this.
Can you please tell me what is the requirement for having chat template here? We can directly access it via the tokenizer right?

I understand if ollama_template is included as it might not be easily accessible.
Please correct me if I'm wrong

@shimmyshimmer shimmyshimmer merged commit 2d7cf7b into unslothai:main May 15, 2025
@kiankyars
Copy link
Contributor Author

Hey @kiankyars , thanks for this. Can you please tell me what is the requirement for having chat template here? We can directly access it via the tokenizer right?

I understand if ollama_template is included as it might not be easily accessible. Please correct me if I'm wrong

You raise a good point. The qwen3_template is included mostly to ensure consistency with other templates. in chat_templates.py

@davedgd
Copy link
Contributor

davedgd commented May 17, 2025

@kiankyars: There seems to be issue with the qwen-3 template that was added. Specifically, it reliably produces the following error:

TemplateSyntaxError: expected token 'end of print statement', got 'name'

I've created a minimal example here that works with qwen-2.5 but fails with qwen-3:

from unsloth import FastLanguageModel
from unsloth.chat_templates import get_chat_template

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Qwen3-0.6B-unsloth-bnb-4bit",
    max_seq_length = 8192,
    load_in_4bit = True,
)

tokenizer = get_chat_template(
    tokenizer,
    chat_template = "qwen-3", # works with qwen-2.5 but not qwen-3
)

messages = [{"role": "system", "content": "You are an expert assistant."}, 
            {"role": "user", "content": "Say Hello!"}, 
            {"role": "assistant", "content": "Hello!"}]

inputs = tokenizer.apply_chat_template(messages, 
                                       tokenize = False, 
                                       add_generation_prompt = False)

print(inputs)

With qwen-2.5 as the template, the result is as expected:

<|im_start|>system
You are an expert assistant.<|im_end|>
<|im_start|>user
Say Hello!<|im_end|>
<|im_start|>assistant
Hello!<|im_end|>

However, with qwen-3 as in the example, you will get the error noted earlier:

TemplateSyntaxError: expected token 'end of print statement', got 'name'

@shimmyshimmer
Copy link
Collaborator

@kiankyars: There seems to be issue with the qwen-3 template that was added. Specifically, it reliably produces the following error:

TemplateSyntaxError: expected token 'end of print statement', got 'name'

I've created a minimal example here that works with qwen-2.5 but fails with qwen-3:

from unsloth import FastLanguageModel
from unsloth.chat_templates import get_chat_template

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Qwen3-0.6B-unsloth-bnb-4bit",
    max_seq_length = 8192,
    load_in_4bit = True,
)

tokenizer = get_chat_template(
    tokenizer,
    chat_template = "qwen-3", # works with qwen-2.5 but not qwen-3
)

messages = [{"role": "system", "content": "You are an expert assistant."}, 
            {"role": "user", "content": "Say Hello!"}, 
            {"role": "assistant", "content": "Hello!"}]

inputs = tokenizer.apply_chat_template(messages, 
                                       tokenize = False, 
                                       add_generation_prompt = False)

print(inputs)

With qwen-2.5 as the template, the result is as expected:

<|im_start|>system
You are an expert assistant.<|im_end|>
<|im_start|>user
Say Hello!<|im_end|>
<|im_start|>assistant
Hello!<|im_end|>

However, with qwen-3 as in the example, you will get the error noted earlier:

TemplateSyntaxError: expected token 'end of print statement', got 'name'

@davedgd oh great point! would you like to add a PR so I can accept it! Thank you :)

@davedgd
Copy link
Contributor

davedgd commented May 17, 2025

@davedgd oh great point! would you like to add a PR so I can accept it! Thank you :)

@shimmyshimmer: Done! :) I traced down the issue to a couple of missing escapes on the double quotes. Seems to work fine with the patch in place:

<|im_start|>system
You are an expert assistant.<|im_end|>
<|im_start|>user
Say Hello!<|im_end|>
<|im_start|>assistant
<think>

</think>

Hello!<|im_end|>

@kiankyars
Copy link
Contributor Author

kiankyars commented May 17, 2025

@davedgd oh great point! would you like to add a PR so I can accept it! Thank you :)

@shimmyshimmer: Done! :) I traced down the issue to a couple of missing escapes on the double quotes. Seems to work fine with the patch in place:

<|im_start|>system
You are an expert assistant.<|im_end|>
<|im_start|>user
Say Hello!<|im_end|>
<|im_start|>assistant
<think>

</think>

Hello!<|im_end|>

Thanks for pointing that out! I gotta figure out how my tests did not reveal that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants