BoundaryML/baml · error · FormatterError
{0:?}
Error message
{0:?} What it means
FormatterError::ParseErrors wraps a Vec<ParseError> produced when the baml_fmt formatter fails to parse the input source. The formatter cannot build an AST from a lossy tree, so it collects all parse errors and returns them via this variant; the Display impl debug-prints the error list. It is the top-level formatter error for syntactically invalid BAML input.
Source
Thrown at baml_language/crates/baml_fmt/src/lib.rs:132
pub line_width: usize,
/// Indent width. Default: `4`
pub indent_width: usize,
}
impl Default for FormatOptions {
fn default() -> Self {
Self {
line_width: 100,
indent_width: CANONICAL_INDENT_WIDTH,
}
}
}
/// Canonical indentation used by the CLI, LSP, and default library formatter.
pub const CANONICAL_INDENT_WIDTH: usize = 4;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum FormatterError {
#[error("{0:?}")]
ParseErrors(Vec<ParseError>),
#[error("{0}")]
StrongAstError(#[from] ast::StrongAstError),
}
#[cfg(test)]
mod format_options_tests {
use super::*;
#[test]
fn default_options_use_the_canonical_four_space_indent() {
let options = FormatOptions::default();
assert_eq!(CANONICAL_INDENT_WIDTH, 4);
assert_eq!(options.indent_width, CANONICAL_INDENT_WIDTH);
let formatted = format("function value() -> int {\n result\n}\n", &options)
.expect("default formatter options should format a function body");
assert_eq!(formatted, "function value() -> int {\n result\n}\n");
}View on GitHub (pinned to bd85ce9dee)
Solutions
- Fix the BAML syntax errors listed in the wrapped ParseErrors (each includes its range) before formatting
- Run the parser/`baml` CLI diagnostics on the file to enumerate all errors
- Upgrade baml_fmt/parser if the file uses syntax newer than the installed toolchain
- Exclude generated or known-bad files from format-on-save until they are valid
Example fix
// before (unclosed string) prompt " Hello // after prompt " Hello "
Defensive patterns
Strategy: validation
Validate before calling
// before formatting, run the parser and bail on any errors
let (tree, errors) = parse_baml(source);
if !errors.is_empty() {
return Err(FormatterError::ParseErrors(errors)); // handle before formatting
} Try / catch
match format_source(source) {
Ok(formatted) => save(formatted),
Err(FormatterError::ParseErrors(errs)) => {
for e in errs { report_parse_error(e); }
// skip writing formatted output
}
Err(e) => report_other(e),
} Prevention
- Fix all syntax errors before running the formatter or format-on-save
- Run parser diagnostics/`baml` CLI checks in CI to catch broken files early
- Disable format-on-save for buffers with active parse errors in LSP setups
- Keep the BAML toolchain updated to parse newer syntax
When it happens
Trigger: Calling the formatter (CLI format, LSP formatting, or library format API) on BAML source containing syntax errors — the parse step yields one or more ParseErrors which are returned wrapped in this variant.
Common situations: Formatting a file with typos, unclosed braces/strings, or unsupported/newer syntax; running `baml fmt` on an in-progress edit; LSP format-on-save over a broken buffer; formatting files from a newer BAML version with an older formatter.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- Expected token/node of kind {expected:?}, but found {found:?
- Expected token/node {expected_desc}, but found {found:?} at
- Expected token/node of kind {expected:?}, but was unable to
- Expected token/node {desc}, but was unable to find it in {pa
- Unexpected additional element at {at:?} in {parent:?}
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/55670d0591f26a8b.
Report an issue: GitHub.