{"record":{"id":"a5ff0c6ef686af5e","repo":"BoundaryML/baml","slug":"baml-panics-assertionfailed","errorCode":"baml.panics.AssertionFailed","errorMessage":"assertion failed","messagePattern":"assertion failed","errorType":"error_code","errorClass":"VmPanic","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":51,"sourceCode":"\n    // Raised by array and byte-array subscripting, so the message stays generic\n    // (\"index\", not \"array index\").\n    #[error(\"index out of bounds: {index} of {length}\")]\n    IndexOutOfBounds { index: i64, length: usize },\n\n    #[error(\"invalid field access: field {field_index} of {field_count}\")]\n    InvalidFieldAccess {\n        field_index: usize,\n        field_count: usize,\n    },\n\n    #[error(\"key not found in map\")]\n    MapKeyNotFound,\n\n    #[error(\"stack overflow\")]\n    StackOverflow,\n\n    #[error(\"assertion failed\")]\n    AssertionFailed,\n\n    #[error(\"unreachable code executed\")]\n    Unreachable,\n\n    #[error(\"operation cancelled\")]\n    Cancelled,\n\n    /// A user-caused panic from `baml.sys.panic`, and the stdlib's panic of\n    /// record for a user-violated native invariant (e.g. a reflection kind\n    /// view's `_ty` field overwritten with a type of a different kind).\n    #[error(\"baml.sys.panic: {message}\")]\n    UserPanic { message: String },\n\n    /// A clean process-termination request from `baml.sys.exit(code)`.\n    ///\n    /// Catchable in user code as `baml.panics.Exit` — patterned after\n    /// Python's `SystemExit`: code can intercept it for cleanup or","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L33-L69","documentation":"Raised when a VM assertion instruction fails, i.e. a runtime check inserted into the BAML program evaluated to false. It carries no payload; the surrounding program is expected to provide context. It surfaces user-authored or compiler-inserted invariant checks.","triggerScenarios":"Executing an `assert` (or compiler-generated assertion) whose condition is false at runtime, e.g. `assert(x > 0)` where x is 0 or negative.","commonSituations":"Assertions validating preconditions on user input, invariants after transformations (e.g. sortedness, non-emptiness), regressions after refactoring where an assumption no longer holds.","solutions":["Inspect the failing condition and validate inputs before reaching the assertion.","Replace the bare assert with an explicit check that returns a descriptive error to the caller.","Catch `baml.panics.AssertionFailed` at the boundary only if you cannot fix the condition upstream."],"exampleFix":"// before\nassert(items.length > 0);\n// after\nif (items.length == 0) { return err(\"items must be non-empty\"); }","handlingStrategy":"try-catch","validationCode":"// BAML: validate the precondition instead of asserting it\nif (items.length == 0) { return err(\"items must be non-empty\"); }","typeGuard":null,"tryCatchPattern":"// BAML\ntry {\n  run_checked(input);\n} catch (e: baml.panics.AssertionFailed) {\n  return err(\"assertion violated for input \" + describe(input));\n}","preventionTips":["Turn assertions on external input into explicit error returns","Re-audit assertions after refactors that change invariants","Keep assertions for internal invariants only, not user data"],"tags":["vm","assertion","panic","baml"],"backgroundTag":"invalid-argument-value","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"}