{"record":{"id":"9da947e68c26e34f","repo":"BoundaryML/baml","slug":"failed-to-render-doc","errorCode":null,"errorMessage":"Failed to render doc","messagePattern":"Failed to render doc","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/ast/src/formatter/mod.rs","lineNumber":46,"sourceCode":"    if ignore_directive_regex.is_match(source) {\n        return Ok(source.to_string());\n    }\n\n    let mut schema = BAMLParser::parse(Rule::schema, source)?;\n    let schema_pair = schema.next().ok_or(anyhow!(\"Expected a schema\"))?;\n    if schema_pair.as_rule() != Rule::schema {\n        return Err(anyhow!(\"Expected a schema\"));\n    }\n\n    let formatter = Formatter {\n        indent_width: format_options.indent_width,\n        fail_on_unhandled_rule: format_options.fail_on_unhandled_rule,\n    };\n\n    let doc = formatter.schema_to_doc(schema_pair.into_inner())?;\n    let mut w = Vec::new();\n    doc.render(10, &mut w)\n        .map_err(|_| anyhow!(\"Failed to render doc\"))?;\n    String::from_utf8(w).map_err(|_| anyhow!(\"Failed to convert to string\"))\n}\n\nmacro_rules! next_pair {\n    ($pairs:ident, $rule:expr) => {{\n        loop {\n            match $pairs.peek() {\n                Some(pair) => {\n                    if pair.as_rule() == Rule::NEWLINE {\n                        $pairs.next();\n                        continue;\n                    }\n                    if pair.as_rule() != $rule {\n                        break Err(anyhow!(\n                            \"Expected a {:?}, got a {:?} ({}:{})\",\n                            $rule,\n                            pair.as_rule(),\n                            file!(),","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/ast/src/formatter/mod.rs#L28-L64","documentation":"After building an RcDoc render tree via formatter.schema_to_doc, format_schema calls doc.render(10, &mut w); if the pretty-printer fails (e.g. a doc structure that cannot fit width constraints, typically from infinite/nested doc constructs), it maps the failure to 'Failed to render doc'.","triggerScenarios":"Calling format_schema on a schema whose parsed AST produces a malformed RcDoc that fails render at width 10 — e.g. pathological nesting or a doc built from an unsupported construct.","commonSituations":"Formatting a highly nested or unusual BAML schema that stresses the Wadler-style pretty printer, or a bug in a new formatter rule emitting an impossible doc.","solutions":["Retry rendering with a larger line width to rule out width-constraint failure.","Reduce nesting in the BAML source (split large class/function definitions).","If it persists, file a bug: this indicates a formatter bug producing a non-renderable doc."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"let formatted = match format_schema(source, &options) {\n    Ok(f) => f,\n    Err(e) if e.to_string().contains(\"Failed to render doc\") => {\n        // fall back to original source rather than failing the build\n        source.to_string()\n    }\n    Err(e) => return Err(e),\n};","preventionTips":["Keep BAML sources modestly nested; split very large class/function blocks.","Fall back to the unformatted source when rendering fails so tooling never hard-fails.","Test the formatter against your full corpus of .baml files in CI."],"tags":["rust","formatting","pretty-printer","internal"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}