{"record":{"id":"6fd54718af4db6b0","repo":"BoundaryML/baml","slug":"cannot-convert-to-owned-reason","errorCode":null,"errorMessage":"Cannot convert to owned: {reason}","messagePattern":"Cannot convert to owned: (.+?)","errorType":"exception","errorClass":"AccessError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_heap/src/accessor.rs","lineNumber":30,"sourceCode":"\n#[derive(Debug, PartialEq, thiserror::Error, Clone)]\npub enum AccessError {\n    #[error(\"Invalid handle: expected {expected}\")]\n    InvalidHandle { expected: &'static str },\n\n    #[error(\"Type mismatch: expected {expected}, got {actual}\")]\n    TypeMismatch {\n        expected: &'static str,\n        actual: String,\n    },\n\n    #[error(\"Field not found: expected {expected}\")]\n    FieldNotFound { expected: String },\n\n    #[error(\"Function not found: {expected}\")]\n    FunctionNotFound { expected: String },\n\n    #[error(\"Cannot convert to owned: {reason}\")]\n    CannotConvertToOwned { reason: String },\n}\n\npub enum BexValue<'a> {\n    ExternalValue(&'a BexExternalValue),\n    HeapPtr(&'a HeapPtr),\n    Value(&'a Value),\n    OwnedValue(Value),\n}\n\nimpl<'a> From<&'a BexExternalValue> for BexValue<'a> {\n    fn from(value: &'a BexExternalValue) -> Self {\n        BexValue::ExternalValue(value)\n    }\n}\n\npub enum BexClass<'a> {\n    ExternalClass {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_heap/src/accessor.rs#L12-L48","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","If the value is a HeapPtr, first dereference/resolve it to its concrete value via the heap accessor, then convert the resolved value.","Restructure code to consume the value within the heap's borrow scope instead of converting to owned.","If you need owned external values, ensure the external value itself is owned (BexExternalValue owned variant) rather than a borrowed reference."],"exampleFix":"// before\nlet owned = heap.convert_to_owned(value)?; // value is BexValue::HeapPtr\n// after\nlet resolved = heap.deref(value.as_heap_ptr())?; // resolve pointer to concrete value first\nlet owned = heap.convert_to_owned(resolved)?;","handlingStrategy":"try-catch","validationCode":"if let BexValue::HeapPtr(_) | BexValue::ExternalValue(_) = value {\n    // resolve to a concrete owned-representable value before converting\n}","typeGuard":"fn is_owned_convertible(v: &BexValue) -> bool {\n    !matches!(v, BexValue::HeapPtr(_) | BexValue::ExternalValue(_))\n}","tryCatchPattern":"match heap.convert_to_owned(value) {\n    Ok(owned) => owned,\n    Err(AccessError::CannotConvertToOwned { reason }) => {\n        // deref pointer / materialize value, then retry conversion\n        let resolved = heap.deref(ptr)?;\n        heap.convert_to_owned(resolved)?\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Always dereference HeapPtr values before requesting owned conversion","Keep value consumption within the heap borrow scope when possible","Match on the BexValue variant and handle pointer/external kinds explicitly"],"tags":["rust","heap","ownership","lifetime"],"backgroundTag":"unsupported-operation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}