{"record":{"id":"0b2e7b16be0d0043","repo":"BoundaryML/baml","slug":"failed-to-convert-to-string","errorCode":null,"errorMessage":"Failed to convert to string","messagePattern":"Failed to convert to string","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/ast/src/formatter/mod.rs","lineNumber":47,"sourceCode":"        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!(),\n                            line!()","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/ast/src/formatter/mod.rs#L29-L65","documentation":"After a successful doc.render into a Vec<u8>, format_schema converts the bytes to a String via String::from_utf8; non-UTF8 output maps to 'Failed to convert to string'. Since BAML source is UTF-8, this indicates corrupted renderer output.","triggerScenarios":"Calling format_schema where the rendered byte buffer contains invalid UTF-8 — practically only from a formatter bug or non-UTF8 input leaking through parsing.","commonSituations":"Feeding files with invalid UTF-8 bytes (e.g. Latin-1 encoded bytes) that survived pest parsing into the formatter.","solutions":["Verify the .baml file is valid UTF-8 (iconv/file -bi) before formatting.","Re-encode the input file to UTF-8.","If input is valid UTF-8, this is a formatter bug — report it."],"exampleFix":"// before\nlet formatted = format_schema(source, options)?;\n// after\nif std::str::from_utf8(source.as_bytes()).is_err() {\n    anyhow::bail!(\"source is not valid UTF-8\");\n}\nlet formatted = format_schema(source, options)?;","handlingStrategy":"validation","validationCode":"if std::str::from_utf8(source.as_bytes()).is_err() {\n    anyhow::bail!(\"BAML source must be valid UTF-8 before formatting\");\n}","typeGuard":"fn is_valid_utf8(bytes: &[u8]) -> bool {\n    std::str::from_utf8(bytes).is_ok()\n}","tryCatchPattern":null,"preventionTips":["Always save .baml files as UTF-8 (configure editor encoding).","Reject non-UTF-8 files at read time with a clear message.","Normalize inputs from external systems with an encoding sniff/convert step."],"tags":["rust","formatting","utf-8","encoding"],"backgroundTag":"invalid-argument-format","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"}