Skip to content

🚨 Image-text pipeline expects correctly formatted chat - #42359

Merged
zucchini-nlp merged 2 commits into
huggingface:mainfrom
zucchini-nlp:image-text-pipe-chat
Nov 26, 2025
Merged

zucchini-nlp merged 2 commits into
huggingface:mainfrom
zucchini-nlp:image-text-pipe-chat

Conversation

@zucchini-nlp

Copy link
Copy Markdown
Member

What does this PR do?

As per title, we can break for v5 and enforce a correct usage of chat templates. I will request review after adjusting tests

@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.

@zucchini-nlp

Copy link
Copy Markdown
Member Author

cc @ebezzam as discussed internally. I didn't move the Chat anywhere in this PR, ig it's already a part of the PR for text-to-audio

@molbap molbap left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Way cleaner, thanks 👍

Comment on lines +203 to +211
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What’s the difference between these two images?"},
{"type": "image", "url": image_ny},
{"type": "image", "url": image_chicago},
],
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

much better. It's already documented but shall we advertise also "path" here in addition to "url"? Just for completeness.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The chat template are tested with local path and url in processing tests. Since the pipeline internally only calls apply_chat_template, I think it isn't necessary

@yonigozlan yonigozlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice to see this cleaned up! I don't see anything wrong with removing that for v5 if everything is now correctly handled inside chat templates.

Comment on lines +285 to +292
if images is not None:
raise ValueError(
"Invalid input: you passed `chat` and `images` as separate input arguments. "
"Images must be placed inside the chat message's `content`. For example, "
"'content': ["
" {'type': 'image', 'url': 'image_url'}, {'type': 'text', 'text': 'Describe the image.'}}"
"]"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we mention that this is a deprecated behavior in >v5? Otherwise users might not understand why their previously working code is now crashing

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, I merged assuming I've read all comments. Sorry. Yeah, saying it's broken for v5 might be more clear, ig users will realize v5 broke a lot of stuff 🙃

@Rocketknight1

Copy link
Copy Markdown
Member

Big fan of this cleanup!

@zucchini-nlp
zucchini-nlp merged commit 9c1082a into huggingface:main Nov 26, 2025
23 checks passed

@ebezzam ebezzam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@zucchini-nlp I think there's still a fix needed to handle chat templates with URL?

self.messages = messages


def add_images_to_messages(messages: dict, images: Union[str, list[str], "Image.Image", list["Image.Image"]] | None):

@ebezzam ebezzam Nov 26, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@zucchini-nlp I don't think the standardization is as simple as removing this function causes unfortunately :/

As this test to fails because add_images_to_messages was actually modifying the chat template when there were URLs

# RUN_SLOW=1 pytest tests/pipelines/test_pipelines_image_text_to_text.py::ImageTextToTextPipelineTests::test_model_pt_chat_template_image_url

    @slow
    @require_torch
    def test_model_pt_chat_template_image_url(self):
        pipe = pipeline("image-text-to-text", model="llava-hf/llava-interleave-qwen-0.5b-hf")
        messages = [
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
                        },
                    },
                    {"type": "text", "text": "Describe this image in one sentence."},
                ],
            }
        ]
        outputs = pipe(text=messages, return_full_text=False, max_new_tokens=10)[0]["generated_text"]
>       self.assertEqual(outputs, "A statue of liberty in the foreground of a city")
E       AssertionError: "A close-up of a person's face with a" != 'A statue of liberty in the foreground of a city'
E       - A close-up of a person's face with a
E       + A statue of liberty in the foreground of a city

tests/pipelines/test_pipelines_image_text_to_text.py:380: AssertionError

"The number of images in the chat messages should be the same as the number of images passed to the pipeline."
)
# Add support for OpenAI/TGI chat format
elif content_type == "image_url":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this was the part rewrittting

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixing it to support OpenAI format in pipelines

SangbumChoi pushed a commit to SangbumChoi/transformers that referenced this pull request Jan 23, 2026
…2359)

* don't support images as separate arg

* adjust the test case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants