Skip to content
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 edge case for continue_final_message #36404

Merged
merged 6 commits into from
Mar 3, 2025

Conversation

Rocketknight1
Copy link
Member

@Rocketknight1 Rocketknight1 commented Feb 25, 2025

The code for continue_final_message in apply_chat_template fails in the following edge case:

  • The chat template trims trailing spaces from messages
  • The chat template adds its own spacing after messages
  • The unstripped content of the final message is identical to the stripped content of a previous message plus the template's built-in spacing

This causes the template to incorrectly identify the previous message as the final message and strip off a big chunk of the conversation after this point. This is a fairly uncommon edge case, but it's worth fixing nonetheless! cc @TK-21st

This PR handles things more elegantly without a try-except block, and also improves handling when assistant messages contain text/image blocks (which is rare right now, but will probably become more common in future)

Fixes #35433
Fixes #36440

@HuggingFaceDocBuilderDev

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.

@Rocketknight1
Copy link
Member Author

cc @Cyrilvallez @ArthurZucker for core maintainer review!

@Rocketknight1
Copy link
Member Author

Also added a clearer error message for the case described in #36440 - this should also be quite rare.

Copy link
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

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

Thanks a lot! LGTM apart from the iteration that should be removed, see comment 🤗

Comment on lines 1705 to 1714
for content_block in reversed(final_message):
if "text" in content_block:
# Pick the last text block in the message (the first one we hit while iterating in reverse)
final_message = content_block["text"]
break
else:
raise ValueError(
"continue_final_message is set but we could not find any text to continue"
"in the final message!"
)
Copy link
Member

Choose a reason for hiding this comment

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

We should not need to iterate here as we are either breaking or raising on first iter no?

Copy link
Member Author

Choose a reason for hiding this comment

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

This was actually a massive bug! I think the only reason it didn't cause tests to fail was because of the faulty commit you mentioned below.

The else block is incorrectly indented - this should be a for/else loop, so the error is only raised if no blocks have a text field.

@Cyrilvallez
Copy link
Member

Also, please make sure to rebase to properly trigger tests as this one also has its branch pointing just after the faulty commit!

@Rocketknight1 Rocketknight1 force-pushed the fix-continue-final-message-edge-case branch from 4c05a70 to 9d0467a Compare March 3, 2025 16:58
@Rocketknight1 Rocketknight1 force-pushed the fix-continue-final-message-edge-case branch from 9d0467a to 7b4b5da Compare March 3, 2025 17:38
@Rocketknight1 Rocketknight1 merged commit 1975be4 into main Mar 3, 2025
24 checks passed
@Rocketknight1 Rocketknight1 deleted the fix-continue-final-message-edge-case branch March 3, 2025 18:03
garrett361 pushed a commit to garrett361/transformers that referenced this pull request Mar 4, 2025
* Fix edge case for continue_final_message

* lstrip() correctly

* Add regression test

* Add a clearer error message when the final message is not present

* Add a clearer error message when the final message is not present

* Fix massive bug!
garrett361 pushed a commit to garrett361/transformers that referenced this pull request Mar 4, 2025
* Fix edge case for continue_final_message

* lstrip() correctly

* Add regression test

* Add a clearer error message when the final message is not present

* Add a clearer error message when the final message is not present

* Fix massive bug!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants