Skip to content

Conversation

@0xrushi
Copy link

@0xrushi 0xrushi commented Oct 26, 2025

Purpose

When using tokenizer.apply_chat_template() to manually format chat messages and then passing the resulting string to llm.generate(), vLLM incorrectly inserts an extra BOS token during tokenization.

Resolves #27486

Test Plan

def min_repro():
    from vllm import LLM, SamplingParams
    from transformers import AutoTokenizer
    
    tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
    MODEL_NAME = "meta-llama/Llama-3.2-1B-Instruct"
    prompt = "hello, how are you?"
    prompt_with_template = tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False, add_generation_prompt=True, add_special_tokens=False)
    llm = LLM(MODEL_NAME, max_model_len=1024, gpu_memory_utilization=0.60)
    outputs = llm.generate([prompt_with_template], sampling_params=SamplingParams(temperature=0.0, top_p=1.0, logprobs=1))
    print(outputs[0].prompt_token_ids)
    prompt_with_template_tokens = tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=True, add_generation_prompt=True)
    print(prompt_with_template_tokens)
    assert len(outputs[0].prompt_token_ids) == len(prompt_with_template_tokens)
    

if __name__ == "__main__":
    min_repro()

Test Result

assert = success


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

@github-actions
Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors.

You ask your reviewers to trigger select CI tests on top of fastcheck CI.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

If you have any questions, please reach out to us on Slack at https://slack.vllm.ai.

🚀

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a fix to prevent duplicate beginning-of-sequence (BOS) tokens during input preprocessing. The change checks if a prompt already starts with the BOS token text. If so, it disables the tokenizer's automatic addition of special tokens, provided the user hasn't explicitly configured this behavior. The implementation is safe and only applies to tokenizers that expose a bos_token string attribute. My review finds the logic to be sound and without any high or critical severity issues.

@0xrushi 0xrushi changed the title fix: Add support for optional BOS token handling in input prepro… fix: double bos token Oct 26, 2025
inc-jeong and others added 24 commits October 25, 2025 22:59
…oject#26225)

Signed-off-by: inc-jeong <[email protected]>
Signed-off-by: InChang Jeong <[email protected]>
Co-authored-by: USER <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
…llm-project#24097)

Signed-off-by: chenjun <[email protected]>
Signed-off-by: kliuae <[email protected]>
Co-authored-by: valarLip <[email protected]>
Co-authored-by: TJian <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
Signed-off-by: Chendi Xue <[email protected]>
Signed-off-by: Chendi Xue <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
Signed-off-by: DarkLight1337 <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
…ect / index_copy to reduce d2h) (vllm-project#26437)

Signed-off-by: Vadim Gimpelson <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
Signed-off-by: Bram Wasti <[email protected]>
Co-authored-by: Wentao Ye <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
…llm-project#26987)

Signed-off-by: Akash Kaothalkar <[email protected]>
Co-authored-by: Akash Kaothalkar <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
…roject#27008)

Signed-off-by: bogdanm <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: 0xrushi <[email protected]>
…ct#24057)

Signed-off-by: Tahsin Tunan <[email protected]>
Signed-off-by: Harry Mellor <[email protected]>
Co-authored-by: Nick Hill <[email protected]>
Co-authored-by: Harry Mellor <[email protected]>
Signed-off-by: 0xrushi <[email protected]>
@mergify mergify bot added ci/build deepseek Related to DeepSeek models frontend llama Related to Llama models multi-modality Related to multi-modality (#4194) new-model Requests to new models performance Performance-related issues qwen Related to Qwen models gpt-oss Related to GPT-OSS models rocm Related to AMD ROCm labels Oct 26, 2025
@mergify mergify bot added v1 tpu Related to Google TPUs tool-calling labels Oct 26, 2025
@mergify mergify bot added the kv-connector label Oct 26, 2025
@DarkLight1337
Copy link
Member

DarkLight1337 commented Oct 26, 2025

Someone else has already opened #27515, can you work with them on that PR instead?

Signed-off-by: 0xrushi <[email protected]>
@mergify
Copy link

mergify bot commented Oct 26, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @0xrushi.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Oct 26, 2025
@0xrushi
Copy link
Author

0xrushi commented Oct 26, 2025

Someone else has already opened #27515, can you work with them on that PR instead?

Oh I didn't notice, closing this

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

Labels

ci/build deepseek Related to DeepSeek models documentation Improvements or additions to documentation frontend gpt-oss Related to GPT-OSS models kv-connector llama Related to Llama models multi-modality Related to multi-modality (#4194) needs-rebase new-model Requests to new models performance Performance-related issues qwen Related to Qwen models rocm Related to AMD ROCm speculative-decoding structured-output tool-calling tpu Related to Google TPUs v1

Projects

Status: Done
Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]: Llama-3.2-1b-instruct prepending extra bos token