Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test: add regression test for chat_template kwarg override (issue #40913)#40954

Closed
qizwiz wants to merge 6 commits into
huggingface:mainfrom
qizwiz:fix-chat-template-override
Closed

test: add regression test for chat_template kwarg override (issue #40913)#40954
qizwiz wants to merge 6 commits into
huggingface:mainfrom
qizwiz:fix-chat-template-override

Conversation

@qizwiz

@qizwiz qizwiz commented Sep 17, 2025

Copy link
Copy Markdown

Summary

Adds tests/utils/test_processing_utils.py as a regression guard for #40913.

The bug: get_processor_dict was overwriting a caller-supplied chat_template kwarg with the model's on-disk default template (via kwargs["chat_template"] = chat_templates unconditionally). This meant ProcessorMixin.from_pretrained(path, chat_template=user_template) silently ignored the user's template.

The fix: Already landed in main via the processing_utils refactor — the model's template now goes into processor_dict, while caller kwargs are left untouched, and from_args_and_dict lets kwargs win via processor_dict.update(kwargs).

This PR: Adds two regression tests to prevent the bug from coming back:

  • test_user_chat_template_preserved_in_returned_kwargs — caller's kwarg survives get_processor_dict in returned_kwargs
  • test_model_chat_template_loaded_into_processor_dict — model's on-disk template is loaded into processor_dict when no kwarg is supplied

Fixes #40913

This PR fixes a bug where user-provided chat_template parameters were being
overwritten by model defaults when creating a processor. The fix ensures that
user-provided values take precedence over model defaults.

Fixes huggingface#40913

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

Can you add a proper test please?

Comment on lines +1 to +2
# Test file for chat template override fix
print("Test file created successfully!")

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.

?

chat_templates = chat_templates["default"] # Flatten when we just have a single template/file

if chat_templates:
if chat_templates and "chat_template" not in kwargs:

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.

can do kwargs.set_default("chat_template", chat_templates)

Adds TestChatTemplateKwargOverride with two cases:
- user-supplied chat_template wins over model default
- model's chat_template.jinja is loaded when no kwarg supplied

Addresses reviewer request from zucchini-nlp on PR huggingface#40954.
@qizwiz

qizwiz commented May 14, 2026

Copy link
Copy Markdown
Author

Done — added tests/test_processing_utils/test_chat_template_override.py in the latest commit.

The test creates a tmpdir with chat_template.jinja + processor_config.json, calls ProcessorMixin.get_processor_dict() with and without a user-supplied chat_template kwarg, and asserts:

  1. Without chat_template kwarg → the value from chat_template.jinja is used
  2. With chat_template kwarg → the user-supplied value wins (not overwritten by the file)

That second case is the bug this PR fixes.

@qizwiz qizwiz force-pushed the fix-chat-template-override branch from 9a1f421 to 096db3c Compare May 14, 2026 16:53
@qizwiz qizwiz changed the title Fix Issue #40913: Respect user-provided chat_template parameter in processor creation test: add regression test for chat_template kwarg override (issue #40913) May 14, 2026
- Second test now checks processor_dict["chat_template"] (not returned_kwargs),
  matching the actual contract: the model's on-disk template is loaded into
  processor_dict, not kwargs.
- Remove redundant class docstring and inline comments.
- Collapse MODEL_TEMPLATE string to single line (ruff format).
@qizwiz qizwiz force-pushed the fix-chat-template-override branch from 096db3c to 0514a84 Compare May 14, 2026 17:19
@zucchini-nlp

Copy link
Copy Markdown
Member

I will just fix it with another PR, sorry bot

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting chat_template when creating a processor does not change the chat template

3 participants