BoundaryML/baml · critical · VmPanic

baml.panics.AllocFailure

baml.panics.AllocFailure

Error message

memory allocation failed: {message}

What it means

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.

Solutions

  1. Reduce the memory footprint of the BAML program: avoid unbounded loops that append to arrays/strings and cap collection sizes.
  2. Catch baml.panics.AllocFailure and fall back to streaming or chunked processing instead of buffering whole payloads.
  3. Raise the memory limit of the host process / container (e.g. container memory limits, ulimit) if the workload legitimately needs more memory.
  4. Upgrade the VM or report the case if a single small allocation spuriously fails (possible allocator bug).

Example fix

// before
let mut parts = [];
for line in huge_stream {
  parts.push(line); // grows unbounded -> AllocFailure
}
// after
let mut count = 0;
for line in huge_stream {
  count += 1; // process incrementally, don't buffer
}
Defensive patterns

Strategy: try-catch

Validate before calling

// BAML: cap growth before allocating
if items.length > MAX_ITEMS {
  return err("too many items");
}

Try / catch

try {
  let big = build_large_collection();
} catch e: baml.panics.AllocFailure {
  log("OOM, falling back to chunked processing");
  return chunked_build();
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/e6779f2ce62dacff. Report an issue: GitHub.

Appendix: source

Thrown at baml_language/crates/bex_vm_types/src/errors.rs:79

    /// record for a user-violated native invariant (e.g. a reflection kind
    /// view's `_ty` field overwritten with a type of a different kind).
    #[error("baml.sys.panic: {message}")]
    UserPanic { message: String },

    /// A clean process-termination request from `baml.sys.exit(code)`.
    ///
    /// Catchable in user code as `baml.panics.Exit` — patterned after
    /// Python's `SystemExit`: code can intercept it for cleanup or
    /// testing, and if nothing catches it the engine surfaces the code
    /// as `EngineError::Exit` and the host terminates with it.
    ///
    /// BAML `int` is `i64`, so the signal carries the full value the
    /// user wrote; the host narrows to `i32` for `std::process::exit`.
    #[error("baml.sys.exit({code})")]
    Exit { code: i64 },

    /// The graceful-ish way to handle potential OOM errors, instead of hard-crashing.
    #[error("memory allocation failed: {message}")]
    AllocFailure { message: String },

    /// A required host resource is unavailable — e.g. the OS entropy source
    /// returned an error in a sandboxed runtime. Catchable so user code can
    /// fall back gracefully instead of aborting the host process.
    #[error("host resource '{resource}' unavailable: {message}")]
    HostUnavailable { resource: String, message: String },

    /// The right operand of a bigint shift (`<<` / `>>`) was negative.
    /// Catchable because the count is a runtime `bigint` and the type
    /// system can't rule out negative values.
    #[error("negative bit shift: {message}")]
    NegativeBitShift { message: String },

    /// A host callable returned a value of the wrong type, or threw a value
    /// that does not match its declared `throws` contract `E`. Surfaces in
    /// BAML as `baml.panics.HostContractViolation`.
    ///

View on GitHub (pinned to bd85ce9dee)