{"record":{"id":"8a59e885e1197ff4","repo":"BoundaryML/baml","slug":"baml-errors-context-stack-trace-expected-instance","errorCode":null,"errorMessage":"baml.errors.Context.stack_trace: expected Instance","messagePattern":"baml\\.errors\\.Context\\.stack_trace: expected Instance","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"baml_language/crates/bex_vm/src/package_baml/error_context.rs","lineNumber":53,"sourceCode":"        while let Some(cause_value) = cause {\n            let instance = vm\n                .as_instance(&cause_value)\n                .expect(\"baml.errors.Context.cause: expected Instance\");\n            let link = view::errors::Context { instance };\n            links.push((link.error(), link.stack_trace()));\n            cause = link.cause(vm);\n        }\n\n        // Render oldest → newest, Python-style.\n        let mut out = String::new();\n        for (i, (error, stack_trace)) in links.iter().rev().enumerate() {\n            if i > 0 {\n                out.push_str(CHAIN_SEPARATOR);\n            }\n\n            let st_instance = vm\n                .as_instance(stack_trace)\n                .expect(\"baml.errors.Context.stack_trace: expected Instance\");\n            let st_view = view::errors::StackTrace {\n                instance: st_instance,\n            };\n            let trace =\n                <PackageBamlImpl as BamlClassErrorsStackTrace>::_to_string_impl(vm, &st_view);\n            let _ = write!(out, \"{trace}\");\n\n            let _ = write!(out, \"\\n{}\", render_error_value(vm, *error));\n        }\n\n        bex_str::BexStr::from(out.trim_end().to_string())\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":67,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm/src/package_baml/error_context.rs#L35-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check any BAML code that constructs baml.errors.Context values and ensure stack_trace is always a StackTrace instance.","Avoid mutating ctx.stack_trace after an error is raised; let the VM populate it.","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."],"exampleFix":"// before (BAML)\nlet ctx = baml.errors.Context(error=e, stack_trace=\"not a trace\", cause=null)\n// after\nlet ctx = baml.errors.Context(error=e, stack_trace=e.stack_trace, cause=null)","handlingStrategy":"type-guard","validationCode":"// In BAML: never assign a non-StackTrace to Context.stack_trace\n// check e.stack_trace is a StackTrace instance before constructing a Context manually","typeGuard":"// Rust-side guard before reading the field\nfn stack_trace_is_instance(vm: &BexVm, v: &Value) -> bool {\n    vm.as_instance(v).is_ok()\n}","tryCatchPattern":"// Avoid the panic by validating instead of expect():\nlet st_instance = match vm.as_instance(stack_trace) {\n    Ok(inst) => inst,\n    Err(_) => return bex_str::BexStr::from(format!(\"<invalid stack_trace: {stack_trace:?}>\")),\n};","preventionTips":["Never manually construct baml.errors.Context with a fabricated stack_trace value.","Let the VM populate error context fields; read-only from BAML code.","Pin BAML tooling and runtime to matching versions so error values keep their shape.","Report any panic of this form with a reproducer — it signals a VM bug."],"tags":["baml-vm","panic","cause-chain","invariant-violation","stack-trace"],"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"}