{"record":{"id":"1f9d69df7b4c6c16","repo":"BoundaryML/baml","slug":"failed-to-convert-result-to-owned-value-0","errorCode":null,"errorMessage":"Failed to convert result to owned value: {0}","messagePattern":"Failed to convert result to owned value: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_project/src/lib.rs","lineNumber":100,"sourceCode":"    }\n}\n\n/// Errors that can occur during runtime operations.\n#[derive(Debug, Error)]\npub enum RuntimeError {\n    #[error(\"{0}\")]\n    Other(String),\n\n    #[error(\"Invalid argument: {name}\")]\n    InvalidArgument { name: String },\n\n    #[error(\"{message}\")]\n    Compilation { message: String },\n\n    #[error(\"{0}\")]\n    Engine(#[from] bex_engine::EngineError),\n\n    #[error(\"Failed to convert result to owned value: {0}\")]\n    Access(#[from] bex_heap::AccessError),\n}\n\n/// True iff `err` wraps an engine cancellation panic.\n///\n/// Centralizes the cancellation-classification logic that bridges and the\n/// LSP server need to distinguish cancellation from other runtime errors.\npub fn is_cancelled_runtime_error(err: &RuntimeError) -> bool {\n    matches!(err, RuntimeError::Engine(e) if is_cancelled_engine_error(e))\n}\n\n/// Compile a BAML project from in-memory sources and initialize a runtime.\n///\n/// `files` are the project's `.baml` sources keyed by the path the host\n/// spelled (relative to `root_path` or absolute); the embedded stdlib is\n/// compiled from source alongside them. Compile errors surface as\n/// [`RuntimeError::Compilation`] listing every diagnostic.\n///","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_project/src/lib.rs#L82-L118","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check that the engine run completed successfully and the result handle is still valid before converting to an owned value.","Inspect the inner bex_heap::AccessError (via Display/source chain) to identify whether it is an invalid handle, type mismatch, or lifetime issue.","Ensure the engine/heap is not mutated or dropped while results are being read (avoid holding results across runs).","If caused by cancellation, check the sibling cancellation-classification logic and handle cancellation before accessing results."],"exampleFix":"// before\nlet value = project.result(handle)?.to_owned()?;\n// after\nlet value = match project.result(handle) {\n    Ok(v) => v.to_owned().map_err(|e| eprintln!(\"heap access failed: {e}\"))?,\n    Err(e) => { eprintln!(\"result unavailable: {e}\"); return Err(e.into()); }\n};","handlingStrategy":"try-catch","validationCode":"// before converting\nif !project.result_handle_is_valid(handle) {\n    return Err(\"result handle no longer valid; re-run the engine\");\n}","typeGuard":"fn is_access_err(e: &ProjectError) -> Option<&bex_heap::AccessError> {\n    match e { ProjectError::Access(a) => Some(a), _ => None }\n}","tryCatchPattern":"match project.result(handle) {\n    Ok(v) => match v.to_owned() {\n        Ok(owned) => use_value(owned),\n        Err(e) => log::error!(\"heap access failed: {e}\"),\n    },\n    Err(e) => log::error!(\"project error: {e}\"),\n}","preventionTips":["Never hold result handles beyond the engine run's lifetime.","Avoid mutating or dropping the engine/heap while results are being read.","Check cancellation status before accessing results."],"tags":["rust","heap","internal-state"],"backgroundTag":"internal-invariant-violation","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"}