Commit 2efe7f1d4 for llama.cpp
commit 2efe7f1d41f5c7403387e4e8fe1fa02a81e126ba
Author: frogscooper <frogscooper@gmail.com>
Date: Tue Dec 1 04:16:55 2026 -0500
jinja : make functions truthy and give them no attributes (#33419)
diff --git a/common/jinja/value.cpp b/common/jinja/value.cpp
index 330059726..e7a45ab0b 100644
--- a/common/jinja/value.cpp
+++ b/common/jinja/value.cpp
@@ -1571,6 +1571,12 @@ const func_builtins & value_undefined_t::get_builtins() const {
return builtins;
}
+const func_builtins & value_func_t::get_builtins() const {
+ // no attributes, so func.foo and func['foo'] are undefined
+ static const func_builtins no_builtins = {};
+ return no_builtins;
+}
+
//////////////////////////////////
diff --git a/common/jinja/value.h b/common/jinja/value.h
index 6843bf57a..70f8c37cc 100644
--- a/common/jinja/value.h
+++ b/common/jinja/value.h
@@ -770,7 +770,9 @@ struct value_func_t : public value_t {
return val_func(new_args);
}
virtual std::string type() const override { return "Function"; }
+ virtual bool as_bool() const override { return true; } // like python, a function is truthy
virtual std::string as_repr() const override { return type() + "<" + name + ">(" + (arg0 ? arg0->as_repr() : "") + ")"; }
+ virtual const func_builtins & get_builtins() const override;
virtual bool is_hashable() const override { return false; }
virtual hasher unique_hash() const noexcept override {
// Note: this is unused for now, we don't support function as object keys
diff --git a/tests/test-chat.cpp b/tests/test-chat.cpp
index 7726473d0..fab75335c 100644
--- a/tests/test-chat.cpp
+++ b/tests/test-chat.cpp
@@ -7817,6 +7817,13 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
.expect(message_assist_call_thoughts)
.run();
+ // Tool with an array param that has no "items" (#30781)
+ tst.test(" to=functions.todo_list<|channel|>analysis<|message|>{\"todos\": [\"buy milk\"]}")
+ .reasoning_format(COMMON_REASONING_FORMAT_AUTO)
+ .tools({ todo_list })
+ .expect(simple_assist_msg("", "", "todo_list", "{\"todos\": [\"buy milk\"]}"))
+ .run();
+
// Tool call with recipient in channel header: "<|channel|>analysis to=functions.NAME<|message|>JSON"
tst.test("<|channel|>analysis to=functions.special_function<|message|>{\"arg1\": 1}")
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp
index 120ec2447..c9220cc0d 100644
--- a/tests/test-jinja.cpp
+++ b/tests/test-jinja.cpp
@@ -389,6 +389,19 @@ static void test_conditionals(testing & t) {
{{"y", 1.0}},
"yes"
);
+
+ // a missing 'items' key falls back to the dict's items() function, which is truthy
+ test_template(t, "function is truthy",
+ "{{ 'yes' if p['items'] else 'no' }}",
+ {{"p", {{"type", "array"}}}},
+ "yes"
+ );
+
+ test_template(t, "subscript on a function is undefined",
+ "{% if p['items'] %}{{ p['items']['type'] is defined }}{% endif %}",
+ {{"p", {{"type", "array"}}}},
+ "False"
+ );
}
static void test_loops(testing & t) {