The Trailblazer Community in Slack is now in Open Beta—join and connect in real time. [Join us in Slack]
This guide covers all supported features for writing prompt templates in Prompt Builder.
Prompt templates in Prompt Builder are built on Jinja. At runtime, Prompt Builder populates the template from grounded data and evaluates expressions to include or exclude text in the completed prompt sent to the large language model. Prompt Builder uses the Block Editor. This topic describes Jinja syntax, the Jinja features Prompt Builder supports, and how Prompt Builder evaluates the language.
See Prompt Builder help pages for more information:
Use double curly braces to insert values:
1Hello {{ name }}!
2Welcome to {{ company }}.Use dot notation to access nested properties:
1{{ User.Name }}
2{{ Account.Address.City }}
3{{ Order.Items[0].Name }}Comments are not rendered in the output:
1{# This is a comment #}
2{{ name }} {# inline comment #}| Operator | Description | Example |
|---|---|---|
+ | Addition | {{ a + b }} |
- | Subtraction | {{ a - b }} |
* | Multiplication | {{ a * b }} |
/ | Division | {{ a / b }} |
// | Floor division | {{ a // b }} |
% | Modulo | {{ a % b }} |
** | Power | {{ a ** 2 }} |
| Operator | Description | Example |
|---|---|---|
and | Logical AND | {% if a and b %} |
or | Logical OR | {% if a or b %} |
not | Logical NOT | {% if not a %} |
| Operator | Description | Example |
|---|---|---|
== | Equal | {% if a == b %} |
!= | Not equal | {% if a != b %} |
> | Greater than | {% if a > b %} |
< | Less than | {% if a < b %} |
>= | Greater or equal | {% if a >= b %} |
<= | Less or equal | {% if a <= b %} |
in | Membership test | {% if item in items %} |
not in | Negated membership | {% if item not in items %} |
| Operator | Description | Example |
|---|---|---|
- | Negative | {{ -value }} |
+ | Positive | {{ +value }} |
1{% if condition %}
2 Content when true
3{% elif other_condition %}
4 Alternative content
5{% else %}
6 Default content
7{% endif %}Example:
1{% if User.Role == "admin" %}
2 You have full access.
3{% elif User.Role == "editor" %}
4 You can edit content.
5{% else %}
6 You have read-only access.
7{% endif %}1{% for item in items %}
2 {{ item }}
3{% endfor %}With index:
1{% for item in items %}
2 {{ loop.index }}. {{ item.name }}
3{% endfor %}Loop variables:
| Variable | Description |
|---|---|
loop.index | Current iteration (1-indexed) |
loop.index0 | Current iteration (0-indexed) |
loop.first | True if first iteration |
loop.last | True if last iteration |
loop.length | Total number of items |
Empty loop handling:
1{% for item in items %}
2 {{ item }}
3{% else %}
4 No items found.
5{% endfor %}Range loops:
1{% for i in range(5) %}
2 Item {{ i }}
3{% endfor %}Note: Range is limited to 1000 items maximum.
Loop controls:
You can use break and continue to control loop execution:
1{% for item in items %}
2 {% if item.skip %}
3 {% continue %}
4 {% endif %}
5 {% if item.stop %}
6 {% break %}
7 {% endif %}
8 {{ item.name }}
9{% endfor %}| Function | Description | Example |
|---|---|---|
str(value) | Convert to string | {{ str(123) }} |
int(value) | Convert to integer | {{ int("42") }} |
float(value) | Convert to float | {{ float("3.14") }} |
bool(value) | Convert to boolean | {{ bool(1) }} |
| Function | Description | Example |
|---|---|---|
list(value) | Convert to list | {{ list(items) }} |
dict() | Create dictionary | {{ dict(a=1, b=2) }} |
tuple(value) | Convert to tuple | {{ tuple(items) }} |
set(value) | Convert to set | {{ set(items) }} |
sorted(items) | Sort items | {{ sorted(names) }} |
reversed(items) | Reverse items | {{ reversed(items) }} |
zip(a, b) | Zip iterables | {% for x, y in zip(a, b) %} |
| Function | Description | Example |
|---|---|---|
len(value) | Get length | {{ len(items) }} |
max(items) | Maximum value | {{ max(scores) }} |
min(items) | Minimum value | {{ min(scores) }} |
sum(items) | Sum of items | {{ sum(prices) }} |
abs(value) | Absolute value | {{ abs(-5) }} |
round(value) | Round number | {{ round(3.7) }} |
range(n) | Generate range | {% for i in range(10) %} |
Filters modify values using the pipe (|) syntax:
1{{ value|filter }}
2{{ value|filter(arg) }}| Filter | Description | Example |
|---|---|---|
default(value) | Provide default if undefined/none | `{{ name |
trim | Remove leading/trailing whitespace | `{{ text |
upper | Convert to uppercase | `{{ name |
lower | Convert to lowercase | `{{ name |
length | Get length of string/list | `{{ items |
first | Get first item | `{{ items |
last | Get last item | `{{ items |
join(separator) | Join list into string | `{{ items |
selectattr(attr[, test, value]) | Filter a list of objects by an attribute | `{{ users |
Examples:
1{{ User.Name|default("Anonymous") }}
2{{ description|trim|upper }}
3{{ tags|join(", ") }}
4{{ items|first }}selectattr — pick items from a list by attribute value:
Without a test, selectattr keeps items whose attribute is truthy:
1{% for u in users|selectattr("active") %}{{ u.name }}{% endfor %}With a test (e.g. eq), selectattr keeps items where the attribute matches the given value. Combine with |first to pull a single item by id:
1{{ (products|selectattr("id", "eq", target_id)|first).name }}Context:
1{
2 "products": [
3 { "id": 1, "name": "laptop" },
4 { "id": 2, "name": "mouse" },
5 { "id": 3, "name": "keyboard" }
6 ],
7 "target_id": 2
8}Output:
1mouseTests check conditions using the is keyword:
1{% if value is test %}
2{% if value is not test %}| Test | Description | Example |
|---|---|---|
none | Value is None | {% if value is none %} |
defined | Variable exists | {% if name is defined %} |
undefined | Variable doesn’t exist | {% if name is undefined %} |
true | Value is true | {% if flag is true %} |
false | Value is false | {% if flag is false %} |
string | Value is a string | {% if value is string %} |
number | Value is a number | {% if value is number %} |
integer | Value is an integer | {% if value is integer %} |
float | Value is a float | {% if value is float %} |
sequence | Value is a sequence | {% if items is sequence %} |
mapping | Value is a dict | {% if data is mapping %} |
iterable | Value is iterable | {% if items is iterable %} |
even | Number is even | {% if num is even %} |
odd | Number is odd | {% if num is odd %} |
eq(value) | Value equals the given value (also usable as a selectattr test) | {% if status is eq("active") %} |
ne(value) | Value is not equal to the given value | {% if status is ne("inactive") %} |
gt(value) | Value is greater than the given value | {% if price is gt(100) %} |
ge(value) | Value is greater than or equal to the given value | {% if price is ge(100) %} |
lt(value) | Value is less than the given value | {% if price is lt(100) %} |
le(value) | Value is less than or equal to the given value | {% if price is le(100) %} |
When a value is None, it renders as a placeholder:
1{{ User.Name }}If User.Name is None, output:
1[Provide:{NAME}]1{% if value is none %}
2 Value is not provided
3{% else %}
4 Value: {{ value }}
5{% endif %}1{% if User.Email is not none %}
2 Contact: {{ User.Email }}
3{% endif %}By default, the newline after block tags ({% if %}, {% for %}, {% endif %}, etc.) is automatically removed. This keeps your output clean without extra blank lines.
For additional whitespace control, use - inside tags:
1{%- if condition -%} {# strips whitespace on both sides #}
2 content
3{%- endif -%}| Syntax | Effect |
|---|---|
{%- | Strip whitespace before |
-%} | Strip whitespace after |
{{- | Strip whitespace before |
-}} | Strip whitespace after |
1{{ "Yes" if condition else "No" }}
2{{ value if value else "default" }}Use {% set %} to create or modify variables:
1{% set greeting = "Hello" %}
2{% set full_name = User.FirstName ~ " " ~ User.LastName %}
3{{ greeting }}, {{ full_name }}!Block assignment for multi-line content:
1{% set message %}
2 Welcome to {{ company }}.
3 Your account is {{ status }}.
4{% endset %}
5{{ message }}Create scoped variables that only exist within the block:
1{% with total = items|length %}
2 There are {{ total }} items.
3{% endwith %}Multiple variables:
1{% with first = items|first, last = items|last %}
2 First: {{ first }}, Last: {{ last }}
3{% endwith %}Create lists directly in templates:
1{% set colors = ["red", "green", "blue"] %}
2{% for color in colors %}
3 {{ color }}
4{% endfor %}Create dictionaries inline:
1{% set person = {"name": "Alice", "age": 30} %}
2{{ person.name }} is {{ person.age }} years old.Create tuples for immutable sequences:
1{% set point = (10, 20) %}
2X: {{ point[0] }}, Y: {{ point[1] }}Use the ~ operator to concatenate strings:
1{{ "Hello, " ~ name ~ "!" }}
2{% set full_name = first ~ " " ~ last %}Access portions of sequences using slice notation:
1{{ items[0] }} {# First item #}
2{{ items[-1] }} {# Last item #}
3{{ items[1:3] }} {# Items 1 and 2 #}
4{{ items[:3] }} {# First 3 items #}
5{{ items[2:] }} {# From item 2 to end #}
6{{ name[0:5] }} {# First 5 characters #}Use {% do %} to execute expressions without output (useful for method calls):
1{% do items.append(new_item) %}
2{% do log.debug("Processing complete") %}Template:
1Hello {{ User.Name }}!
2Your role is {{ User.Role }}.Context:
1{ "User": { "Name": "Alice", "Role": "admin" } }Output:
1Hello Alice!
2Your role is admin.Template:
1Hello {{ User.Name }}!
2{% if User.Role == "admin" %}
3You have administrator privileges.
4{% endif %}
5Welcome to the system.Context:
1{ "User": { "Name": "Alice", "Role": "admin" } }Output:
1Hello Alice!
2You have administrator privileges.
3Welcome to the system.Template:
1Your order contains {{ len(items) }} items:
2{% for item in items %}
3- {{ item.Name }}: ${{ item.Price }}
4{% endfor %}Context:
1{
2 "items": [
3 { "Name": "Widget", "Price": 10 },
4 { "Name": "Gadget", "Price": 25 }
5 ]
6}Output:
1Your order contains 2 items:
2- Widget: $10
3- Gadget: $25Template:
1Name: {{ User.Name }}
2Email: {{ User.Email }}Context:
1{ "User": { "Name": "Alice", "Email": null } }Output:
1Name: Alice
2Email: [Provide:{EMAIL}]Template:
1{% if Account.Industry is not none %}
2Industry: {{ Account.Industry }}
3{% endif %}
4{% if Account.Website is not none %}
5Website: {{ Account.Website }}
6{% endif %}Context:
1{ "Account": { "Industry": "Technology", "Website": null } }Output:
1Industry: Technology