Skip to main content

Jinja Filters

Filters transform values in BAML prompts using the pipe operator (|).

Syntax​

{{ value|filter_name }}
{{ value|filter_name(arg1, arg2) }}
{{ value|filter1|filter2|filter3 }}

BAML Custom Filters​

format​

Serializes values to structured text, preserving BAML aliases.

Signature: value|format(type="yaml" | "json" | "toon", **kwargs)

FormatDescription
yamlHuman-friendly YAML with aliases
jsonStandard JSON (minified)
toonCompact TOON format for uniform arrays
{{ organization|format(type="yaml") }}
{{ organization|format(type="json") }}
{{ organization|format(type="toon", indent=4, delimiter="tab") }}

TOON Options:

OptionDefaultDescription
indent2Spaces per indent level
delimiter"comma"Column separator ("comma", "tab", "pipe")
length_marker(none)Prefix for array lengths

regex_match​

Tests if a string matches a regex pattern.

{% if phone|regex_match("\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}") %}
Valid phone number
{% endif %}

sum​

Sums numeric values in an array.

Total: {{ prices|sum }}

Standard Jinja Filters​

String Filters​

FilterDescription
upperConverts to uppercase
lowerConverts to lowercase
titleConverts to title case
trimRemoves leading/trailing whitespace
replace(old, new)Replaces substring
{{ name|upper }}
{{ name|lower }}
{{ title|title }}
{{ text|trim }}
{{ text|replace("old", "new") }}

Array Filters​

FilterDescription
lengthReturns item count
firstReturns first element
lastReturns last element
join(sep)Joins with separator
sortSorts array
uniqueRemoves duplicates
reverseReverses order
{{ items|length }}
{{ items|first }}
{{ items|last }}
{{ tags|join(", ") }}
{{ items|sort }}
{{ values|unique }}
{{ items|reverse }}

Number Filters​

FilterDescription
round(n)Rounds to n decimal places
absReturns absolute value
{{ price|round(2) }}
{{ value|abs }}

Default Values​

{{ user_name|default("Guest") }}

Chaining Filters​

{{ name|trim|lower|title }}
{{ items|unique|sort|join(", ") }}

TOON Format​

TOON (Token-Oriented Object Notation) combines YAML indentation with CSV-style tabular layout.

Best for:

  • Uniform arrays of objects
  • Highly structured, tabular data

Consider alternatives for:

  • Deeply nested structures (use JSON)
  • Mixed/semi-uniform data
  • Pure flat tables (use CSV)

See the TOON repository for details.

More Information​

See the Minijinja documentation for all standard filters.

See Also​