{"record":{"id":"46d5aad81acc15d4","repo":"BoundaryML/baml","slug":"baml-panics-stackoverflow","errorCode":"baml.panics.StackOverflow","errorMessage":"stack overflow","messagePattern":"stack overflow","errorType":"error_code","errorClass":"VmPanic","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":48,"sourceCode":"    /// cold overflow path, so the `String` alloc never touches hot code.\n    #[error(\"integer overflow: {message}\")]\n    IntegerOverflow { message: String },\n\n    // Raised by array and byte-array subscripting, so the message stays generic\n    // (\"index\", not \"array index\").\n    #[error(\"index out of bounds: {index} of {length}\")]\n    IndexOutOfBounds { index: i64, length: usize },\n\n    #[error(\"invalid field access: field {field_index} of {field_count}\")]\n    InvalidFieldAccess {\n        field_index: usize,\n        field_count: usize,\n    },\n\n    #[error(\"key not found in map\")]\n    MapKeyNotFound,\n\n    #[error(\"stack overflow\")]\n    StackOverflow,\n\n    #[error(\"assertion failed\")]\n    AssertionFailed,\n\n    #[error(\"unreachable code executed\")]\n    Unreachable,\n\n    #[error(\"operation cancelled\")]\n    Cancelled,\n\n    /// A user-caused panic from `baml.sys.panic`, and the stdlib's panic of\n    /// record for a user-violated native invariant (e.g. a reflection kind\n    /// view's `_ty` field overwritten with a type of a different kind).\n    #[error(\"baml.sys.panic: {message}\")]\n    UserPanic { message: String },\n\n    /// A clean process-termination request from `baml.sys.exit(code)`.","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L30-L66","documentation":"Raised when the VM's call/eval stack exceeds its maximum depth. This protects the interpreter from unbounded recursion exhausting host memory. It is a control-flow limit, not a memory allocation failure.","triggerScenarios":"Deeply or infinitely recursive BAML functions, mutual recursion without a base case, or extremely deep non-recursive evaluation (e.g. deeply nested closures/chain calls) exceeding the VM stack limit.","commonSituations":"Recursive tree/graph traversal without memoization, recursion whose base case depends on data that never terminates, porting code written for a host language with a much larger stack.","solutions":["Fix the recursion: add or repair the base case so recursion terminates.","Rewrite deep recursion iteratively with an explicit worklist/stack.","Catch `baml.panics.StackOverflow` at a top-level boundary to fail gracefully, though recovery is limited."],"exampleFix":"// before\nfn walk(n) { return walk(n); } // no base case\n// after\nfn walk(n) { if (n <= 0) { return 0; } return walk(n - 1); }","handlingStrategy":"validation","validationCode":"// BAML: cap recursion depth up front\nfn walk(n: int, depth: int) -> int {\n  if (depth > 10_000) { return err(\"too deep\"); }\n  ...\n}","typeGuard":null,"tryCatchPattern":"// BAML\ntry {\n  return recurse(input);\n} catch (e: baml.panics.StackOverflow) {\n  return err(\"input too deeply nested\");\n}","preventionTips":["Always verify recursive base cases are reachable","Add a depth parameter to recursive traversals","Convert deep recursion to iteration with an explicit stack"],"tags":["vm","recursion","stack","panic","baml"],"backgroundTag":"invalid-state-transition","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"}