Repository navigation
Fix for tokenizer.apply_chat_template with continue_final_message=True - #34214
Merged
Rocketknight1 merged 3 commits intoOct 17, 2024
Merged
Rocketknight1 merged 3 commits into
Rocketknight1 merged 3 commits into
Conversation
Contributor
Author
|
Alternative or possibly more robust way would be to only do the stripping if the full final message string cannot be found in the rendered chat as is. |
Rocketknight1
approved these changes
Oct 17, 2024
Member
There was a problem hiding this comment.
@schoennenbeck this is a really good fix, thank you! I think we don't need the more robust solution - because of how whitespace is tokenized, I think it will be quite hard to end your message in multiple spaces and still get a good continuation.
(We're having some CI issues, but hopefully we'll resolve them later today and then I can merge this)
Member
|
@schoennenbeck merged! Thanks again for a clean and helpful PR! |
1 of 5 tasks
BernardZach
pushed a commit
to BernardZach/transformers
that referenced
this pull request
Dec 5, 2024
huggingface#34214) * Strip final message * Do full strip instead of rstrip * Retrigger CI --------- Co-authored-by: Matt <rocketknight1@gmail.com>
This was referenced Sep 15, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For a lot of tokenizers in
Tokenizer.apply_chat_templatewithcontinue_final_message=Truewe get a "ValueError: substring not found" if the final message starts or ends in some whitespace. Here is some example code that exhibits the issue:This is due to the fact that the
apply_chat_template-method looks for the full final message in the rendered chat but many modern chat templates (in particular the Llama3.1-chat-template) actually trim messages before rendering.This PR strips the final message before looking it up in the rendered string. This fixes the issue. However, this means that the continuation can now happen at a slightly different spot than the user intended. I believe this is the best way to address this issue but I would also be open to simply raise a more descriptive error in case this happens so the user can strip the last message themselves to handle this.
Either way I think the current failure mode is not ideal.
If required I could also add a test for this behaviour.
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
Potential reviewers (based on affected area)