BoundaryML/baml · error · AccessError

Cannot convert to owned: {reason}

Error message

Cannot convert to owned: {reason}

What it means

BexHeap's AccessError::CannotConvertToOwned is raised when a borrowed BexValue (e.g. a reference into the heap or an external value) cannot be converted into an owned, 'a-free BexValue. Conversion only works for value kinds that have an owned representation; a heap pointer or external reference with no owned clone fails. The `reason` string names the specific kind that could not be converted.

Source

Thrown at baml_language/crates/bex_heap/src/accessor.rs:30

#[derive(Debug, PartialEq, thiserror::Error, Clone)]
pub enum AccessError {
    #[error("Invalid handle: expected {expected}")]
    InvalidHandle { expected: &'static str },

    #[error("Type mismatch: expected {expected}, got {actual}")]
    TypeMismatch {
        expected: &'static str,
        actual: String,
    },

    #[error("Field not found: expected {expected}")]
    FieldNotFound { expected: String },

    #[error("Function not found: {expected}")]
    FunctionNotFound { expected: String },

    #[error("Cannot convert to owned: {reason}")]
    CannotConvertToOwned { reason: String },
}

pub enum BexValue<'a> {
    ExternalValue(&'a BexExternalValue),
    HeapPtr(&'a HeapPtr),
    Value(&'a Value),
    OwnedValue(Value),
}

impl<'a> From<&'a BexExternalValue> for BexValue<'a> {
    fn from(value: &'a BexExternalValue) -> Self {
        BexValue::ExternalValue(value)
    }
}

pub enum BexClass<'a> {
    ExternalClass {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Read the `reason` field to identify which value kind failed, and materialize that kind into a supported owned form (e.g. copy primitive/bytes values) before conversion.
  2. If the value is a HeapPtr, first dereference/resolve it to its concrete value via the heap accessor, then convert the resolved value.
  3. Restructure code to consume the value within the heap's borrow scope instead of converting to owned.
  4. If you need owned external values, ensure the external value itself is owned (BexExternalValue owned variant) rather than a borrowed reference.

Example fix

// before
let owned = heap.convert_to_owned(value)?; // value is BexValue::HeapPtr
// after
let resolved = heap.deref(value.as_heap_ptr())?; // resolve pointer to concrete value first
let owned = heap.convert_to_owned(resolved)?;
Defensive patterns

Strategy: try-catch

Validate before calling

if let BexValue::HeapPtr(_) | BexValue::ExternalValue(_) = value {
    // resolve to a concrete owned-representable value before converting
}

Type guard

fn is_owned_convertible(v: &BexValue) -> bool {
    !matches!(v, BexValue::HeapPtr(_) | BexValue::ExternalValue(_))
}

Try / catch

match heap.convert_to_owned(value) {
    Ok(owned) => owned,
    Err(AccessError::CannotConvertToOwned { reason }) => {
        // deref pointer / materialize value, then retry conversion
        let resolved = heap.deref(ptr)?;
        heap.convert_to_owned(resolved)?
    }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: Calling the heap accessor's to-owned/conversion API on a BexValue::HeapPtr or BexValue::ExternalValue that has no owned representation; retrieving a result from the engine that is still a pointer into heap memory and requesting an owned copy of it.

Common situations: Crossing an API boundary that requires 'static values (e.g. storing results beyond the heap's lifetime); extracting function-call results from the bex runtime into plain owned data; holding a value after the borrow of the heap has ended.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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