Skip to main content
Templates let you inject variables into prompts at runtime. Braintrust supports Mustache and Nunjucks templating:
  • Mustache (default): Simple variable substitution and basic logic.
  • Nunjucks: Advanced templating with loops, conditionals, and filters.

Mustache

Mustache is the default templating language. For the complete syntax, see the Mustache documentation.

Basic variable substitution

Use {{variable}} to insert values:

Nested properties

Access nested object properties with dot notation:

Sections and iteration

Use sections to iterate over arrays or conditionally show content:

Inverted sections

Use ^ to show content when a value is falsy or empty:

Comments

Use {{! comment }} for comments that won’t appear in output:

Preserve special characters

If you want to preserve double curly brackets {{ and }} as plain text when using Mustache, change the delimiter tags:

Strict mode

Mustache supports strict mode, which throws an error when required template variables are missing:

Nunjucks

For more complex templating needs, use Nunjucks, which implements Jinja2 syntax in JavaScript. For the full list of syntax and features, see the Nunjucks templating documentation.

Loops

Process arrays and iterate over data:
Loop variables provide useful metadata:
Available loop variables are loop.index (1-indexed), loop.index0 (0-indexed), loop.first, loop.last, and loop.length.

Conditionals

Add logic to your prompts:
Combine conditionals with loops:

Filters

Transform data with built-in filters:
Common filters:
  • upper, lower: Change case.
  • title, capitalize: Capitalize text.
  • join(separator): Join array elements.
  • length: Get array or string length.
  • default(value): Provide default value.
  • replace(old, new): Replace text.

String operations

Concatenate strings with ~:

Nested data access

Access nested properties and array elements:

Next steps