Jinja Filters

Jinja filters allow you to transform and format values within your BAML prompts. Filters are applied using the pipe operator (|).

Basic Usage

1{{ value|filter_name }}
2{{ value|filter_name(arg1, arg2) }}

BAML Custom Filters

format

Serializes values into structured text while preserving all BAML-specific aliases. Use the required type argument to choose the output representation.

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

Required arguments:

  • type (string): "yaml", "json", or "toon" (alias for TOON serializer)

Format-specific behavior:

FormatDescriptionExtra kwargs
yamlHuman-friendly YAML string with enum/class aliasesNone
jsonStandard JSON string (minified) with alias-safe serializationNone
toonCompact TOON format for uniform arraysindent, delimiter, length_marker

All formats reuse the same alias-aware serializer, so enums emit their @alias values and class properties honor @alias names at every nesting level (lists, maps, nested classes, etc.).

Examples:

1{# YAML for readability #}
2{{ organization|format(type="yaml") }}
3
4{# JSON for API interop #}
5{{ organization|format(type="json") }}
6
7{# TOON with custom options #}
8{{ organization|format(type="toon", indent=4, delimiter="tab") }}

TOON options (only when format="toon"):

  • indent (int, default 2): spaces per indent level
  • delimiter ("comma" | "tab" | "pipe", default "comma"): column separator for arrays
  • length_marker (single char, optional): prefix to annotate array lengths

Invalid combinations raise a template error (e.g., missing type or an unknown delimiter for TOON).

About TOON:

TOON (Token-Oriented Object Notation) combines YAML’s indentation with CSV-style tabular layout. It’s designed for uniform arrays of objects (multiple fields per row, same structure across items), where it can provide significant token savings while maintaining LLM-friendly structure validation through explicit array lengths and field headers.

When TOON works best:

  • Uniform arrays of objects (e.g., lists of users, transactions, products)
  • Highly structured, tabular data

When other formats may be better:

  • Deeply nested structures (compact JSON may use fewer tokens)
  • Semi-uniform or mixed data
  • Pure flat tables (CSV is more compact)

For benchmarks, detailed comparisons, and the full specification, see the TOON repository.

regex_match

Tests if a string matches a regular expression pattern.

Signature: value|regex_match(pattern)

Example:

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

sum

Sums all numeric values in an array.

Signature: array|sum

Example:

1Total: {{ prices|sum }}

Standard Jinja Filters

BAML supports all standard Minijinja filters. Here are some commonly used ones:

length

Returns the length of a string, array, or object.

1{{ items|length }} items

upper / lower

Converts string to uppercase or lowercase.

1{{ name|upper }}
2{{ name|lower }}

title

Converts string to title case.

1{{ title|title }}

trim

Removes whitespace from both ends of a string.

1{{ text|trim }}

default

Provides a default value if the variable is undefined or empty.

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

join

Joins array elements into a string with a separator.

1{{ tags|join(", ") }}

first / last

Returns the first or last element of an array.

1{{ items|first }}
2{{ items|last }}

sort

Sorts an array.

1{% for item in items|sort %}
2 {{ item }}
3{% endfor %}

unique

Removes duplicate values from an array.

1{{ values|unique }}

reverse

Reverses an array or string.

1{{ items|reverse }}

replace

Replaces occurrences of a substring.

1{{ text|replace("old", "new") }}

round

Rounds a number to a specified precision.

1{{ price|round(2) }}

abs

Returns the absolute value of a number.

1{{ value|abs }}

Chaining Filters

You can chain multiple filters together:

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

More Information

For a complete list of standard Jinja filters, see the Minijinja documentation.