Skip to content

Commit 037b1c8

Browse files
Verify not empty rendered chat template (#1574)
Co-authored-by: Ilya Lavrenov <ilya.lavrenov@intel.com>
1 parent cd46e19 commit 037b1c8

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/cpp/src/tokenizer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,18 @@ class Tokenizer::TokenizerImpl {
575575
{"slice", slice_callable},
576576
};
577577

578+
std::string result;
578579
try {
579-
return tpl.RenderAsString(params).value();
580+
result = tpl.RenderAsString(params).value();
580581
} catch (const std::exception& error) {
581582
OPENVINO_THROW("Chat template for the current model is not supported by Jinja2Cpp. "
582583
"Please apply template manually to your prompt before calling generate. "
583584
"For example: <start_of_turn>user{user_prompt}<end_of_turn><start_of_turn>model");
584585
}
586+
OPENVINO_ASSERT(!result.empty(), "Applied chat template resulted in an empty string. "
587+
"Please check the chat template or apply template manually to your prompt before calling generate."
588+
"For example: <start_of_turn>user{user_prompt}<end_of_turn><start_of_turn>model");
589+
return result;
585590
}
586591

587592
void set_chat_template(const std::string& chat_template) {

tests/python_tests/test_tokenizer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ def test_apply_chat_template(model_tmp_path, chat_config: Tuple[str, Dict]):
181181
print(f'ov_genai out: {ov_full_history_str}')
182182
assert ov_full_history_str == hf_full_history_str
183183

184+
# Test throwing exception for empty rendered chat template
185+
# Example: Qwen2-VL chat template
186+
chat_template_for_empty_output = "{% if messages is string %}{{ messages }}{% else %}{% for content in messages %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'video' or 'video' in content %}<|vision_start|><|video_pad|><|vision_end|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}{% endif %}"
187+
with pytest.raises(Exception):
188+
ov_tokenizer.apply_chat_template(conversation, chat_template=chat_template_for_empty_output)
189+
184190

185191
@pytest.mark.precommit
186192
@pytest.mark.nightly

0 commit comments

Comments
 (0)