BoundaryML/baml · error · RuntimeError

Failed to convert result to owned value: {0}

Error message

Failed to convert result to owned value: {0}

What it means

This error wraps `bex_heap::AccessError` and is thrown when the BEX bridge tries to convert a value out of the heap into an owned Rust value and the heap-level access fails. It surfaces as `Error::Access` in bex_project's error enum, meaning the underlying engine data is not accessible or convertible at that point.

Source

Thrown at baml_language/crates/bex_project/src/lib.rs:100

    }
}

/// Errors that can occur during runtime operations.
#[derive(Debug, Error)]
pub enum RuntimeError {
    #[error("{0}")]
    Other(String),

    #[error("Invalid argument: {name}")]
    InvalidArgument { name: String },

    #[error("{message}")]
    Compilation { message: String },

    #[error("{0}")]
    Engine(#[from] bex_engine::EngineError),

    #[error("Failed to convert result to owned value: {0}")]
    Access(#[from] bex_heap::AccessError),
}

/// True iff `err` wraps an engine cancellation panic.
///
/// Centralizes the cancellation-classification logic that bridges and the
/// LSP server need to distinguish cancellation from other runtime errors.
pub fn is_cancelled_runtime_error(err: &RuntimeError) -> bool {
    matches!(err, RuntimeError::Engine(e) if is_cancelled_engine_error(e))
}

/// Compile a BAML project from in-memory sources and initialize a runtime.
///
/// `files` are the project's `.baml` sources keyed by the path the host
/// spelled (relative to `root_path` or absolute); the embedded stdlib is
/// compiled from source alongside them. Compile errors surface as
/// [`RuntimeError::Compilation`] listing every diagnostic.
///

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check that the engine run completed successfully and the result handle is still valid before converting to an owned value.
  2. Inspect the inner bex_heap::AccessError (via Display/source chain) to identify whether it is an invalid handle, type mismatch, or lifetime issue.
  3. Ensure the engine/heap is not mutated or dropped while results are being read (avoid holding results across runs).
  4. If caused by cancellation, check the sibling cancellation-classification logic and handle cancellation before accessing results.

Example fix

// before
let value = project.result(handle)?.to_owned()?;
// after
let value = match project.result(handle) {
    Ok(v) => v.to_owned().map_err(|e| eprintln!("heap access failed: {e}"))?,
    Err(e) => { eprintln!("result unavailable: {e}"); return Err(e.into()); }
};
Defensive patterns

Strategy: try-catch

Validate before calling

// before converting
if !project.result_handle_is_valid(handle) {
    return Err("result handle no longer valid; re-run the engine");
}

Type guard

fn is_access_err(e: &ProjectError) -> Option<&bex_heap::AccessError> {
    match e { ProjectError::Access(a) => Some(a), _ => None }
}

Try / catch

match project.result(handle) {
    Ok(v) => match v.to_owned() {
        Ok(owned) => use_value(owned),
        Err(e) => log::error!("heap access failed: {e}"),
    },
    Err(e) => log::error!("project error: {e}"),
}

Prevention

When it happens

Trigger: Calling project/bridge APIs that read a result value from the BEX heap (via bex_heap accessors) when the heap value is invalid, freed, or otherwise not convertible to an owned value, e.g. after the engine has been torn down or a handle was invalidated.

Common situations: Holding a reference/handle to a value past the lifetime of the engine run; concurrent access while the heap is mutated; engine cancellation panics leaving results unconvertible.

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/1f9d69df7b4c6c16. Report an issue: GitHub.