/
Smart Chain Engine
  • Ready for review
  • Smart Chain Engine

    Introduction

    The smart chain engine is the central component that allows for customization of every aspect of Prospera Labs system. Generally speaking, you should not need to interact with the smart chain engine unless you are trying to customize the behavior of a premade agent or build a custom agent from scratch.

    Features

    1. Provides an access point to customize behaviors and features deep within the Prospera Labs engine

    2. Provides ability for partners to customize agents on behalf of the customers that then log into their system

    3. A complete template engine to provide programmatic ways of setting options and building prompts

    4. Ability to tie multiple prompts together into prompt-chains, similar to tools like LangGraph or our own older FlowThought / prompt chart tool

    5. Able to use many different forms of AI prompting, from system prompts, conversational prompts, tool selections, structured JSON outputs, ranked selections with confidence scores, and more

    6. Provides direct access to LLM model inputs and outputs without processing by the surrounding system, allowing us to use pretty much any prompting technique that works with the underlying models

    7. Able to perform actions like writing data to the agents database, and incorporating data pulled from the database into prompts

    8. Able to quickly switch which particular smart-chain is used for what within the system through the binding system

    How it Works

    When the system is distilled down to its most basic essence, the smart chain system is just a simple text processing engine with emphasis on LLM based processing steps. A smart chain takes JSON data on the input, and produces JSON data as its output. The individual steps within a smart chain work the same way - taking JSON as their input and producing JSON as their output.

    Smart Chain Engine - High Level Overview.png

    Each time that a smart chain is invoked, it will go through the following process in order to compute the result:

    Smart Chain Engine.png

    Those steps are as follows:

    1. Some section of code submits for a smart chain to get executed, providing a specific binding_name and a PromptContext object. The prompt context object itself needs to be prefilled with several values, including a tenant_id, user_id and conversation_id (if needed).

    2. The system will invoke the get_data function on each of the smart chain data providers, accumulating a value for each one within the input data for the smart chain

    3. The system resolves the binding_name down to a specific smart chain. The process here is a bit more complex then whats described above, because there are also published versions of smart chains and built-in chains that are provided from code. But at the essence, we are trying to find the latest version available of the smart chain with a chain_name equal to what is set on the binding whose binding_name matches what was provided.

    4. Execution of the smart chain begins. Smart chains have multiple steps that are identical except that values can be fed from early steps into the input on later steps. Each step execution goes through the following processes:

      1. Compile the content template

      2. Run the content template, producing the content text

      3. Compile the options template

      4. Run the options template and then parse it as JSON, producing the options JSON object

      5. Execute the smart chain step by providing the accumulated data from the data providers and prior step outputs, the content, and the options. Each different type of smart chain step has different code associated with it.

      6. The result data from the smart chain step is then merged with the current input data, and the next step in the smart chain is executed

    5. The output of the last step in the sequence becomes the output for the smart chain as a whole. The smart chain engine returns this output JSON object and execution completes.

    Agents and Bots

    Since the smart chain system is really just a general purpose text processing and prompt chaining engine, you might reasonably ask, how exactly can you build a bot or an agent with this system?

    The smart chain system constructs agents by executing several special step types which allow one to choose tools for an agent to perform and maintain a conversation history. The most basic construction for an Agent within the Prospera system has the following form:

    1. A Conversational Tool Selection step, often called select_action, which runs an LLM prompt and has the LLM decide which action for the agent to perform

    2. A Conversational Agent Action step, often called execute_action, which calls the appropriate agent module with the parameters selected by the LLM

    3. A Record Agent Action step, often called record_action, which records the action executed in the second step into the bots conversation history.

    Agents we construct can get more complex then this, but this is the most basic construction. When you record an event to the conversation history, that becomes the events that you see on this screen:

    Another key distinction with smart-chains that are intended to be used with Agents and Bots versus other types of smart chains is the fact that the user can trigger them. Our system has various communication channels through which the bot can communicate. One which you can access directly is the Web Chat:

    When the user sends a message through this web interface, a new event gets added into the Conversation History. This happens completely outside of the smart chain system and is a function of how that particular communication channel is implemented.

    The communication channel will then trigger the Agent Kernel. The agent kernel will then attempt to figure out which smart chain should get activated for the bot. It will first check the Conversation object to see if there is any specific smart chain binding set. This can be seen under then Agent Name section of the conversation interface:

    If there is a specific smart chain binding set for this conversation, then that smart chain binding is invoked. If the Conversation is marked as default, then we will look up the default smart chain binding for that tenancy. That is set here within the Tenant configuration by the Prospera Labs team.

    Depending on which environment / tenant you are on, a different smart chain binding may be set as the default binding.

    Main Loop

    Once the agent kernel has executed the smart chain, the system is allowed to run a second time if there is a value for should_execute_another_action returned by the smart chain and it is set to True. There is a hard coded limit of a maximum of three smart chain invocations in when the kernel is triggered. This is to prevent the agent from going out of control and repeating forever if there is an error.

    By default, any time the agent communicates with the user, such as by sending a message, email, or speaking, should_execute_another_action is set to False. When the agent performs some sort of action, such as doing a knowledge base lookup or checking the users schedule for a booking, should_execute_another_action is set to True.

    This gives the agent an opportunity to follow up on the result from the action it took by telling the user what it did and what happened.

    User Interface

    Access to the smart chain system is found underneath the “Advanced” sub-menu within the “Configuration” menu:

    There are two different menus to access the smart chain system. One is the Smart Chains themselves, and the other are Smart Chain Bindings.

    In both cases, when you click the menu link, you will be brought to a table view that allows you to search through the data.

    If you are looking at a brand new account, all you will see are the built-in smart chains. These built-in chains can be distinguished because they have no data in the “user_id” column. By contrast, any smart chains that you have created yourself or you have modified will show some value in the “user_id” column.

    The built-in smart chains are being continuously updated and improved by our team. However, you are still free to modify them. If you do modify them, then you will no longer receive any of the changes that our team puts in. Often modifying a smart chain is the only way to get a specific or custom behavior you are looking for.

    Editor

    If you click into a specific smart chain, you will be brought to the smart chain editor, which as of right now looks like this:

    Sometimes it can be convenient to shrink the left side menu so that you have more space for the editor:

    Tool-bar

    Step Types

    Prompt Chaining

    In many situations, it is necessary to chain together multiple different AI prompts, feeding the output of one as the input of another. This is trivial to accomplish within the Smart Chain system.

    To understand how this works, we must first observe that each smart chain step has a name. The names can be observed when you open the list of smart chain steps:

    When a smart chain step produces an output, that output is duplicated in two places within the final output object. First it is saved directly into the output object, and second, it is saved in a sub-object with the given step name. This can be observed when you examine the output data schema for a smart chain step.

    You will notice that the second sub object has a key that is the same as the name of the step. This gives you two options to reference data from prior steps. You can reference either the variable directly, or you can reference it through the individual step name. Those two variations are seen below in the record_action step.

    You can reference a variable directly, as seen here:

    Or you can use ‘dot’ notation in order to reference the sub variable within a particular step, as seen here:

    Which option you choose depends entirely on you. But its important to remember that if multiple different steps have outputs with the same field name, the later step values will overwrite the earlier step values. This usually isn’t a problem, but can be important to understand when using common variable names like text

    Test Samples & Replay

    Within the smart chain editor, you will find a tab called Test Samples. This allows you to view recent executions of the smart chain step. If we take for granted that the smart chain system is, at its essence, a system that takes a JSON as input and produces JSON as output:

    Those JSON objects manifest very literally within the user-interface under the Test Samples tab:

    In fact, the JSON’ish nature of the data can be even more clearly seen by pressing the button in the top-right corner of the widget:

    By default, the Test Samples view shows you the most recent executions of this smart chain step. You can also elect to get a random selection of executions. That option is more useful when doing one-off prompts and prompt-chains that don’t involve conversations.

    Replay

    The most useful part of the Test Samples interface is the ‘replay’ functionality. That is the button you see in the center here:

    When you press this button, the Smart Chain Step will re-execute using the latest options and latest prompt, but the same input-data as what you see on the interface. This can be very useful if you are trying to change the behaviour of the agent. You can update the prompt and then see if your new prompt has changed the behaviour in the way you expect, knowing that it was run with all of the exact same data, and the only thing that changed was your prompt.

    When you rerun a smart chain step, any side effects that run as a consequence of that action will occur a second time. For example, if the smart chain step involves sending a message to the user, rerunning that step will result in additional messages being sent to the user. Generally it is only reccomended to rerun smart chain steps that use an LLM or perform a computation. There are few reasons to rerun the Conversational Agent Action or Record Agent Action steps.

    Recursion - Calling One Smart Chain From Within Another

    Smart chains can execute other smart chains as part of evaluating either the Content Template or the Options Template.

    All smart chain bindings are loaded into the smart chain system as functions. So if you have a binding that is named “formatted_business_info”, as seen here:

    You can then invoke that smart chain by calling it as a function within your template. This can be seen in action within the built-in Receptionist smart chains.

    When you invoke a different smart chain, it will receive the same prompt context as the smart chain that did the invocation. Additionally, any side-effects that result from that other smart chain will also occur when that smart chain is invoked. This allows you to, if you put in some effort, to do primitive functions and conditional executions of smart chain sections.

    Bindings and Published Versions

    Smart chain bindings are used to reference smart chains. There is no way to access a smart chain EXCEPT through its binding. Any time that there are sections of our code that need to reference or execute a smart chain, it is done through a binding. Any time that smart chains reference each other, it is done through bindings.

    Why have the bindings? The bindings provide us a mechanism to easily flip between different versions of a particular smart chain. For example, you may want to prepare a new version of a smart chain that is a significant change from your current version. Through the binding, you can keep the system pointed towards the current smart chain, but then flip it over to the new smart chain whenever it is ready.

    In the future, we may add additional features to the binding system, such as an ability to directly A/B test different versions of a smart chain and measure the difference in metrics that result. Or we may be able to provide fallback smart-chains that are automatically invoked if there is a failure in a main version.

    Additionally, smart chains themselves have a system that allow you to publish versions. That can be accessed through this button:

    When you publish a smart chain for the first time, that published version will then take precedence over whatever is currently being edited. You will see your newly published version show up on this menu:

    This also allows you to revert whatever version is currently in your editor to a previous published and verified version.

    The binding system is defined by two variables: the binding_name and the chain_name. The binding_name is what you see in this column in the binding view:

    The binding system really breaks down to just a way to define key value pairs, where the key is the binding_name and the value is the chain_name.

    The chain_name refers to the name of the smart chain, which can be edited HERE within the UI:

    The chain_name and binding_name are used to disambiguate smart chains and bindings within the database. Some smart chains and some bindings are built-in. If you do not have a smart chain or a binding with that chain_name or binding_name, then the default smart chain or default binding will be automatically inferred by the system.

    Resolving the Binding

    When the system attempts to resolve a given binding_name into a final smart chain, the following process is executed:

    1. Attempt to find any smart-chain binding object with the given binding_name and user_id from the users account

    2. If none found, attempt find a built in smart chain binding object with the given binding_name and user_id of null

    3. If no binding object is found, return an error

    4. Take the chain_name from the smart chain binding object

    5. Attempt to the latest published smart chain version with the chain_name taken from the binding and the user_id from the users account

    6. If no published version is found, we attempt to find any smart chain with the chain_name taken from the binding and the user_id from the users account

    7. If no user specific smart chain was found at all, we will attempt to find a built-in smart chain with the chain_name specified in the binding and user_id of null

    8. If smart chain was found at all, return an error

     

    Or to understand this in a simpler way, smart chains are resolved from bindings in the following priority:

    1. Published Smart Chains with matching chain_name local to the user

    2. Any smart chain at all with matching chain_name local to the user

    3. Default built-in smart chains with matching chain_name and user_id of null

    Template Engine

    The template engine allows users to write complex templates that may involve conditions, loops, variable references, and even simple calculations and function calls.

    Variables

    Variable substitutions use the double curly brace syntax:

    {{variable_name}}

    If you want to reference a variable within a variable, you can use either dot notation or square bracket notation:

    {{sub_object.variable_name}} {{sub_object['variable_name']}}

    The variables you reference here must be part of the prompts input. You can see what input variables are available by going here:

    Filters

    Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (|) and may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.

    For example, {{ name|striptags|title }} will remove all HTML Tags from variable name and title-case the output (title(striptags(name))).

    Filters that accept arguments have parentheses around the arguments, just like a function call. For example: {{ listx|join(', ') }} will join a list with commas (str.join(', ', listx)).

    The List of Builtin Filters below describes all the builtin filters.

    Tests

    Beside filters, there are also so-called “tests” available. Tests can be used to test a variable against a common expression. To test a variable or expression, you add is plus the name of the test after the variable. For example, to find out if a variable is defined, you can do name is defined, which will then return true or false depending on whether name is defined in the current template context.

    Tests can accept arguments, too. If the test only takes one argument, you can leave out the parentheses. For example, the following two expressions do the same thing:

    {% if loop.index is divisibleby 3 %} {% if loop.index is divisibleby(3) %}

    The List of Builtin Tests below describes all the builtin tests.

    Comments

    To comment-out part of a line in a template, use the comment syntax which is by default set to {# ... #}. This is useful to comment out parts of the template for debugging or to add information for other template designers or yourself:

    Whitespace Control

    In the default configuration:

    • a single trailing newline is stripped if present

    • other whitespace (spaces, tabs, newlines etc.) is returned unchanged

    If an application configures the template engine to trim_blocks, the first newline after a template tag is removed automatically (like in PHP). The lstrip_blocks option can also be set to strip tabs and spaces from the beginning of a line to the start of a block. (Nothing will be stripped if there are other characters before the start of the block.)

    With both trim_blocks and lstrip_blocks disabled (the default), block tags on their own lines will be removed, but a blank line will remain and the spaces in the content will be preserved. For example, this template:

    With both trim_blocks and lstrip_blocks disabled, the template is rendered with blank lines inside the div:

    With both trim_blocks and lstrip_blocks enabled, the template block lines are completely removed:

    You can manually disable the lstrip_blocks behavior by putting a plus sign (+) at the start of a block:

    Similarly, you can manually disable the trim_blocks behavior by putting a plus sign (+) at the end of a block:

    You can also strip whitespace in templates by hand. If you add a minus sign (-) to the start or end of a block (e.g. a For tag), a comment, or a variable expression, the whitespaces before or after that block will be removed:

    This will yield all elements without whitespace between them. If seq was a list of numbers from 1 to 9, the output would be 123456789.

    If Line Statements are enabled, they strip leading whitespace automatically up to the beginning of the line.

    By default, the template engine also removes trailing newlines. To keep single trailing newlines, configure the template engine to keep_trailing_newline.

    Note

    You must not add whitespace between the tag and the minus sign.

    valid:

    invalid:

    Escaping

    It is sometimes desirable – even necessary – to have the template engine ignore parts it would otherwise handle as variables or blocks. For example, if, with the default syntax, you want to use {{ as a raw string in a template and not start a variable, you have to use a trick.

    The easiest way to output a literal variable delimiter ({{) is by using a variable expression:

    For bigger sections, it makes sense to mark a block raw. For example, to include example the template engine syntax in a template, you can use this snippet:

    Note

    Minus sign at the end of {% raw -%} tag cleans all the spaces and newlines preceding the first character of your raw data.

    Line Statements

    If line statements are enabled by the application, it’s possible to mark a line as a statement. For example, if the line statement prefix is configured to #, the following two examples are equivalent:

    The line statement prefix can appear anywhere on the line as long as no text precedes it. For better readability, statements that start a block (such as for, if, elif etc.) may end with a colon:

    Note

    Line statements can span multiple lines if there are open parentheses, braces or brackets:

    Line-based comments are available as well. For example, if the line-comment prefix is configured to be ##, everything from ## to the end of the line is ignored (excluding the newline sign):

    List of Control Structures

    A control structure refers to all those things that control the flow of a program - conditionals (i.e. if/elif/else), for-loops, as well as things like macros and blocks. With the default syntax, control structures appear inside {% ... %} blocks.

    For

    Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:

    As variables in templates retain their object properties, it is possible to iterate over containers like dict:

    Python dicts may not be in the order you want to display them in. If order matters, use the |dictsort filter.

    Inside of a for-loop block, you can access some special variables:

    Variable

    Description

    Variable

    Description

    loop.index

    The current iteration of the loop. (1 indexed)

    loop.index0

    The current iteration of the loop. (0 indexed)

    loop.revindex

    The number of iterations from the end of the loop (1 indexed)

    loop.revindex0

    The number of iterations from the end of the loop (0 indexed)

    loop.first

    True if first iteration.

    loop.last

    True if last iteration.

    loop.length

    The number of items in the sequence.

    loop.cycle

    A helper function to cycle between a list of sequences. See the explanation below.

    loop.depth

    Indicates how deep in a recursive loop the rendering currently is. Starts at level 1

    loop.depth0

    Indicates how deep in a recursive loop the rendering currently is. Starts at level 0

    loop.previtem

    The item from the previous iteration of the loop. Undefined during the first iteration.

    loop.nextitem

    The item from the following iteration of the loop. Undefined during the last iteration.

    loop.changed(*val)

    True if previously called with a different value (or not called at all).

    Within a for-loop, it’s possible to cycle among a list of strings/variables each time through the loop by using the special loop.cycle helper:

    An extra cycle helper exists that allows loop-unbound cycling. For more information, have a look at the List of Global Functions.

    Unlike in Python, it’s not possible to break or continue in a loop. You can, however, filter the sequence during iteration, which allows you to skip items. The following example skips all the users which are hidden:

    The advantage is that the special loop variable will count correctly; thus not counting the users not iterated over.

    If no iteration took place because the sequence was empty or the filtering removed all the items from the sequence, you can render a default block by using else:

    Note that, in Python, else blocks are executed whenever the corresponding loop did not break. Since the template engine loops cannot break anyway, a slightly different behavior of the else keyword was chosen.

    It is also possible to use loops recursively. This is useful if you are dealing with recursive data such as sitemaps or RDFa. To use loops recursively, you basically have to add the recursive modifier to the loop definition and call the loop variable with the new iterable where you want to recurse.

    The following example implements a sitemap with recursive loops:

    The loop variable always refers to the closest (innermost) loop. If we have more than one level of loops, we can rebind the variable loop by writing {% set outer_loop = loop %} after the loop that we want to use recursively. Then, we can call it using {{ outer_loop(...) }}

    Please note that assignments in loops will be cleared at the end of the iteration and cannot outlive the loop scope. Older versions of the template engine had a bug where in some circumstances it appeared that assignments would work. This is not supported. See Assignments for more information about how to deal with this.

    If all you want to do is check whether some value has changed since the last iteration or will change in the next iteration, you can use previtem and nextitem:

    If you only care whether the value changed at all, using changed is even easier:

    If

    The if statement in the template engine is comparable with the Python if statement. In the simplest form, you can use it to test if a variable is defined, not empty and not false:

    For multiple branches, elif and else can be used like in Python. You can use more complex Expressions there, too:

    If can also be used as an inline expression and for loop filtering.

    Macros

    Macros are comparable with functions in regular programming languages. They are useful to put often used idioms into reusable functions to not repeat yourself (“DRY”).

    Here’s a small example of a macro that renders a form element:

    The macro can then be called like a function in the namespace:

    If the macro was defined in a different template, you have to import it first.

    Inside macros, you have access to three special variables:

    varargs

    If more positional arguments are passed to the macro than accepted by the macro, they end up in the special varargs variable as a list of values.

    kwargs

    Like varargs but for keyword arguments. All unconsumed keyword arguments are stored in this special variable.

    caller

    If the macro was called from a call tag, the caller is stored in this variable as a callable macro.

    Macros also expose some of their internal details. The following attributes are available on a macro object:

    name

    The name of the macro. {{ input.name }} will print input.

    arguments

    A tuple of the names of arguments the macro accepts.

    catch_kwargs

    This is true if the macro accepts extra keyword arguments (i.e.: accesses the special kwargs variable).

    catch_varargs

    This is true if the macro accepts extra positional arguments (i.e.: accesses the special varargs variable).

    caller

    This is true if the macro accesses the special caller variable and may be called from a call tag.

    If a macro name starts with an underscore, it’s not exported and can’t be imported.

    Due to how scopes work in the template engine, a macro in a child template does not override a macro in a parent template. The following will output “LAYOUT”, not “CHILD”.

    layout.txt

    child.txt

    Call

    In some cases it can be useful to pass a macro to another macro. For this purpose, you can use the special call block. The following example shows a macro that takes advantage of the call functionality and how it can be used:

    It’s also possible to pass arguments back to the call block. This makes it useful as a replacement for loops. Generally speaking, a call block works exactly like a macro without a name.

    Here’s an example of how a call block can be used with arguments:

    Filters

    Filter sections allow you to apply regular the template engine filters on a block of template data. Just wrap the code in the special filter section:

    Filters that accept arguments can be called like this:

    Assignments

    Inside code blocks, you can also assign values to variables. Assignments at top level (outside of blocks, macros or loops) are exported from the template like top level macros and can be imported by other templates.

    Assignments use the set tag and can have multiple targets:

    Scoping Behavior

    Please keep in mind that it is not possible to set variables inside a block and have them show up outside of it. This also applies to loops. The only exception to that rule are if statements which do not introduce a scope. As a result the following template is not going to do what you might expect:

    It is not possible with the template engine syntax to do this. Instead use alternative constructs like the loop else block or the special loop variable:

    As of version 2.10 more complex use cases can be handled using namespace objects which allow propagating of changes across scopes:

    Note that the obj.attr notation in the set tag is only allowed for namespace objects; attempting to assign an attribute on any other object will raise an exception.

    Block Assignments

    It’s possible to use set as a block to assign the content of the block to a variable. This can be used to create multi-line strings, since the template engine doesn’t support Python’s triple quotes (""", ''').

    Instead of using an equals sign and a value, you only write the variable name, and everything until {% endset %} is captured.

    Filters applied to the variable name will be applied to the block’s content.

    Expressions

    The template engine allows basic expressions everywhere. These work very similarly to regular Python; even if you’re not working with Python you should feel comfortable with it.

    Literals

    The simplest form of expressions are literals. Literals are representations for Python objects such as strings and numbers. The following literals exist:

    "Hello World"

    Everything between two double or single quotes is a string. They are useful whenever you need a string in the template (e.g. as arguments to function calls and filters, or just to extend or include a template).

    42 / 123_456

    Integers are whole numbers without a decimal part. The ‘_’ character can be used to separate groups for legibility.

    42.23 / 42.1e2 / 123_456.789

    Floating point numbers can be written using a ‘.’ as a decimal mark. They can also be written in scientific notation with an upper or lower case ‘e’ to indicate the exponent part. The ‘_’ character can be used to separate groups for legibility, but cannot be used in the exponent part.

    ['list', 'of', 'objects']

    Everything between two brackets is a list. Lists are useful for storing sequential data to be iterated over. For example, you can easily create a list of links using lists and tuples for (and with) a for loop:

    ('tuple', 'of', 'values')

    Tuples are like lists that cannot be modified (“immutable”). If a tuple only has one item, it must be followed by a comma (('1-tuple',)). Tuples are usually used to represent items of two or more elements. See the list example above for more details.

    {'dict': 'of', 'key': 'and', 'value': 'pairs'}

    A dict in Python is a structure that combines keys and values. Keys must be unique and always have exactly one value. Dicts are rarely used in templates; they are useful in some rare cases such as the xmlattr() filter.

    true / false

    true is always true and false is always false.

    Note

    The special constants true, false, and none are indeed lowercase. Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false), all three can now also be written in title case (True, False, and None). However, for consistency, (all identifiers are lowercase) you should use the lowercase versions.

    Math

    The template engine allows you to calculate with values. This is rarely useful in templates but exists for completeness’ sake. The following operators are supported:

    +

    Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator. {{ 1 + 1 }} is 2.

    -

    Subtract the second number from the first one. {{ 3 - 2 }} is 1.

    /

    Divide two numbers. The return value will be a floating point number. {{ 1 / 2 }} is {{ 0.5 }}.

    //

    Divide two numbers and return the truncated integer result. {{ 20 // 7 }} is 2.

    %

    Calculate the remainder of an integer division. {{ 11 % 7 }} is 4.

    *

    Multiply the left operand with the right one. {{ 2 * 2 }} would return 4. This can also be used to repeat a string multiple times. {{ '=' * 80 }} would print a bar of 80 equal signs.

    **

    Raise the left operand to the power of the right operand. {{ 2**3 }} would return 8.

    Unlike Python, chained pow is evaluated left to right. {{ 3**3**3 }} is evaluated as (3**3)**3 in the template engine, but would be evaluated as 3**(3**3) in Python. Use parentheses in the template engine to be explicit about what order you want. It is usually preferable to do extended math in Python and pass the results to render rather than doing it in the template.

    This behavior may be changed in the future to match Python, if it’s possible to introduce an upgrade path.

    Comparisons

    ==

    Compares two objects for equality.

    !=

    Compares two objects for inequality.

    >

    true if the left hand side is greater than the right hand side.

    >=

    true if the left hand side is greater or equal to the right hand side.

    <

    true if the left hand side is lower than the right hand side.

    <=

    true if the left hand side is lower or equal to the right hand side.

    Logic

    For if statements, for filtering, and if expressions, it can be useful to combine multiple expressions.

    and

    For x and y, if x is false, then the value is x, else y. In a boolean context, this will be treated as True if both operands are truthy.

    or

    For x or y, if x is true, then the value is x, else y. In a boolean context, this will be treated as True if at least one operand is truthy.

    not

    For not x, if x is false, then the value is True, else False.

    Prefer negating is and in using their infix notation: foo is not bar instead of not foo is bar; foo not in bar instead of not foo in bar. All other expressions require prefix notation: not (foo and bar).

    (expr)

    Parentheses group an expression. This is used to change evaluation order, or to make a long expression easier to read or less ambiguous.

    Other Operators

    The following operators are very useful but don’t fit into any of the other two categories:

    in

    Perform a sequence / mapping containment test. Returns true if the left operand is contained in the right. {{ 1 in [1, 2, 3] }} would, for example, return true.

    is

    Performs a test.

    | (pipe, vertical bar)

    Applies a filter.

    ~ (tilde)

    Converts all operands into strings and concatenates them.

    {{ "Hello " ~ name ~ "!" }} would return (assuming name is set to 'John') Hello John!.

    ()

    Call a callable: {{ post.render() }}. Inside of the parentheses you can use positional arguments and keyword arguments like in Python:

    {{ post.render(user, full=true) }}.

    . / []

    Get an attribute of an object. (See Variables)

    If Expression

    It is also possible to use inline if expressions. These are useful in some situations. For example, you can use this to extend from one template if a variable is defined, otherwise from the default layout template:

    The general syntax is <do something> if <something is true> else <do something else>.

    The else part is optional. If not provided, the else block implicitly evaluates into an Undefined object (regardless of what undefined in the environment is set to):

    Python Methods

    You can also use any of the methods defined on a variable’s type. The value returned from the method invocation is used as the value of the expression. Here is an example that uses methods defined on strings (where page.title is a string):

    This works for methods on user-defined types. For example, if variable f of type Foo has a method bar defined on it, you can do the following:

    Operator methods also work as expected. For example, % implements printf-style for strings:

    Although you should prefer the .format method for that case (which is a bit contrived in the context of rendering a template):

    List of Builtin Filters

    abs()

    forceescape()

    map()

    select()

    unique()

    attr()

    format()

    max()

    selectattr()

    upper()

    batch()

    groupby()

    min()

    slice()

    urlencode()

    capitalize()

    indent()

    pprint()

    sort()

    urlize()

    center()

    int()

    random()

    string()

    wordcount()

    default()

    items()

    reject()

    striptags()

    wordwrap()

    dictsort()

    join()

    rejectattr()

    sum()

    xmlattr()

    escape()

    last()

    replace()

    title()

     

    filesizeformat()

    length()

    reverse()

    tojson()

     

    first()

    list()

    round()

    trim()

     

    float()

    lower()

    safe()

    truncate()

     

    abs(x, /)

    Return the absolute value of the argument.

    attr(obj, name)

    Get an attribute of an object. foo|attr("bar") works like foo.bar just that always an attribute is returned and items are not looked up.

    batch(value, linecount, fill_with = None)

    A filter that batches items. It works pretty much like slice just the other way round. It returns a list of lists with the given number of items. If you provide a second parameter this is used to fill up missing items. See this example:

    capitalize(s)

    Capitalize a value. The first character will be uppercase, all others lowercase.

    center(value, width = 80)

    Centers the value in a field of a given width.

    default(value, default_value = '', boolean = False)

    If the value is undefined it will return the passed default value, otherwise the value of the variable:

    This will output the value of my_variable if the variable was defined, otherwise 'my_variable is not defined'. If you want to use default with variables that evaluate to false you have to set the second parameter to true:

    dictsort(value, case_sensitive = False, by = 'key', reverse = False)

    Sort a dict and yield (key, value) pairs. Python dicts may not be in the order you want to display them in, so sort them first.

    escape(s)

    Replace the characters &, <, >, ', and " in the string with HTML-safe sequences. Use this if you need to display text that might contain such characters in HTML.

    If the object has an __html__ method, it is called and the return value is assumed to already be safe for HTML.

    Parameters:

    s – An object to be converted to a string and escaped.

    Returns:

    A Markup string with the escaped text.

    Aliases:

    e

    filesizeformat(value, binary = False)

    Format the value like a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega, Giga, etc.), if the second parameter is set to True the binary prefixes are used (Mebi, Gibi).

    first(seq)

    Return the first item of a sequence.

    float(value, default = 0.0)

    Convert the value into a floating point number. If the conversion doesn’t work it will return 0.0. You can override this default using the first parameter.

    forceescape(value)

    Enforce HTML escaping. This will probably double escape variables.

    format(value, *args, **kwargs)

    Apply the given values to a printf-style format string, like string % values.

    In most cases it should be more convenient and efficient to use the % operator or str.format().

    groupby(value, attribute, default = None, case_sensitive = False)

    Group a sequence of objects by an attribute using Python’s itertools.groupby(). The attribute can use dot notation for nested access, like "address.city". Unlike Python’s groupby, the values are sorted first so only one group is returned for each unique value.

    For example, a list of User objects with a city attribute can be rendered in groups. In this example, grouper refers to the city value of the group.

    groupby yields namedtuples of (grouper, list), which can be used instead of the tuple unpacking above. grouper is the value of the attribute, and list is the items with that value.

    You can specify a default value to use if an object in the list does not have the given attribute.

    Like the sort() filter, sorting and grouping is case-insensitive by default. The key for each group will have the case of the first item in that group of values. For example, if a list of users has cities ["CA", "NY", "ca"], the “CA” group will have two values. This can be disabled by passing case_sensitive=True.

    Changed in version 3.1: Added the case_sensitive parameter. Sorting and grouping is case-insensitive by default, matching other filters that do comparisons.

    indent(s, width = 4, first = False, blank = False)

    Return a copy of the string with each line indented by 4 spaces. The first line and blank lines are not indented by default.

    Parameters:

    • width – Number of spaces, or a string, to indent by.

    • first – Don’t skip indenting the first line.

    • blank – Don’t skip indenting empty lines.

    int(value, default = 0, base = 10)

    Convert the value into an integer. If the conversion doesn’t work it will return 0. You can override this default using the first parameter. You can also override the default base (10) in the second parameter, which handles input with prefixes such as 0b, 0o and 0x for bases 2, 8 and 16 respectively. The base is ignored for decimal numbers and non-string values.

    items(value)

    Return an iterator over the (key, value) items of a mapping.

    x|items is the same as x.items(), except if x is undefined an empty iterator is returned.

    This filter is useful if you expect the template to be rendered with an implementation of the template engine in another programming language that does not have a .items() method on its mapping type.

    join(value, d = '', attribute = None)

    Return a string which is the concatenation of the strings in the sequence. The separator between elements is an empty string per default, you can define it with the optional parameter:

    It is also possible to join certain attributes of an object:

    last(seq)

    Return the last item of a sequence.

    Note: Does not work with generators. You may want to explicitly convert it to a list:

    length(obj)

    Return the number of items in a container.

    Aliases:

    count

    list(value)

    Convert the value into a list. If it was a string the returned list will be a list of characters.

    lower(s)

    Convert a value to lowercase.

    map(value, *args, **kwargs)

    Applies a filter on a sequence of objects or looks up an attribute. This is useful when dealing with lists of objects but you are really only interested in a certain value of it.

    The basic usage is mapping on an attribute. Imagine you have a list of users but you are only interested in a list of usernames:

    You can specify a default value to use if an object in the list does not have the given attribute.

    Alternatively you can let it invoke a filter by passing the name of the filter and the arguments afterwards. A good example would be applying a text conversion filter on a sequence:

    Similar to a generator comprehension such as:

    max(value, case_sensitive = False, attribute = None)

    Return the largest item from the sequence.

    Parameters:

    • case_sensitive – Treat upper and lower case strings as distinct.

    • attribute – Get the object with the max value of this attribute.

    min(value, case_sensitive = False, attribute = None)

    Return the smallest item from the sequence.

    Parameters:

    • case_sensitive – Treat upper and lower case strings as distinct.

    • attribute – Get the object with the min value of this attribute.

    pprint(value)

    Pretty print a variable. Useful for debugging.

    random(seq)

    Return a random item from the sequence.

    reject(value, *args, **kwargs)

    Filters a sequence of objects by applying a test to each object, and rejecting the objects with the test succeeding.

    If no test is specified, each object will be evaluated as a boolean.

    Example usage:

    Similar to a generator comprehension such as:

    rejectattr(value, *args, **kwargs)

    Filters a sequence of objects by applying a test to the specified attribute of each object, and rejecting the objects with the test succeeding.

    If no test is specified, the attribute’s value will be evaluated as a boolean.

    Similar to a generator comprehension such as:

    replace(s, old, new, count= None)

    Return a copy of the value with all occurrences of a substring replaced with a new one. The first argument is the substring that should be replaced, the second is the replacement string. If the optional third argument count is given, only the first count occurrences are replaced:

    reverse(value)

    Reverse the object or return an iterator that iterates over it the other way round.

    round(value, precision = 0, method = 'common')

    Round the number to a given precision. The first parameter specifies the precision (default is 0), the second the rounding method:

    • 'common' rounds either up or down

    • 'ceil' always rounds up

    • 'floor' always rounds down

    If you don’t specify a method 'common' is used.

    Note that even if rounded to 0 precision, a float is returned. If you need a real integer, pipe it through int:

    safe(value)

    Mark the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped.

    select(value, *args, **kwargs)

    Filters a sequence of objects by applying a test to each object, and only selecting the objects with the test succeeding.

    If no test is specified, each object will be evaluated as a boolean.

    Example usage:

    Similar to a generator comprehension such as:

    selectattr(value, *args, **kwargs)

    Filters a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding.

    If no test is specified, the attribute’s value will be evaluated as a boolean.

    Example usage:

    Similar to a generator comprehension such as:

    slice(value, slices:, fill_with = None)

    Slice an iterator and return a list of lists containing those items. Useful if you want to create a div containing three ul tags that represent columns:

    If you pass it a second argument it’s used to fill missing values on the last iteration.

    sort(value, reverse = False, case_sensitive = False, attribute = None)

    Sort an iterable using Python’s sorted().

    Parameters:

    • reverse – Sort descending instead of ascending.

    • case_sensitive – When sorting strings, sort upper and lower case separately.

    • attribute – When sorting objects or dicts, an attribute or key to sort by. Can use dot notation like "address.city". Can be a list of attributes like "age,name".

    The sort is stable, it does not change the relative order of elements that compare equal. This makes it is possible to chain sorts on different attributes and ordering.

    As a shortcut to chaining when the direction is the same for all attributes, pass a comma separate list of attributes.

    string(s)

    Convert an object to a string if it isn’t already. This preserves a Markup string rather than converting it back to a basic string, so it will still be marked as safe and won’t be escaped again.

    striptags(value)

    Strip SGML/XML tags and replace adjacent whitespace by one space.

    sum(iterable, attribute = None, start = 0)

    Returns the sum of a sequence of numbers plus the value of parameter ‘start’ (which defaults to 0). When the sequence is empty it returns start.

    It is also possible to sum up only certain attributes:

    title(s)

    Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.

    tojson(value, indent = None)

    Serialize an object to a string of JSON, and mark it safe to render in HTML. This filter is only for use in HTML documents.

    The returned string is safe to render in HTML documents and <script> tags. The exception is in HTML attributes that are double quoted; either use single quotes or the |forceescape filter.

    Parameters:

    • value – The object to serialize to JSON.

    • indent – The indent parameter passed to dumps, for pretty-printing the value.

    trim(value, chars = None)

    Strip leading and trailing characters, by default whitespace.

    truncate(s, length = 255, killwords = False, end = '...', leeway = None)

    Return a truncated copy of the string. The length is specified with the first parameter which defaults to 255. If the second parameter is true the filter will cut the text at length. Otherwise it will discard the last word. If the text was in fact truncated it will append an ellipsis sign ("..."). If you want a different ellipsis sign than "..." you can specify it using the third parameter. Strings that only exceed the length by the tolerance margin given in the fourth parameter will not be truncated.

    The default leeway is 0 before but can be reconfigured globally.

    unique(value, case_sensitive = False, attribute = None)

    Returns a list of unique items from the given iterable.

    The unique items are yielded in the same order as their first occurrence in the iterable passed to the filter.

    Parameters:

    • case_sensitive – Treat upper and lower case strings as distinct.

    • attribute – Filter objects with unique values for this attribute.

    upper(s)

    Convert a value to uppercase.

    urlencode(value)

    Quote data for use in a URL path or query using UTF-8.

    Basic wrapper around urllib.parse.quote() when given a string, or urllib.parse.urlencode() for a dict or iterable.

    Parameters:

    value – Data to quote. A string will be quoted directly. A dict or iterable of (key, value) pairs will be joined as a query string.

    When given a string, “/” is not quoted. HTTP servers treat “/” and “%2F” equivalently in paths. If you need quoted slashes, use the |replace("/", "%2F") filter.

    urlize(value, trim_url_limit = None, nofollow = False, target = None, rel = None, extra_schemes = None)

    Convert URLs in text into clickable links.

    This may not recognize links in some situations. Usually, a more comprehensive formatter, such as a Markdown library, is a better choice.

    Works on http://, https://, www., mailto:, and email addresses. Links with trailing punctuation (periods, commas, closing parentheses) and leading punctuation (opening parentheses) are recognized excluding the punctuation. Email addresses that include header fields are not recognized (for example, mailto:address@example.com?cc=copy@example.com).

    Parameters:

    • value – Original text containing URLs to link.

    • trim_url_limit – Shorten displayed URL values to this length.

    • nofollow – Add the rel=nofollow attribute to links.

    • target – Add the target attribute to links.

    • rel – Add the rel attribute to links.

    • extra_schemes – Recognize URLs that start with these schemes in addition to the default behavior. Defaults to env.policies["urlize.extra_schemes"], which defaults to no extra schemes.

    wordcount(s)

    Count the words in that string.

    wordwrap(s, width, break_long_words = True, wrapstring = None, break_on_hyphens = True)

    Wrap a string to the given width. Existing newlines are treated as paragraphs to be wrapped separately.

    Parameters:

    • s – Original text to wrap.

    • width – Maximum length of wrapped lines.

    • break_long_words – If a word is longer than width, break it across lines.

    • break_on_hyphens – If a word contains hyphens, it may be split across lines.

    • wrapstring – String to join each wrapped line. Defaults to Environment.newline_sequence.

    xmlattr(d, autospace = True)

    Create an SGML/XML attribute string based on the items in a dict.

    Values that are neither none nor undefined are automatically escaped, safely allowing untrusted user input.

    User input should not be used as keys to this filter. If any key contains a space, / solidus, > greater-than sign, or = equals sign, this fails with a ValueError. Regardless of this, user input should never be used as keys to this filter, or must be separately validated first.

    Results in something like this:

    As you can see it automatically prepends a space in front of the item if the filter returned something unless the second parameter is false.

    Changed in version 3.1.4: Keys with / solidus, > greater-than sign, or = equals sign are not allowed.

    Changed in version 3.1.3: Keys with spaces are not allowed.

    List of Builtin Tests

    boolean()

    even()

    in()

    mapping()

    sequence()

    callable()

    false()

    integer()

    ne()

    string()

    defined()

    filter()

    iterable()

    none()

    test()

    divisibleby()

    float()

    le()

    number()

    true()

    eq()

    ge()

    lower()

    odd()

    undefined()

    escaped()

    gt()

    lt()

    sameas()

    upper()

    boolean(value)

    Return true if the object is a boolean value.

    callable(obj)

    Return whether the object is callable (i.e., some kind of function).

    Note that classes are callable, as are instances of classes with a __call__() method.

    defined(value)

    Return true if the variable is defined:

    See the default() filter for a simple way to set undefined variables.

    divisibleby(value, num)

    Check if a variable is divisible by a number.

    eq(a, b)

    Same as a == b.

    Aliases:

    ==, equalto

    escaped(value)

    Check if the value is escaped.

    even(value)

    Return true if the variable is even.

    false(value)

    Return true if the object is False.

    filter(value)

    Check if a filter exists by name. Useful if a filter may be optionally available.

    float(value)

    Return true if the object is a float.

    ge(a, b )

    Same as a >= b.

    Aliases:

    >=

    gt(a, b )

    Same as a > b.

    Aliases:

    >, greaterthan

    in(value, seq)

    Check if value is in seq.

    integer(value)

    Return true if the object is an integer.

    iterable(value)

    Check if it’s possible to iterate over an object.

    le(a, b )

    Same as a <= b.

    Aliases:

    <=

    lower(value )

    Return true if the variable is lowercased.

    lt(a, b )

    Same as a < b.

    Aliases:

    <, lessthan

    mapping(value)

    Return true if the object is a mapping (dict etc.).

    ne(a, b )

    Same as a != b.

    Aliases:

    !=

    none(value)

    Return true if the variable is none.

    number(value)

    Return true if the variable is a number.

    odd(value)

    Return true if the variable is odd.

    sameas(value, other)

    Check if an object points to the same memory address than another object:

    sequence(value)

    Return true if the variable is a sequence. Sequences are variables that are iterable.

    string(value)

    Return true if the object is a string.

    test(value)

    Check if a test exists by name. Useful if a test may be optionally available.

    true(value)

    Return true if the object is True.

    undefined(value)

    Like defined() but the other way round.

    upper(value)

    Return true if the variable is uppercased.

    List of Global Functions

    The following functions are available in the global scope by default:

    range([start, ]stop[, step])

    Return a list containing an arithmetic progression of integers. range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0. When step is given, it specifies the increment (or decrement). For example, range(4) and range(0, 4, 1) return [0, 1, 2, 3]. The end point is omitted! These are exactly the valid indices for a list of 4 elements.

    This is useful to repeat a template block multiple times, e.g. to fill a list. Imagine you have 7 users in the list but you want to render three empty items to enforce a height with CSS:

    lipsum(n=5, html=True, min=20, max=100)

    Generates some lorem ipsum for the template. By default, five paragraphs of HTML are generated with each paragraph between 20 and 100 words. If html is False, regular text is returned. This is useful to generate simple contents for layout testing.

    dict(\**items)

    A convenient alternative to dict literals. {'foo': 'bar'} is the same as dict(foo='bar').

    class cycler(\*items)

    Cycle through values by yielding them one at a time, then restarting once the end is reached.

    Similar to loop.cycle, but can be used outside loops or across multiple loops. For example, render a list of folders and files in a list, alternating giving them “odd” and “even” classes.

    Parameters:

    items – Each positional argument will be yielded in the order given for each cycle.

    property current

    Return the current item. Equivalent to the item that will be returned next time next() is called.

    next()

    Return the current item, then advance current to the next item.

    reset()

    Resets the current item to the first item.

    class joiner(sep=', ')

    A tiny helper that can be used to “join” multiple sections. A joiner is passed a string and will return that string every time it’s called, except the first time (in which case it returns an empty string). You can use this to join things:

    class namespace(...)

    Creates a new container that allows attribute assignment using the {% set %} tag:

    The main purpose of this is to allow carrying a value from within a loop body to an outer scope. Initial values can be provided as a dict, as keyword arguments, or both (same behavior as Python’s dict constructor):