> Version notice: This site documents BAML v0, the legacy DSL. BAML v1 is a separate, fully featured programming language in public beta. If the user intends to use BAML v1, ignore this site's content and use the BAML v1 documentation instead. If the intended version is unclear, clarify it before using this page. > For clean Markdown of any page, append .md to the page URL. For the complete documentation index, see https://docs.boundaryml.com/llms.txt. For MCP access, connect to https://docs.boundaryml.com/_mcp/server. # Jinja Filters Jinja filters allow you to transform and format values within your BAML prompts. Filters are applied using the pipe operator (`|`). ## Basic Usage ```jinja {{ value|filter_name }} {{ 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. > **Note** > > This is BAML's serialization filter, not Python's `%`-style string formatting. The `|format` filter in BAML always requires a `type` keyword argument. > > For string formatting, use Python's `.format()` method syntax instead: `"{}, {}!".format("Hello", "World")`. See the [String Formatting](/ref/prompt-syntax/what-is-jinja#string-formatting) cookbook for examples. **Signature**: `value|format(type="yaml" | "json" | "toon", **kwargs)` **Required arguments**: * `type` (string): `"yaml"`, `"json"`, or `"toon"` (alias for TOON serializer) **Format-specific behavior**: | Format | Description | Extra kwargs | | ------ | ------------------------------------------------------------- | -------------------------------------- | | `yaml` | Human-friendly YAML string with enum/class aliases | None | | `json` | Standard JSON string (minified) with alias-safe serialization | None | | `toon` | Compact TOON format for uniform arrays | `indent`, `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**: ```jinja {# YAML for readability #} {{ organization|format(type="yaml") }} {# JSON for API interop #} {{ organization|format(type="json") }} {# TOON with custom options #} {{ 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](https://github.com/toon-format/toon). ### regex\_match Tests if a string matches a regular expression pattern. **Signature**: `value|regex_match(pattern)` **Example**: ```jinja {% if phone_number|regex_match("\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}") %} Valid phone number {% endif %} ``` ### sum Sums all numeric values in an array. **Signature**: `array|sum` **Example**: ```jinja Total: {{ 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. ```jinja {{ items|length }} items ``` ### upper / lower Converts string to uppercase or lowercase. ```jinja {{ name|upper }} {{ name|lower }} ``` ### title Converts string to title case. ```jinja {{ title|title }} ``` ### trim Removes whitespace from both ends of a string. ```jinja {{ text|trim }} ``` ### default Provides a default value if the variable is undefined or empty. ```jinja {{ user_name|default("Guest") }} ``` ### join Joins array elements into a string with a separator. ```jinja {{ tags|join(", ") }} ``` ### first / last Returns the first or last element of an array. ```jinja {{ items|first }} {{ items|last }} ``` ### sort Sorts an array. ```jinja {% for item in items|sort %} {{ item }} {% endfor %} ``` ### unique Removes duplicate values from an array. ```jinja {{ values|unique }} ``` ### reverse Reverses an array or string. ```jinja {{ items|reverse }} ``` ### replace Replaces occurrences of a substring. ```jinja {{ text|replace("old", "new") }} ``` ### round Rounds a number to a specified precision. ```jinja {{ price|round(2) }} ``` ### abs Returns the absolute value of a number. ```jinja {{ value|abs }} ``` ## Chaining Filters You can chain multiple filters together: ```jinja {{ name|trim|lower|title }} {{ items|unique|sort|join(", ") }} ``` ## More Information For a complete list of standard Jinja filters, see the [Minijinja documentation](https://docs.rs/minijinja/latest/minijinja/filters/index.html#functions).