Implementation:Neuml Txtai TemplateTask
| Knowledge Sources | |
|---|---|
| Domains | Workflow, Templating, LLM Prompting |
| Last Updated | 2026-02-10 01:00 GMT |
Overview
Concrete tool for generating text from templates and task inputs within workflows provided by txtai.
Description
The TemplateTask class extends the base Task class to generate text by applying Python format string templates to input data. It supports three input modes:
- dict input: Template placeholders are filled from dictionary keys (e.g.,
{name}maps toelement["name"]) - tuple input: Template placeholders use positional arguments (
{arg0},{arg1}, etc.) - string input: The element is substituted into a
{text}placeholder
The class also supports processing rules, which allow short-circuiting the template when a dict element matches a rule condition. In strict mode (default), all template placeholders must be consumed by the input; non-strict mode uses Python's standard Formatter which ignores missing keys. The module also includes RagTask, a subclass that prepares input for RAG pipelines by separating query and question fields.
Usage
Use TemplateTask in txtai workflows when you need to construct formatted text from structured data, such as generating LLM prompts, preparing input for downstream pipeline stages, or transforming data between workflow steps. It is particularly valuable for RAG workflows where questions need to be formatted with context before being sent to a language model.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File: src/python/txtai/workflow/task/template.py
Signature
class TemplateTask(Task):
def register(self, template=None, rules=None, strict=True)
def prepare(self, element)
def defaulttemplate(self)
def defaultrules(self)
def match(self, element)
class RagTask(TemplateTask):
def prepare(self, element)
Import
from txtai.workflow.task.template import TemplateTask, RagTask
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| elements | list | Yes | List of data elements (str, dict, or tuple) to apply the template to |
| executor | object | No | Optional executor instance for concurrent task actions |
| template | str | No (register) | Python format string template with placeholders; defaults to None (passthrough) |
| rules | dict | No (register) | Dictionary of key-value rules for conditional short-circuit matching; defaults to empty dict |
| strict | bool | No (register) | If True (default), all template placeholders must be consumed; if False, missing keys are ignored |
Outputs
| Name | Type | Description |
|---|---|---|
| results | list | List of formatted text strings generated by applying the template to each input element |
| results (RagTask) | list[dict] | List of dicts with "query" and "question" keys, where "question" has the template applied |
Usage Examples
from txtai.workflow.task.template import TemplateTask, RagTask
# String input with {text} placeholder
template = TemplateTask(
template="Summarize the following: {text}",
action=lambda x: x
)
results = template(["This is a long document about AI."])
# Returns ["Summarize the following: This is a long document about AI."]
# Dict input with named placeholders
template = TemplateTask(
template="Question: {question}\nContext: {context}",
action=lambda x: x
)
results = template([{"question": "What is AI?", "context": "AI stands for..."}])
# Returns ["Question: What is AI?\nContext: AI stands for..."]
# Tuple input with positional arguments
template = TemplateTask(
template="{arg0} is related to {arg1}",
action=lambda x: x
)
results = template([("Python", "programming")])
# RAG task for preparing retrieval-augmented generation input
rag = RagTask(
template="Answer this question: {text}",
action=lambda x: x
)
results = rag(["What is machine learning?"])
# Returns [{"query": "What is machine learning?",
# "question": "Answer this question: What is machine learning?"}]
# Using rules for conditional processing
template = TemplateTask(
template="Process: {text}",
rules={"type": "skip"},
action=lambda x: x
)
results = template([{"type": "skip", "text": "should be skipped"}])
# Returns ["skip"] (short-circuited by rule match)