-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Fix continue_final_message
in apply_chat_template
to prevent substring matching issues
#40732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix continue_final_message
in apply_chat_template
to prevent substring matching issues
#40732
Conversation
@Rocketknight1 I made the PR as we discussed on #40687 The main issue (which make the tests fails) is that when the temple end with "trim" instruction. Adding the TAG make it impossible to do the Edit: now I created workaround for it and it worked |
…aseb/transformers into fix_continue_final_message
@Rocketknight1 I make a workaround to solve the trim problem. Update: Now it works for evey case |
|
@usepr It work now with and without the trim, you can work with this branch if you want |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, LGTM now! I think the tests cover most of this, so if they're passing then the new method works. Hopefully this resolves the last of the edge cases!
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
What does this PR do?
This PR fixes an edge case in
apply_chat_template
where thecontinue_final_message
parameter could behave unexpectedly if the final message is empty or happens to match a substring in the rendered template.Previously, the function relied on
rindex(final_message.strip())
to locate the final message in the rendered chat. This caused incorrect truncation when the content was empty or matched other parts of the template.To resolve this, we introduce a special internal marker (
CONTINUE_FINAL_MESSAGE_TAG
) that is appended to the final message during rendering. After rendering, the template is safely split at this tag, and the marker is removed. This ensures correct and predictable behavior in all cases.Fixes #40687
Motivation and Context
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@Rocketknight1 @ArthurZucker