{"record":{"id":"e6779f2ce62dacff","repo":"BoundaryML/baml","slug":"baml-panics-allocfailure","errorCode":"baml.panics.AllocFailure","errorMessage":"memory allocation failed: {message}","messagePattern":"memory allocation failed: (.+?)","errorType":"error_code","errorClass":"VmPanic","httpStatus":null,"severity":"critical","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":79,"sourceCode":"    /// 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)`.\n    ///\n    /// Catchable in user code as `baml.panics.Exit` — patterned after\n    /// Python's `SystemExit`: code can intercept it for cleanup or\n    /// testing, and if nothing catches it the engine surfaces the code\n    /// as `EngineError::Exit` and the host terminates with it.\n    ///\n    /// BAML `int` is `i64`, so the signal carries the full value the\n    /// user wrote; the host narrows to `i32` for `std::process::exit`.\n    #[error(\"baml.sys.exit({code})\")]\n    Exit { code: i64 },\n\n    /// The graceful-ish way to handle potential OOM errors, instead of hard-crashing.\n    #[error(\"memory allocation failed: {message}\")]\n    AllocFailure { message: String },\n\n    /// A required host resource is unavailable — e.g. the OS entropy source\n    /// returned an error in a sandboxed runtime. Catchable so user code can\n    /// fall back gracefully instead of aborting the host process.\n    #[error(\"host resource '{resource}' unavailable: {message}\")]\n    HostUnavailable { resource: String, message: String },\n\n    /// The right operand of a bigint shift (`<<` / `>>`) was negative.\n    /// Catchable because the count is a runtime `bigint` and the type\n    /// system can't rule out negative values.\n    #[error(\"negative bit shift: {message}\")]\n    NegativeBitShift { message: String },\n\n    /// A host callable returned a value of the wrong type, or threw a value\n    /// that does not match its declared `throws` contract `E`. Surfaces in\n    /// BAML as `baml.panics.HostContractViolation`.\n    ///","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L61-L97","documentation":"This panic variant signals that a memory allocation in the BAML VM failed. It is the graceful, catchable way to surface out-of-memory (OOM) conditions instead of hard-crashing the host process, so user BAML code can attempt recovery or unwind cleanly.","triggerScenarios":"A BAML program allocates memory (e.g. building a very large string, array, or bigint) and the allocator rejects the request; the VM raises baml.panics.AllocFailure with a message describing the failed allocation.","commonSituations":"Running BAML programs with unbounded recursion or loops that grow collections, processing very large LLM outputs/inputs in memory, or executing in memory-constrained sandboxes/containers with a low memory limit.","solutions":["Reduce the memory footprint of the BAML program: avoid unbounded loops that append to arrays/strings and cap collection sizes.","Catch baml.panics.AllocFailure and fall back to streaming or chunked processing instead of buffering whole payloads.","Raise the memory limit of the host process / container (e.g. container memory limits, ulimit) if the workload legitimately needs more memory.","Upgrade the VM or report the case if a single small allocation spuriously fails (possible allocator bug)."],"exampleFix":"// before\nlet mut parts = [];\nfor line in huge_stream {\n  parts.push(line); // grows unbounded -> AllocFailure\n}\n// after\nlet mut count = 0;\nfor line in huge_stream {\n  count += 1; // process incrementally, don't buffer\n}","handlingStrategy":"try-catch","validationCode":"// BAML: cap growth before allocating\nif items.length > MAX_ITEMS {\n  return err(\"too many items\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  let big = build_large_collection();\n} catch e: baml.panics.AllocFailure {\n  log(\"OOM, falling back to chunked processing\");\n  return chunked_build();\n}","preventionTips":["Avoid unbounded loops that append to arrays or strings.","Process large payloads in chunks or streams instead of buffering.","Set realistic memory limits for containers running BAML programs.","Profile memory usage of large workflows before production."],"tags":["memory","allocation","oom","vm-panic","catchable"],"backgroundTag":"memory-allocation-failed","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"}