BoundaryML/baml · error · StrongAstError

Unexpected additional element at {at:?} in {parent:?}

Error message

Unexpected additional element at {at:?} in {parent:?}

What it means

StrongAstError::UnexpectedAdditionalElement is raised when a node should have no more children (e.g. a statement finished parsing) but leftover children remain in the parent. This indicates the CST contains more elements than the strong-AST grammar consumes — a shape violation between the parser output and the AST builder's expectations.

Source

Thrown at baml_language/crates/baml_fmt/src/ast/mod.rs:70

    UnexpectedKindDesc {
        expected_desc: Cow<'static, str>,
        found: SyntaxKind,
        at: TextRange,
    },
    /// When an element is expected (of a specific [`SyntaxKind`]) but there were no more children left.
    #[error("Expected token/node of kind {expected:?}, but was unable to find it in {parent:?}")]
    MissingExpectedElement {
        expected: SyntaxKind,
        parent: TextRange,
    },
    /// When an element is expected (not of a single specific [`SyntaxKind`]) but there were no more children left.
    #[error("Expected token/node {desc}, but was unable to find it in {parent:?}")]
    MissingExpectedElementDesc {
        desc: Cow<'static, str>,
        parent: TextRange,
    },
    /// When the node isn't expected to have any more children (e.g. a statement found a `;`) but there are still children left.
    #[error("Unexpected additional element at {at:?} in {parent:?}")]
    UnexpectedAdditionalElement { parent: TextRange, at: TextRange },
    /// When an element is expected to be a node but it's actually a token.
    #[error("An element at {at:?} was a node when it should have been a token.")]
    ShouldBeNode { at: TextRange },
    /// When an element is expected to be a token but it's actually a node.
    #[error("An element at {at:?} was a token when it should have been a node.")]
    ShouldBeToken { at: TextRange },
}
impl StrongAstError {
    /// Checks that the given node is of the specified [`SyntaxKind`].
    ///
    /// # Errors
    /// Returns [`StrongAstError::UnexpectedKind`] if the element not the expected kind.
    pub fn assert_kind_node(node: &SyntaxNode, expected: SyntaxKind) -> Result<(), Self> {
        if node.kind() == expected {
            Ok(())
        } else {
            Err(Self::UnexpectedKind {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Inspect the source at the `at` TextRange and remove the extraneous token/element
  2. Re-parse and fix parser errors; stray tokens often result from typos (e.g. double semicolons)
  3. Update baml_fmt alongside any parser grammar changes so new child kinds are consumed
  4. If mutating trees programmatically, strip leftover children before conversion

Example fix

// before
let x = 1;;
// after
let x = 1;
Defensive patterns

Strategy: validation

Validate before calling

// after building the element, assert no leftover children remain
if node.children_with_tokens().next().is_some() {
    return Err("unexpected leftover children in node");
}

Try / catch

match StrongAst::from_cst(node) {
    Ok(ast) => use(ast),
    Err(StrongAstError::UnexpectedAdditionalElement { parent, at }) => {
        report(at, format!("extra element in node at {parent:?}"));
    }
    Err(e) => report_all(e),
}

Prevention

When it happens

Trigger: During FromCST conversion, after an AST element is fully constructed, another child is still present at `at` inside the parent (`parent` TextRange) — e.g. an extra `;`, stray token, or duplicated node left in the tree.

Common situations: Stray/duplicated tokens in BAML source; parser producing nodes with extra trailing children; grammar updates adding new node kinds not yet handled by the strong-AST builder.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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