BoundaryML/baml · error

Encountered impossible jinja expression during parsing

Error message

Encountered impossible jinja expression during parsing

What it means

This is an internal parser invariant panic in BAML's AST parser. After parsing the inner tokens of a jinja expression ({{ ... }}), the code assumes the grammar always yields exactly one value expression; if none was produced it calls unreachable!(). Hitting it means the pest grammar and the Rust parser code disagree — a bug in the library, not user input the parser was designed to reject.

Source

Thrown at engine/baml-lib/ast/src/parser/parse_expression.rs:637

                Expression::JinjaExpressionValue(
                    JinjaExpression(inner_text),
                    diagnostics.span(token.as_span()),
                )
            }
            _ => {
                unreachable_rule(&token, "jinja_expression", diagnostics);
                Expression::JinjaExpressionValue(
                    JinjaExpression(String::new()),
                    diagnostics.span(token.as_span()),
                )
            }
        })
        .next();

    if let Some(value) = value {
        value
    } else {
        unreachable!("Encountered impossible jinja expression during parsing")
    }
}

pub fn parse_class_constructor(token: Pair<'_>, diagnostics: &mut Diagnostics) -> Expression {
    assert_correct_parser(&token, &[Rule::class_constructor], diagnostics);

    let span = diagnostics.span(token.as_span());
    let mut tokens = token.into_inner();

    let ident_token = tokens.next().expect("Guaranteed by the grammar");

    let class_name = match ident_token.as_rule() {
        Rule::identifier => parse_identifier(ident_token, diagnostics),
        Rule::path_identifier => parse_path_identifier(ident_token, diagnostics),
        _ => panic!("Encountered impossible class constructor during parsing"),
    };

    let mut fields = Vec::new();

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check the .bml source at the reported span for an empty or degenerate {{ }} expression and fix the input
  2. Report the input snippet to the BAML repository (bug in grammar/parser mismatch)
  3. Pin/downgrade to a previous BAML version where the jinja grammar matched the parser
  4. Rebuild the parser to ensure the grammar .pest file and generated code are in sync

Example fix

// before (input)
prompt {{}}
// after
prompt {{ name }}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-scan .bml prompt text for empty jinja expressions before parsing
fn has_empty_jinja(src: &str) -> bool {
    src.match_indices("{{").any(|(i, _)| {
        let rest = &src[i + 2..];
        match rest.find("}}") {
            Some(j) => rest[..j].trim().is_empty(),
            None => true,
        }
    })
}
if has_empty_jinja(source) { panic!("fix empty {{ }} before parsing"); }

Type guard

fn is_valid_jinja(expr: &str) -> bool {
    let inner = expr.trim().trim_start_matches("{{").trim_end_matches("}}").trim();
    !inner.is_empty()
}

Try / catch

// Panic (not Result) — isolate parsing in a subprocess/catch_unwind
let result = std::panic::catch_unwind(|| baml_ast::parse(source));
match result {
    Ok(ast) => use_ast(ast),
    Err(_) => report_parser_bug(source),
}

Prevention

When it happens

Trigger: Calling parse_jinja_expression with a Rule::jinja_expression pair whose inner token stream yields no expression after token filtering — i.e. an empty or malformed inner content that nevertheless matched the grammar rule.

Common situations: Developers hit this when using a BAML file with jinja-style {{ }} expressions after upgrading BAML versions where the grammar changed, or when the parser's token filter (.filter / map chain) drops the only child token. Effectively only reachable through library development or a genuine parser bug.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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