From d33a1c389f737a87471c64138be30bf5c59b5e8a Mon Sep 17 00:00:00 2001 From: Raushan Turganbay Date: Wed, 16 Jul 2025 14:31:35 +0500 Subject: [PATCH] [chat template] add a testcase for kwargs (#39415) add a testcase --- tests/test_processing_common.py | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/test_processing_common.py b/tests/test_processing_common.py index 855bcaaf27..f67a9e49f9 100644 --- a/tests/test_processing_common.py +++ b/tests/test_processing_common.py @@ -1110,3 +1110,42 @@ class ProcessorTesterMixin: self.assertEqual(len(out_dict["attention_mask"]), 1) # batch-size=1 self.assertEqual(len(out_dict[self.audio_input_name]), 1) # 1 audio in the conversation self.assertEqual(len(out_dict[self.videos_input_name]), 1) # 1 video in the conversation + + def test_chat_template_jinja_kwargs(self): + """Tests that users can pass any kwargs and they will be used in jinja templates.""" + processor = self.get_processor() + if processor.chat_template is None: + self.skipTest("Processor has no chat template") + + messages = [ + { + "role": "user", + "content": [ + {"type": "text", "text": "Which of these animals is making the sound?"}, + ], + }, + { + "role": "assistant", + "content": [{"type": "text", "text": "It is a cow."}], + }, + ] + + dummy_template = ( + "{% for message in messages %}" + "{% if add_system_prompt %}" + "{{'You are a helpful assistant.'}}" + "{% endif %}" + "{% if (message['role'] != 'assistant') %}" + "{{'<|special_start|>' + message['role'] + '\n' + message['content'][0]['text'] + '<|special_end|>' + '\n'}}" + "{% elif (message['role'] == 'assistant')%}" + "{{'<|special_start|>' + message['role'] + '\n'}}" + "{{message['content'][0]['text'] + '<|special_end|>' + '\n'}}" + "{% endif %}" + "{% endfor %}" + ) + + formatted_prompt = processor.apply_chat_template( + messages, add_system_prompt=True, tokenize=False, chat_template=dummy_template + ) + expected_prompt = "You are a helpful assistant.<|special_start|>user\nWhich of these animals is making the sound?<|special_end|>\nYou are a helpful assistant.<|special_start|>assistant\nIt is a cow.<|special_end|>\n" + self.assertEqual(formatted_prompt, expected_prompt)