core/text: a template that cannot render a conversation is refused at load, not thrown

ChatTemplate::load ran the thinking probe, the first full render, outside any handler; a template
that compiles but fails in the reply-format analysis (here llama.cpp's own DeepSeek-V4.1 template,
which calls dict()) threw out of load and ended the process. It is now RAD_E_FORMAT with the
template's words, as a failing render already is per request.
This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-27 18:15:58 +01:00
1 parent fa8c746497
commit 70639f7ea2
2 files changed
+33 -9

No files matched your search

+23 -9
View File
@@ -24,6 +24,14 @@ struct ChatTemplate::Impl {
bool add_bos = false, add_eos = false;
};
static std::string template_words(const char* what) {
static const char kWrap[] = "Unable to generate parser for this template. Automatic parser "
"generation failed: ";
std::string w = what ? what : "";
if (w.compare(0, sizeof kWrap - 1, kWrap) == 0) w.erase(0, sizeof kWrap - 1);
return w;
}
ChatTemplate::ChatTemplate() : impl_(std::make_unique<Impl>()) {}
ChatTemplate::~ChatTemplate() = default;
@@ -90,11 +98,24 @@ int ChatTemplate::load(const std::string& jinja_source, const std::string& bos,
impl_->caps.typed_content = get("supports_typed_content", false);
impl_->caps.object_arguments = get("supports_object_arguments", false);
/* The thinking probe is the first full render, through the reply-format analysis every render
* runs. A template that compiles but cannot render a one-message conversation there (it calls
* a function this engine lacks, or the analysis cannot generate its parser) would fail every
* request the same way; it is refused here as a value, since the throw would end the
* process. */
bool thinking = false;
try {
thinking = common_chat_templates_support_enable_thinking(impl_->tmpls.get());
} catch (const std::exception& e) {
RAD_ERR("chat: the template compiles but does not render a conversation: %s",
template_words(e.what()).c_str());
impl_->tmpls.reset();
return RAD_E_FORMAT;
}
RAD_INFO("chat: template loaded (%zu bytes)%s; tools=%d tool_calls=%d system=%d thinking=%d",
src.size(), fallback ? " [ChatML fallback]" : "",
(int)impl_->caps.tools, (int)impl_->caps.tool_calls,
(int)impl_->caps.system_role,
(int)common_chat_templates_support_enable_thinking(impl_->tmpls.get()));
(int)impl_->caps.system_role, (int)thinking);
return RAD_OK;
}
@@ -171,13 +192,6 @@ static bool lead_system(const ojson& messages, ojson* out) {
/* The template's own words: the lifted autoparser wraps whatever the render threw in a sentence
* about parser generation, which the render's failure is not. */
static std::string template_words(const char* what) {
static const char kWrap[] = "Unable to generate parser for this template. Automatic parser "
"generation failed: ";
std::string w = what ? what : "";
if (w.compare(0, sizeof kWrap - 1, kWrap) == 0) w.erase(0, sizeof kWrap - 1);
return w;
}
int ChatTemplate::apply(const ojson& messages, const ojson& tools, const ChatOptions& opt,
ChatPrompt* out) const {
+10
View File
@@ -1150,6 +1150,16 @@ TEST(a_template_that_does_not_parse_is_a_value_and_not_a_throw) {
CHECK(!t.ready());
}
/* A template that compiles but cannot render a conversation (here it calls a function the engine
* does not have) is refused at load as a value too: the first full render, the thinking probe,
* used to throw out of load() and end the process. */
TEST(a_template_that_cannot_render_is_refused_at_load_and_not_thrown) {
ChatTemplate t;
CHECK(t.load("{%- for m in messages -%}{{- dict(m, x=1).content -}}{%- endfor -%}", "", "") ==
RAD_E_FORMAT);
CHECK(!t.ready());
}
/* Messages come from an HTTP request, so any malformed shape is refused, never thrown. */
TEST(a_message_list_that_is_not_one_is_refused_rather_than_thrown) {
ChatTemplate t;