Fix Issue #40913: Respect user-provided chat_template parameter in processor creation#40954
Open
qizwiz wants to merge 1 commit intohuggingface:mainfrom
Open
Fix Issue #40913: Respect user-provided chat_template parameter in processor creation#40954qizwiz wants to merge 1 commit intohuggingface:mainfrom
qizwiz wants to merge 1 commit intohuggingface:mainfrom
Conversation
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
4 tasks
Member
zucchini-nlp
left a comment
There was a problem hiding this comment.
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!") |
| 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: |
Member
There was a problem hiding this comment.
can do kwargs.set_default("chat_template", chat_templates)
This was referenced Apr 29, 2026
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.
This PR fixes a bug where user-provided
chat_templateparameters were being overwritten by model defaults when creating a processor. The fix ensures that user-provided values take precedence over model defaults.Fixes #40913
Problem
When creating a processor with a user-provided
chat_templateparameter, the value was being overwritten by the model's default chat template. This prevented users from customizing the chat template as intended.Reproduction:
Root Cause
In
src/transformers/processing_utils.pyline 1086, the code was unconditionally setting:This overwrote any user-provided
chat_templateparameter.Solution
The fix modifies the condition to only set the chat template from model files if no user-provided value exists:
This ensures that:
chat_templatevalues take precedenceTesting
Added a test case that verifies:
Impact
This is a backward-compatible bug fix that allows users to override chat templates as expected when loading processors.