BoundaryML/baml · error

Encountered impossible raw string during parsing

Error message

Encountered impossible raw string during parsing

What it means

This is a `unreachable!()` panic in `parse_raw_string` in the BAML AST parser. After looping over the children of a `raw_string_literal` pair, the function asserts it captured a `(content, span)` tuple; reaching the fallback means no child assigned content — the raw-string grammar produced a node with no recognized content token, breaking the grammar/parser contract. It indicates the raw string literal parsed to an empty/unrecognized shape.

Source

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

    for current in token.into_inner() {
        match current.as_rule() {
            Rule::raw_string_literal_content_1
            | Rule::raw_string_literal_content_2
            | Rule::raw_string_literal_content_3
            | Rule::raw_string_literal_content_4
            | Rule::raw_string_literal_content_5 => {
                content = Some((
                    current.as_str().to_string(),
                    diagnostics.span(current.as_span()),
                ));
            }
            _ => unreachable_rule(&current, "raw_string_literal", diagnostics),
        };
    }
    match content {
        Some((content, span)) => RawString::new(content, span),
        _ => unreachable!("Encountered impossible raw string during parsing"),
    }
}

// NOTE(sam): this doesn't handle unicode escape sequences e.g. \u1234
// also this has panicks in it (see the hex logic)
fn unescape_string(val: &str) -> String {
    let mut result = String::with_capacity(val.len());
    let mut chars = val.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('n') => result.push('\n'),
                Some('r') => result.push('\r'),
                Some('t') => result.push('\t'),
                Some('0') => result.push('\0'),
                Some('\'') => result.push('\''),
                Some('\"') => result.push('\"'),

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Rewrite the raw string using standard BAML raw-string syntax (`#"text"#`) with balanced delimiters
  2. Replace the raw string with a regular quoted string or template string if the escaping allows it
  3. Check the BAML source at the panic span for a truncated or malformed raw string and fix the delimiters
  4. If you are a contributor: extend `parse_raw_string`'s loop to handle all child rules of `raw_string_literal` in baml.pest

Example fix

// before (malformed raw string)
let s #"unterminated
// after
let s #"terminated"#
Defensive patterns

Strategy: validation

Validate before calling

// Validate raw string delimiters are balanced before parsing:
function assertBalancedRawString(src) {
  const opens = (src.match(/#"/g) || []).length;
  const closes = (src.match(/"#/g) || []).length;
  if (opens !== closes) {
    throw new Error(`Unbalanced raw string delimiters will crash the BAML parser: ${src}`);
  }
}

Type guard

const isWellFormedRawString = (s) => /^#"[\s\S]*"#$/.test(s.trim());

Try / catch

catch (panicOutput) {
  if (String(panicOutput).includes('Encountered impossible raw string')) {
    logParserBug('raw_string_literal yielded no content', panicOutput);
    return null;
  }
  throw panicOutput;
}

Prevention

When it happens

Trigger: Parsing a raw string literal (e.g. `#"..."#`) whose `raw_string_literal` pair contains only rules not matched inside the loop — possible after a grammar change to the raw-string rules without updating `parse_raw_string`, or a zero-child raw_string_literal pair.

Common situations: Using raw string syntax with unusual hash counts or delimiters in a BAML version where raw-string lexing changed; contributors editing baml.pest raw_string_literal rules without updating the parser; corrupted/empty string token reaching the parser.

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/efd86fb790afc6e5. Report an issue: GitHub.