{"record":{"id":"d5726868b1b35af8","repo":"BoundaryML/baml","slug":"diagnostics","errorCode":null,"errorMessage":"{diagnostics}","messagePattern":"\\{diagnostics\\}","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/type_builder/mod.rs","lineNumber":677,"sourceCode":"    /// `type_builder.add_baml(\"BAML CODE\")`\n    pub fn add_baml(&self, baml: &str, rt: &BamlRuntime) -> anyhow::Result<()> {\n        use internal_baml_core::{\n            internal_baml_ast::parse_type_builder_contents_from_str,\n            internal_baml_diagnostics::{Diagnostics, SourceFile},\n            ir::repr::IntermediateRepr,\n            run_validation_pipeline_on_db, validate_type_builder_entries,\n        };\n\n        let path = std::path::PathBuf::from(\"TypeBuilder::add_baml\");\n        let source = SourceFile::from((path.clone(), baml));\n\n        let mut diagnostics = Diagnostics::new(path);\n        diagnostics.set_source(&source);\n\n        let type_builder_entries = parse_type_builder_contents_from_str(baml, &mut diagnostics)?;\n\n        if diagnostics.has_errors() {\n            anyhow::bail!(\"{}\", diagnostics.to_pretty_string());\n        }\n\n        // TODO: A bunch of mem usage here but at least we drop this one at the\n        // end of the function, unlike scoped DBs for type builders.\n        let mut scoped_db = rt.db.clone();\n\n        let local_ast =\n            validate_type_builder_entries(&mut diagnostics, &scoped_db, &type_builder_entries);\n        scoped_db.add_ast(local_ast);\n\n        if let Err(d) = scoped_db.validate(&mut diagnostics) {\n            diagnostics.push(d);\n            anyhow::bail!(\"{}\", diagnostics.to_pretty_string());\n        }\n\n        run_validation_pipeline_on_db(&mut scoped_db, &mut diagnostics);\n\n        if diagnostics.has_errors() {","sourceCodeStart":659,"sourceCodeEnd":695,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/type_builder/mod.rs#L659-L695","documentation":"add_baml() parses user-supplied BAML source with parse_type_builder_contents_from_str and collects syntax diagnostics; if any errors were recorded during parsing, the whole call is aborted with the pretty-printed diagnostics. This means the string passed to type_builder.add_baml(...) is not syntactically valid BAML. The library fails fast at parse time so invalid dynamic types never reach the runtime.","triggerScenarios":"Calling type_builder.add_baml(baml_string) (from Python/TS/Ruby wrappers) where baml_string fails BAML parsing, e.g. a malformed class/enum/type-alias declaration inside the dynamic BAML snippet.","commonSituations":"Hand-built or interpolated BAML strings with missing braces, typos in keywords, unquoted field defaults, or template-generated snippets that produce broken syntax; users dynamically composing BAML from config or LLM output.","solutions":["Read the pretty-printed diagnostics in the error message; they point to the exact line/column in the snippet that failed to parse.","Fix the syntax error in the string passed to add_baml (missing braces, bad keyword, malformed field).","If the snippet is generated, print it before calling add_baml and inspect it as a .baml file in an editor with BAML syntax support.","Keep the dynamic snippet minimal and mirror a working .baml file's syntax instead of writing BAML from scratch."],"exampleFix":"// before\nawait tb.add_baml(`class Foo { prop string`);\n// after\nawait tb.add_baml(`class Foo {\\n  prop string\\n}`);","handlingStrategy":"validation","validationCode":"function validateBamlSnippet(src) {\n  if (typeof src !== 'string' || src.trim() === '') throw new Error('add_baml requires non-empty BAML source');\n  if ((src.split('{').length) !== (src.split('}').length)) throw new Error('Unbalanced braces in BAML snippet');\n  return true;\n}\nvalidateBamlSnippet(bamlSrc);","typeGuard":"const isBamlSource = (v) => typeof v === 'string' && v.trim().length > 0;","tryCatchPattern":null,"preventionTips":["Store dynamic BAML in .baml template files linted by the BAML CLI instead of inline strings.","Print the generated snippet before add_baml and eyeball it with BAML syntax highlighting.","Keep snippets small; build up complex schemas incrementally.","Never interpolate user input directly into BAML without escaping/validating."],"tags":["baml","syntax-error","parser","type-builder"],"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-14T05:17:10.506Z"}