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 double curly braces to insert values:

Use dot notation to access nested properties:

Comments are not rendered in the output:

OperatorDescriptionExample
+Addition{{ a + b }}
-Subtraction{{ a - b }}
*Multiplication{{ a * b }}
/Division{{ a / b }}
//Floor division{{ a // b }}
%Modulo{{ a % b }}
**Power{{ a ** 2 }}
OperatorDescriptionExample
andLogical AND{% if a and b %}
orLogical OR{% if a or b %}
notLogical NOT{% if not a %}
OperatorDescriptionExample
==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 %}
inMembership test{% if item in items %}
not inNegated membership{% if item not in items %}
OperatorDescriptionExample
-Negative{{ -value }}
+Positive{{ +value }}

Example:

With index:

Loop variables:

VariableDescription
loop.indexCurrent iteration (1-indexed)
loop.index0Current iteration (0-indexed)
loop.firstTrue if first iteration
loop.lastTrue if last iteration
loop.lengthTotal 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:

FunctionDescriptionExample
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) }}
FunctionDescriptionExample
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) %}
FunctionDescriptionExample
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:

FilterDescriptionExample
default(value)Provide default if undefined/none`{{ name
trimRemove leading/trailing whitespace`{{ text
upperConvert to uppercase`{{ name
lowerConvert to lowercase`{{ name
lengthGet length of string/list`{{ items
firstGet first item`{{ items
lastGet 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:

TestDescriptionExample
noneValue is None{% if value is none %}
definedVariable exists{% if name is defined %}
undefinedVariable doesn’t exist{% if name is undefined %}
trueValue is true{% if flag is true %}
falseValue is false{% if flag is false %}
stringValue is a string{% if value is string %}
numberValue is a number{% if value is number %}
integerValue is an integer{% if value is integer %}
floatValue is a float{% if value is float %}
sequenceValue is a sequence{% if items is sequence %}
mappingValue is a dict{% if data is mapping %}
iterableValue is iterable{% if items is iterable %}
evenNumber is even{% if num is even %}
oddNumber 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:

SyntaxEffect
{%-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: