BoundaryML/baml · error · VmPanic

baml.panics.IndexOutOfBounds

baml.panics.IndexOutOfBounds

Error message

index out of bounds: {index} of {length}

What it means

Raised by array and byte-array subscripting when an index is outside the valid range of the container. The message stays generic ("index", not "array index") because both arrays and byte arrays use it. It reports the offending index and the container's length.

Source

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

///
/// These are user-visible runtime errors (division by zero, index out of
/// bounds, etc.) that can be caught by `catch` handlers. The handler's
/// `ThrowIfPanic` instruction filters which panics are caught vs rethrown.
#[derive(Debug, Error, PartialEq, Clone)]
pub enum VmPanic {
    #[error("division by zero: {left:?} / {right:?}")]
    DivisionByZero { left: Value, right: Value },

    /// An `int` (i63) arithmetic operation overflowed the representable
    /// range `[INT_MIN, INT_MAX]`. Carries a human-readable description of
    /// the operation (e.g. `"4611686018427387903 + 1"`); built only on the
    /// cold overflow path, so the `String` alloc never touches hot code.
    #[error("integer overflow: {message}")]
    IntegerOverflow { message: String },

    // Raised by array and byte-array subscripting, so the message stays generic
    // ("index", not "array index").
    #[error("index out of bounds: {index} of {length}")]
    IndexOutOfBounds { index: i64, length: usize },

    #[error("invalid field access: field {field_index} of {field_count}")]
    InvalidFieldAccess {
        field_index: usize,
        field_count: usize,
    },

    #[error("key not found in map")]
    MapKeyNotFound,

    #[error("stack overflow")]
    StackOverflow,

    #[error("assertion failed")]
    AssertionFailed,

    #[error("unreachable code executed")]

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check `0 <= index && index < arr.length` before subscripting.
  2. Catch `baml.panics.IndexOutOfBounds` around the access if the lookup is optional.
  3. Compute length-derived indices from the actual `.length` rather than hardcoded constants.

Example fix

// before
let last = items[items.length];
// after
let last = items[items.length - 1];
Defensive patterns

Strategy: validation

Validate before calling

// BAML: check before subscripting
if (i < 0 || i >= items.length) { return err("bad index"); }
let v = items[i];

Try / catch

// BAML
try {
  let v = bytes[i];
} catch (e: baml.panics.IndexOutOfBounds) {
  return default_value;
}

Prevention

When it happens

Trigger: Evaluating `arr[i]` or `bytes[i]` where `i` is negative or >= the collection length, e.g. `bytes[bytes.length]` or indexing with a computed offset that went out of range.

Common situations: Off-by-one in loops (`i <= len` instead of `i < len`), empty arrays indexed at 0, parsing byte payloads with assumed fixed offsets, index math from user input or unvalidated parse results.

Related errors


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