From 48a34e21c3f50a54572435c7bdaaa81b45b9742e Mon Sep 17 00:00:00 2001 From: HuaTNA Date: Wed, 23 Sep 2026 13:22:36 -0400 Subject: [PATCH] Preserve falsy TemplateTask rule matches (#1261) --- src/python/txtai/workflow/task/template.py | 2 +- test/python/testworkflow.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/python/txtai/workflow/task/template.py b/src/python/txtai/workflow/task/template.py index 2fae880d..c8522a5f 100644 --- a/src/python/txtai/workflow/task/template.py +++ b/src/python/txtai/workflow/task/template.py @@ -37,7 +37,7 @@ class TemplateTask(Task): def prepare(self, element): # Check if element matches any processing rules match = self.match(element) - if match: + if match is not None: return match # Apply template processing, if applicable diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py index 9c1d2754..a335e24c 100644 --- a/test/python/testworkflow.py +++ b/test/python/testworkflow.py @@ -485,6 +485,26 @@ class TestWorkflow(unittest.TestCase): results = list(workflow([{"text": "prompt"}])) self.assertEqual(results[0], "This is a prompt") + def testTemplateRulesFalsy(self): + """ + Test that falsy rule matches bypass template formatting + """ + + for value in ("", 0, False): + with self.subTest(value=value): + workflow = Workflow([TemplateTask(template="This is a {text}", rules={"text": value})]) + results = list(workflow([{"text": value}, {"text": "prompt"}])) + self.assertEqual(results, [value, "This is a prompt"]) + self.assertIs(type(results[0]), type(value)) + + def testTemplateRulesMissingField(self): + """ + Test that a falsy rule match does not require template fields + """ + + workflow = Workflow([TemplateTask(template="This is a {text}", rules={"status": ""})]) + self.assertEqual(list(workflow([{"status": ""}])), [""]) + def testTemplateRag(self): """ Test rag template task