Jinja filters allow you to transform and format values within your BAML prompts. Filters are applied using the pipe operator (|).
Serializes values into structured text while preserving all BAML-specific aliases. Use the required type argument to choose the output representation.
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 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:
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:
TOON options (only when format="toon"):
indent (int, default 2): spaces per indent leveldelimiter ("comma" | "tab" | "pipe", default "comma"): column separator for arrayslength_marker (single char, optional): prefix to annotate array lengthsInvalid 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:
When other formats may be better:
For benchmarks, detailed comparisons, and the full specification, see the TOON repository.
Tests if a string matches a regular expression pattern.
Signature: value|regex_match(pattern)
Example:
Sums all numeric values in an array.
Signature: array|sum
Example:
BAML supports all standard Minijinja filters. Here are some commonly used ones:
Returns the length of a string, array, or object.
Converts string to uppercase or lowercase.
Converts string to title case.
Removes whitespace from both ends of a string.
Provides a default value if the variable is undefined or empty.
Joins array elements into a string with a separator.
Returns the first or last element of an array.
Sorts an array.
Removes duplicate values from an array.
Reverses an array or string.
Replaces occurrences of a substring.
Rounds a number to a specified precision.
Returns the absolute value of a number.
You can chain multiple filters together:
For a complete list of standard Jinja filters, see the Minijinja documentation.