Skip to content

fix(chat_format): keep text of list-typed message content in built-in formats - #2448

Open
sohailshk wants to merge 1 commit into
abetlen:mainfrom
sohailshk:fix/list-content-parts
Open

sohailshk wants to merge 1 commit into
abetlen:mainfrom
sohailshk:fix/list-content-parts

Conversation

@sohailshk

Copy link
Copy Markdown

The built-in chat formatters drop the text of a message whose content is a list of text parts, which is the OpenAI content-part form that llama_types allows for user messages.

Related: #1080

Repro:

from llama_cpp.llama_chat_format import format_llama3
format_llama3(messages=[{"role": "user", "content": [{"type": "text", "text": "Hello there"}]}]).prompt
# '<|start_header_id|>user<|end_header_id|>\n\n<|start_header_id|>assistant<|end_header_id|>\n\n'
  • Actual: on main, list-typed user content is dropped in all 23 built-in formats registered with register_chat_format. List-typed assistant content is dropped in 22 of them; format_mistral_instruct raises TypeError on it instead.
  • Actual for a list-typed system message: 11 formats (llama-2, qwen, oasst_llama, baichuan-2, baichuan, mistrallite, zephyr, pygmalion, chatml, chatglm3, openchat) insert it as a Python list repr ([{'type': 'text', 'text': ...}]), and alpaca, redpajama-incite and snoozy raise TypeError.
  • Expected: the text parts are used, giving the same prompt as for the equivalent string content.

Fix: _map_roles, _get_system_message and format_mistral_instruct read the text of a list content through a small _content_text helper. The text parts are joined with "", and non-text parts (for example image_url) are ignored. Content with no text parts (for example image-only content) contributes no text.

Test: test_chat_formatters_keep_text_of_content_parts checks that six formatters give the same prompt for list content (one user message also has an image_url part) and for string content.

Red/green: on main, 6 of the 8 tests in tests/test_llama_chat_format.py fail (the six new parametrized cases; 2 pass). With this change all 8 pass. A sweep over all 23 registered formats shows that list content gives the same prompt as string content, and that string, None and empty content prompts are unchanged from main (184 cases). ruff check and ruff format --check pass on the touched files. Changelog entry added under Unreleased.

Not run: tests that need a GGUF model, and the server tests.

The implementation and tests were written with AI assistance.

… formats

The built-in chat formatters only read string message content. A user
message whose content is a list of text parts (the OpenAI content-part
form, which llama_types allows) was dropped from the prompt, and the
mistral-instruct formatter raised TypeError on an assistant message with
list content.

Join the text parts of list content in _map_roles, _get_system_message
and format_mistral_instruct.

Add test_chat_formatters_keep_text_of_content_parts.
@sohailshk
sohailshk force-pushed the fix/list-content-parts branch from f8aaf50 to c4a49d9 Compare November 25, 2026 03:18

This branch has not been deployed

No deployments
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.

1 participant