BoundaryML/baml · error

Map.get_or_insert: should be dispatched via…

Error message

Map.get_or_insert: should be dispatched via __glue_get_or_insert

What it means

`Map.get_or_insert` is a placeholder that always panics: this operation must be dispatched via the `__glue_get_or_insert` glue method, which can allocate new entries inside the VM. The raw function exists only to fill the native-function table and must never be called.

Solutions

  1. Fix the dispatch table so `Map.get_or_insert` resolves to `__glue_get_or_insert`.
  2. Call get_or_insert through the normal BAML map API surface.
  3. Rebuild/upgrade bex_vm if a released build exhibits this panic.

Example fix

// before
"get_or_insert" => NativeFn::Raw(map::get_or_insert),
// after
"get_or_insert" => NativeFn::Glue(map::__glue_get_or_insert),
Defensive patterns

Strategy: validation

Validate before calling

assert!(dispatch_table.get("get_or_insert") == Some(&NativeFn::Glue(map::__glue_get_or_insert)));

Try / catch

// Rust panic — cover the dispatch table with a registration unit test.

Prevention

When it happens

Trigger: Dispatch routing `Map.get_or_insert` to the raw placeholder instead of `__glue_get_or_insert`, due to a misregistered native method name in the map package.

Common situations: VM development or custom glue dispatch layers that bypass the __glue_ naming convention for map operations.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at baml_language/crates/bex_vm/src/package_baml/map.rs:111

    }

    // ── get_or_insert ─────────────────────────────────────────────────────────

    fn __glue_get_or_insert(vm: &mut BexVm, args: &[Value]) -> NativeCallResult {
        let result: NativeFunctionResult = (|| {
            let key_as_string = vm.as_string(&args[1])?.clone();
            let default = args[2];
            let mut map = vm.as_map_mut(&args[0])?;
            Ok(*map.entry(key_as_string).or_insert(default))
        })();
        match result {
            Ok(v) => NativeCallResult::Done(v),
            Err(e) => NativeCallResult::Error(e),
        }
    }

    fn get_or_insert(_map: &mut IndexMap<BexStr, Value>, _key: &Value, _default: &Value) -> Value {
        unreachable!("Map.get_or_insert: should be dispatched via __glue_get_or_insert")
    }

    // ── clear ─────────────────────────────────────────────────────────────────

    #[allow(clippy::unused_unit)]
    fn clear(map: &mut IndexMap<BexStr, Value>) -> () {
        map.clear();
    }
}

View on GitHub (pinned to bd85ce9dee)