BoundaryML/baml · error · VmPanic

baml.panics.MapKeyNotFound

baml.panics.MapKeyNotFound

Error message

key not found in map

What it means

Raised when a map lookup does not find the requested key. Unlike some runtimes it returns a panic rather than an optional value, so absence of a key is treated as an exceptional condition. The error carries no payload since the key is known at the call site.

Source

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

    /// 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")]
    Unreachable,

    #[error("operation cancelled")]
    Cancelled,

    /// A user-caused panic from `baml.sys.panic`, and the stdlib's panic of
    /// 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}")]

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check membership before indexing, or use the map API that returns an option/default value.
  2. Normalize keys (trim/case) on both write and read sides.
  3. Catch `baml.panics.MapKeyNotFound` and substitute a default when absence is acceptable.

Example fix

// before
let port = config["port"];
// after
let port = config.get("port").unwrap_or(8080);
Defensive patterns

Strategy: validation

Validate before calling

// BAML: membership check before indexing
if (config.contains("port")) { let port = config["port"]; }

Try / catch

// BAML
try {
  let v = map[key];
} catch (e: baml.panics.MapKeyNotFound) {
  let v = default;
}

Prevention

When it happens

Trigger: Evaluating a map indexing instruction with a key absent from the map, e.g. `map["missing_key"]` or a computed key that never matches stored keys.

Common situations: Typos in string keys, keys built from user input or external data with different formats (casing, whitespace), assuming a config/env map contains a key it does not.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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