{"record":{"id":"89f7e87b86f14753","repo":"BoundaryML/baml","slug":"an-element-at-at-was-a-node-when-it-should-have-been-a-token","errorCode":null,"errorMessage":"An element at {at:?} was a node when it should have been a token.","messagePattern":"An element at (.+?) was a node when it should have been a token\\.","errorType":"exception","errorClass":"StrongAstError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_fmt/src/ast/mod.rs","lineNumber":73,"sourceCode":"        at: TextRange,\n    },\n    /// When an element is expected (of a specific [`SyntaxKind`]) but there were no more children left.\n    #[error(\"Expected token/node of kind {expected:?}, but was unable to find it in {parent:?}\")]\n    MissingExpectedElement {\n        expected: SyntaxKind,\n        parent: TextRange,\n    },\n    /// When an element is expected (not of a single specific [`SyntaxKind`]) but there were no more children left.\n    #[error(\"Expected token/node {desc}, but was unable to find it in {parent:?}\")]\n    MissingExpectedElementDesc {\n        desc: Cow<'static, str>,\n        parent: TextRange,\n    },\n    /// When the node isn't expected to have any more children (e.g. a statement found a `;`) but there are still children left.\n    #[error(\"Unexpected additional element at {at:?} in {parent:?}\")]\n    UnexpectedAdditionalElement { parent: TextRange, at: TextRange },\n    /// When an element is expected to be a node but it's actually a token.\n    #[error(\"An element at {at:?} was a node when it should have been a token.\")]\n    ShouldBeNode { at: TextRange },\n    /// When an element is expected to be a token but it's actually a node.\n    #[error(\"An element at {at:?} was a token when it should have been a node.\")]\n    ShouldBeToken { at: TextRange },\n}\nimpl StrongAstError {\n    /// Checks that the given node is of the specified [`SyntaxKind`].\n    ///\n    /// # Errors\n    /// Returns [`StrongAstError::UnexpectedKind`] if the element not the expected kind.\n    pub fn assert_kind_node(node: &SyntaxNode, expected: SyntaxKind) -> Result<(), Self> {\n        if node.kind() == expected {\n            Ok(())\n        } else {\n            Err(Self::UnexpectedKind {\n                expected,\n                found: node.kind(),\n                at: node.text_range(),","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_fmt/src/ast/mod.rs#L55-L91","documentation":"StrongAstError::ShouldBeNode is raised when the FromCST conversion expects the current child element to be a composite node but it is actually a bare token. The strong-AST builder distinguishes tokens from nodes; receiving the wrong element type at `at` aborts conversion.","triggerScenarios":"A conversion helper that descends into a child node (e.g. to build a nested statement or expression) encounters a leaf token at that position instead — the CST places a token where the grammar requires a subtree.","commonSituations":"Flattened or malformed trees from parser recovery; version drift between parser and baml_fmt; hand-constructed trees attaching tokens where nodes are required.","solutions":["Check the source at `at` and fix the BAML syntax so the expected structure is present","Fix parser errors first — error recovery can substitute tokens for nodes","Keep parser and formatter crate versions aligned","When building trees manually, wrap children in the proper node kind"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"// verify the element at the position is a node, not a token\nlet el = parent.children_with_tokens().next()?;\nif matches!(el, SyntaxElement::Token(_)) {\n    return Err(\"expected node, found token\");\n}","typeGuard":"fn is_node(e: &SyntaxElement) -> bool { matches!(e, SyntaxElement::Node(_)) }","tryCatchPattern":"match StrongAst::from_cst(node) {\n    Ok(ast) => use(ast),\n    Err(StrongAstError::ShouldBeNode { at }) => {\n        report(at, \"expected a node but found a token\");\n    }\n    Err(e) => report_all(e),\n}","preventionTips":["Fix parser recovery artifacts by clearing parse errors first","Keep parser and baml_fmt versions aligned","Wrap children in proper node kinds when constructing trees manually","Check the `at` range source for flattened/malformed syntax"],"tags":["parser","ast","syntax","formatter"],"backgroundTag":"schema-validation-failed","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}