Skip to content

Simplify role handling in prepare_multimodal_messages#5508

Merged
albertvillanova merged 1 commit into
huggingface:mainfrom
albertvillanova:fu-5496
Apr 13, 2026
Merged

Simplify role handling in prepare_multimodal_messages#5508
albertvillanova merged 1 commit into
huggingface:mainfrom
albertvillanova:fu-5496

Conversation

@albertvillanova

@albertvillanova albertvillanova commented Apr 10, 2026

Copy link
Copy Markdown
Member

Simplify role handling in prepare_multimodal_messages.

Follow-up to:

This PR simplifies and consolidates the logic in the prepare_multimodal_messages function for handling different message roles. The main change is that the preparation of message content for the assistant, system, and tool roles is now handled together, reducing code duplication and improving maintainability.

Changes

Refactoring and logic simplification:

  • Consolidated the handling of assistant, system, and tool message roles into a single code block, reducing repeated code for preparing message content as a list of text entries.
  • Removed the special-casing for the system role at the top of the function, as it is now handled together with the other roles.

Note

Low Risk
Low risk refactor that consolidates per-role branching without changing the public API; main risk is subtle differences in how string content is wrapped for non-user roles.

Overview
Simplifies prepare_multimodal_messages by consolidating the system, assistant, and tool role handling into a single branch that wraps string content into structured [{"type": "text", ...}] blocks.

Keeps the user-role logic for injecting image placeholders before the first user message, while reducing duplicated role-specific code paths.

Reviewed by Cursor Bugbot for commit ac41714. Bugbot is set up for automated code reviews on this repo. Configure here.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@qgallouedec qgallouedec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks good, I'm sure it's on your radar, but tool could contain images now

@albertvillanova albertvillanova merged commit 80eb47d into huggingface:main Apr 13, 2026
10 of 13 checks passed
@albertvillanova

Copy link
Copy Markdown
Member Author

tool could contain images now

Yes, @qgallouedec, I think that tool images are already in-lined (not using image placeholders). A somehow related enhancement is addressed in:

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants