{"record":{"id":"2f14602dfec088fa","repo":"BoundaryML/baml","slug":"non-regular-recursive-generic-class-class-expands-from","errorCode":null,"errorMessage":"Non-regular recursive generic class '{class}' expands from '{ancestor}' to '{instantiation}'","messagePattern":"Non-regular recursive generic class '(.+?)' expands from '(.+?)' to '(.+?)'","errorType":"exception","errorClass":"RenderError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/sys_ops/src/output_format.rs","lineNumber":17,"sourceCode":"use std::fmt::Write as _;\n\nuse ::sys_types::SapTy;\nuse baml_base::Literal as LiteralValue;\nuse indexmap::IndexMap;\nuse thiserror::Error;\n\n/// Error type for output format rendering.\n#[derive(Clone, Debug, Error)]\npub enum RenderError {\n    #[error(\"Enum '{0}' not found\")]\n    EnumNotFound(String),\n    #[error(\"Class '{0}' not found\")]\n    ClassNotFound(String),\n    #[error(\"Type '{0}' is not supported in outputs\")]\n    UnsupportedType(String),\n    #[error(\n        \"Non-regular recursive generic class '{class}' expands from '{ancestor}' to '{instantiation}'\"\n    )]\n    NonRegularRecursiveGeneric {\n        class: String,\n        ancestor: String,\n        instantiation: String,\n    },\n    #[error(\n        \"Output definitions '{first}' and '{second}' both render as '{rendered_name}' in the output schema\"\n    )]\n    RenderedClassNameCollision {\n        rendered_name: String,\n        first: String,\n        second: String,\n    },\n    #[error(\n        \"Output definitions '{first}' and '{second}' both render as '{rendered_name}' in the output schema\"\n    )]","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/sys_ops/src/output_format.rs#L1-L35","documentation":"BAML emits this error while expanding a recursive generic class into its output schema. A recursive generic is 'non-regular' if the generic parameters change shape across recursion levels — e.g. a class instantiating its own ancestor with different type arguments than the fixed point requires. The compiler refuses to expand it because the expansion would diverge or produce an infinite/unrepresentable type.","triggerScenarios":"Declaring a BAML class that recursively references itself or an ancestor generic class with a different type instantiation than the regular fixed point; the expansion loop detects the mismatch between the ancestor's declaration and the new instantiation and raises this variant of OutputFormatError.","commonSituations":"Modeling recursive trees/linked lists in BAML schema with generics; typo in a type argument so the recursive instantiation doesn't match the original; attempting mutual recursion through generic parameters.","solutions":["Make the recursive instantiation regular: ensure the class references its ancestor with the exact same type arguments as the original declaration","Break the recursion by introducing a base case class or optional field so expansion terminates","Replace the recursive generic with a concrete (non-generic) recursive type or an alias if parameterization isn't needed"],"exampleFix":"// before\nclass Node<T> { child Node<Node<T>> }\n// after\nclass Node { child Node? }","handlingStrategy":"validation","validationCode":"fn is_regular_recursive(class: &ClassDef) -> bool {\n    class.type_params.iter().all(|tp| class.uses_ancestor_with_same_args(tp))\n}\nif !is_regular_recursive(&my_class) { /* fix instantiation before generating schema */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep recursive generic instantiations identical to the declaring arguments","Prefer concrete recursive types (e.g. Option<Self>) over re-parameterized recursion","Test schema generation for recursive classes early"],"tags":["baml","schema","generics","recursion"],"backgroundTag":"unsupported-operation","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"}