fix(models): handle string content when merging consecutive same-role messages#2220
Open
VANDRANKI wants to merge 1 commit intohuggingface:mainfrom
Open
fix(models): handle string content when merging consecutive same-role messages#2220VANDRANKI wants to merge 1 commit intohuggingface:mainfrom
VANDRANKI wants to merge 1 commit intohuggingface:mainfrom
Conversation
… messages
get_clean_message_list merges consecutive messages that share the same role.
The merge path assumed message.content is always a list, but messages passed
as plain dicts with a string "content" field (e.g. system messages like
{"role": "system", "content": "..."}) store content as a str after from_dict.
This caused an AssertionError when two consecutive system messages were in
the input list.
Normalize string content to [{"type": "text", "text": ...}] for both the
incoming message and the previously-appended message before the merge, so
the existing list-based merge logic handles all cases uniformly.
Fixes huggingface#1972
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.
Summary
get_clean_message_listraisesAssertionErrorwhen the input contains two or more consecutive messages with the same role (e.g. two system messages) and those messages have plain string content.Reproducer from issue #1972:
Root cause
ChatMessage.contentisstr | list[dict] | None. When messages are passed as plain dicts with a string"content"field,from_dictstores content as astr. The merge branch (hit when two consecutive messages share the same role) immediately assertsisinstance(message.content, list)without handling the string case, causing the crash.Fix
Before attempting the merge, normalize both the incoming
message.contentand the previously-storedoutput_message_list[-1]["content"]to[{"type": "text", "text": ...}]if either is a string. The existing list-based merge logic then handles all cases uniformly, and consecutive string-content system messages are concatenated with a newline.No behavior change for callers that already pass list-format content.
Fixes #1972