BoundaryML/baml · error

Mismatched brackets

Error message

Mismatched brackets

What it means

The multi-json parser tracks an opening-bracket stack while scanning; when a closing '}' or ']' does not match the most recent opener on the stack, it returns 'Mismatched brackets'. The input is structurally invalid JSON — brackets are crossed (e.g. '[}') rather than merely unbalanced.

Source

Thrown at engine/baml-lib/jsonish/src/jsonish/parser/multi_json_parser.rs:26

    let mut stack = Vec::new();
    let mut json_str_start = None;
    let mut json_objects = Vec::new();

    for (index, character) in str.char_indices() {
        match character {
            '{' | '[' => {
                if stack.is_empty() {
                    json_str_start = Some(index);
                }
                stack.push(character);
            }
            '}' | ']' => {
                if let Some(last) = stack.last() {
                    let expected_open = if character == '}' { '{' } else { '[' };
                    if *last == expected_open {
                        stack.pop();
                    } else {
                        return Err(anyhow::anyhow!("Mismatched brackets"));
                    }
                }

                if stack.is_empty() {
                    let end_index = index + 1;
                    let json_str = if let Some(start) = json_str_start {
                        &str[start..end_index]
                    } else {
                        &str[..end_index]
                    };
                    match entry::parse_func(
                        json_str,
                        options.next_from_mode(super::ParsingMode::AllJsonObjects),
                        false,
                    ) {
                        Ok(json) => json_objects.push(json),
                        Err(e) => {
                            // Ignore errors

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Validate the text with a JSON linter to locate the mismatched bracket position
  2. Ask the model to regenerate with strictly valid JSON (response_format json mode where available)
  3. Run the input through the fixing parser, which tolerates some bracket errors
  4. Reject/handle the error upstream rather than attempting to salvage crossed brackets
Defensive patterns

Strategy: validation

Validate before calling

fn brackets_crossed(s: &str) -> bool {
    let mut stack = Vec::new();
    for c in s.chars() {
        match c { '{' | '[' => stack.push(c), '}' | ']' => {
            let open = if c == '}' { '{' } else { '[' };
            match stack.pop() { Some(o) if o == open => {} _ => return true }
        } _ => {} }
    }
    false
}

Try / catch

match multi_json_parser::parse(text) {
    Err(e) if e.to_string().contains("Mismatched brackets") => retry_with_json_mode(),
    other => other,
}

Prevention

When it happens

Trigger: multi_json_parser::parse receiving text where a closer mismatches the top of the stack, e.g. '{...]' or '[...}', or stray closers interleaved across concatenated JSON objects.

Common situations: LLM corrupted output by mixing object/array syntax, or template code concatenated JSON fragments incorrectly.

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


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/b4323da3e867a4d8. Report an issue: GitHub.