BoundaryML/baml · error

Name should always be defined for attribute.

Error message

Name should always be defined for attribute.

What it means

parse_attribute builds an Attribute only when the attribute name pair exists; the None branch is marked unreachable!('Name should always be defined for attribute.'). It is an internal invariant assertion meaning the pest grammar guaranteed a name but the code saw none — a panic, not a catchable anyhow error.

Source

Thrown at engine/baml-lib/ast/src/parser/parse_attribute.rs:45

        match current.as_rule() {
            Rule::identifier => name = parse_identifier(current, diagnostics).into(),
            Rule::path_identifier => name = parse_path_identifier(current, diagnostics).into(),
            Rule::arguments_list => {
                parse_arguments_list(current, &mut arguments, &name, diagnostics)
            }
            _ => parsing_catch_all(current, "attribute", diagnostics),
        }
    }

    match name {
        Some(name) => Attribute {
            name,
            arguments,
            parenthesized,
            span,
        },
        // This is suspicious, can probably cause a panic
        None => unreachable!("Name should always be defined for attribute."),
    }
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Use a consistent, released version of the baml ast crate (grammar + parser from the same build).
  2. If reproducible on a released version, report the .baml snippet as a parser bug.
  3. As a workaround, remove/rewrite the attribute that triggers the panic.
Defensive patterns

Strategy: try-catch

Validate before calling

if let Some(attr) = pair.clone().into_inner().next() {
    parse_attribute(attr)?;
} else {
    eprintln!("skipping degenerate attribute token");
}

Type guard

fn attribute_has_name(pair: &Pair<Rule>) -> bool {
    pair.clone().into_inner().next().is_some()
}

Try / catch

let result = std::panic::catch_unwind(|| parse_attribute(token));
match result {
    Ok(attr) => attr,
    Err(_) => return Err(anyhow!("malformed attribute token")),
}

Prevention

When it happens

Trigger: Calling parse_attribute with a token whose inner pairs are empty — only possible from a grammar/code desync (grammar changed so @attr name pair is optional) or a corrupted/partially built pair.

Common situations: Mixing crate versions where the pest grammar and parser hand-written code come from different builds; custom downstream builds of the ast crate.

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