{"record":{"id":"387a671a9fd6b418","repo":"BoundaryML/baml","slug":"recursion-depth-exceeded-for-0","errorCode":null,"errorMessage":"Recursion depth exceeded for {0}","messagePattern":"Recursion depth exceeded for (.+?)","errorType":"error_code","errorClass":"ConvertError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_sap/src/sap_model/convert.rs","lineNumber":42,"sourceCode":"    #[error(\"Failed to parse float: {0}\")]\n    ParseFloat(#[from] std::num::ParseFloatError),\n    #[error(\"Unknown media kind\")]\n    UnknownMediaKind,\n    #[error(\"Float literals cannot be parsed\")]\n    FloatLiteral,\n    #[error(\"Non-parsable type: {0:?}\")]\n    NonParsableType(Box<SapTy>),\n    #[error(\"Unknown class: {0}\")]\n    UnknownClass(DefKey),\n    #[error(\"Unknown enum: {0}\")]\n    UnknownEnum(DefKey),\n    #[error(\"Unknown type alias: {0}\")]\n    UnknownTypeAlias(DefKey),\n    #[error(\"Unknown name (could not determine if it was a class, enum, or type alias): {0}\")]\n    UnknownName(DefKey),\n    #[error(\"Could not add a type to the database as the name `{0}` is already present\")]\n    AlreadyPresent(DefKey),\n    #[error(\"Recursion depth exceeded for {0}\")]\n    RecursionDepthExceeded(&'static str),\n    #[error(\"Unions must be flattened\")]\n    UnflattenedUnion,\n    /// Something like `type A = B; type B = A;` is invalid.\n    #[error(\"Recursive type alias without indirection: {0}\")]\n    DirectRecursiveTypeAlias(DefKey),\n    #[error(\"Internal error (please report): {0}\")]\n    InternalError(&'static str),\n}\n\nconst MAX_RECURSION_DEPTH: usize = 16;\n\n/// Contains stuff from [`sys_types::SysOpContext`] that we need for converting to the sap model.\n///\n/// ## Representation\n/// - Unions should be flattened:\n///   - Union members cannot be unions\n///   - Union members cannot be optional","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_sap/src/sap_model/convert.rs#L24-L60","documentation":"ConvertError::RecursionDepthExceeded is raised when type-alias resolution or field-attribute derivation recurses past MAX_RECURSION_DEPTH (16). This guards against runaway recursion through chains of type aliases or nested unions during SAP model conversion. The payload string describes which derivation exceeded the limit (e.g. \"type alias\", \"class field nullability derivation\").","triggerScenarios":"convert_type_alias recursing beyond depth 16 through chained aliases; get_field_attrs or field_type_is_nullable_inner walking an alias/union chain deeper than 16 levels.","commonSituations":"Very long alias chains (type A1 = A2; type A2 = A3; ...); deeply nested union types produced by runtime generic substitution; generated code that programmatically nests types.","solutions":["Flatten or shorten the alias chain so references resolve in fewer than 16 hops.","Replace deep alias chaining with a direct reference to the underlying type.","If legitimately deep nesting is required, restructure it into intermediate classes instead of alias chains."],"exampleFix":"// before\ntype A = B\ntype B = C\n// ... 20 levels ...\ntype T = string\n// after\ntype A = string // collapse the chain","handlingStrategy":"validation","validationCode":"// Count alias-chain depth before conversion; bail early if it approaches 16\nfn alias_chain_depth(defs: &HashMap<DefKey, SapTy>, mut cur: DefKey) -> usize {\n    let mut depth = 0;\n    while let Some(SapTy::TypeAlias(next, _)) = defs.get(&cur) {\n        depth += 1;\n        cur = next.clone();\n    }\n    depth\n}","typeGuard":null,"tryCatchPattern":"match result {\n    Err(ConvertError::RecursionDepthExceeded(what)) => eprintln!(\"shorten the {what} chain to <= 16 levels\"),\n    other => other,\n}","preventionTips":["Keep type-alias chains shorter than a handful of hops.","Collapse generated alias chains into direct references.","Model deep structures with classes instead of nested aliases."],"tags":["sap","baml","recursion","type-alias","depth-limit"],"backgroundTag":"value-out-of-range","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"}