Jinja in Prompt Builder Guide
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 Conditional Logic in Prompt Templates
- Add Conditional Logic to a Prompt Template
- Conditional Logic Example Templates
Use double curly braces to insert values:
Use dot notation to access nested properties:
Comments are not rendered in the output:
| 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 }} |
Example:
With index:
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:
Range loops:
Note: Range is limited to 1000 items maximum.
Loop controls:
You can use break and continue to control loop execution:
| 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:
| 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:
selectattr — pick items from a list by attribute value:
Without a test, selectattr keeps items whose attribute is truthy:
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:
Context:
Output:
Tests check conditions using the is keyword:
| 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:
If User.Name is None, output:
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:
| Syntax | Effect |
|---|---|
{%- | Strip whitespace before |
-%} | Strip whitespace after |
{{- | Strip whitespace before |
-}} | Strip whitespace after |
Use {% set %} to create or modify variables:
Block assignment for multi-line content:
Create scoped variables that only exist within the block:
Multiple variables:
Create lists directly in templates:
Create dictionaries inline:
Create tuples for immutable sequences:
Use the ~ operator to concatenate strings:
Access portions of sequences using slice notation:
Use {% do %} to execute expressions without output (useful for method calls):
Template:
Context:
Output:
Template:
Context:
Output:
Template:
Context:
Output:
Template:
Context:
Output:
Template:
Context:
Output: