BoundaryML/baml · critical

baml.errors.Context.stack_trace: expected Instance

Error message

baml.errors.Context.stack_trace: expected Instance

What it means

This panic comes from an expect() in baml.errors.Context._to_string_impl while rendering a Python-style cause chain. Every link in the chain must carry a stack_trace field that is an Instance (a StackTrace object); when the stored value is not an Instance, vm.as_instance fails and the VM aborts with this message. It indicates a corrupted or wrongly-constructed error Context value, not a user error.

Source

Thrown at baml_language/crates/bex_vm/src/package_baml/error_context.rs:53

        while let Some(cause_value) = cause {
            let instance = vm
                .as_instance(&cause_value)
                .expect("baml.errors.Context.cause: expected Instance");
            let link = view::errors::Context { instance };
            links.push((link.error(), link.stack_trace()));
            cause = link.cause(vm);
        }

        // Render oldest → newest, Python-style.
        let mut out = String::new();
        for (i, (error, stack_trace)) in links.iter().rev().enumerate() {
            if i > 0 {
                out.push_str(CHAIN_SEPARATOR);
            }

            let st_instance = vm
                .as_instance(stack_trace)
                .expect("baml.errors.Context.stack_trace: expected Instance");
            let st_view = view::errors::StackTrace {
                instance: st_instance,
            };
            let trace =
                <PackageBamlImpl as BamlClassErrorsStackTrace>::_to_string_impl(vm, &st_view);
            let _ = write!(out, "{trace}");

            let _ = write!(out, "\n{}", render_error_value(vm, *error));
        }

        bex_str::BexStr::from(out.trim_end().to_string())
    }
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check any BAML code that constructs baml.errors.Context values and ensure stack_trace is always a StackTrace instance.
  2. Avoid mutating ctx.stack_trace after an error is raised; let the VM populate it.
  3. If triggered by ordinary error raising/handling with no manual construction, this is an internal invariant violation — report it to the BAML team with the reproducing BAML program.

Example fix

// before (BAML)
let ctx = baml.errors.Context(error=e, stack_trace="not a trace", cause=null)
// after
let ctx = baml.errors.Context(error=e, stack_trace=e.stack_trace, cause=null)
Defensive patterns

Strategy: type-guard

Validate before calling

// In BAML: never assign a non-StackTrace to Context.stack_trace
// check e.stack_trace is a StackTrace instance before constructing a Context manually

Type guard

// Rust-side guard before reading the field
fn stack_trace_is_instance(vm: &BexVm, v: &Value) -> bool {
    vm.as_instance(v).is_ok()
}

Try / catch

// Avoid the panic by validating instead of expect():
let st_instance = match vm.as_instance(stack_trace) {
    Ok(inst) => inst,
    Err(_) => return bex_str::BexStr::from(format!("<invalid stack_trace: {stack_trace:?}>")),
};

Prevention

When it happens

Trigger: Calling to_string() on a baml.errors.Context whose stack_trace field was assigned a non-Instance value (e.g. a string, null, or plain value) instead of a StackTrace instance, or when walking a cause chain whose link's stack_trace slot holds a malformed value.

Common situations: BAML code or tooling constructing a Context manually and setting stack_trace to a wrong value; internal VM bug while linking causes; a version mismatch where serialized error values lose their instance shape.

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/8a59e885e1197ff4. Report an issue: GitHub.