{"record":{"id":"15548ac1af8ef6d0","repo":"BoundaryML/baml","slug":"baml-panics-invalidfieldaccess","errorCode":"baml.panics.InvalidFieldAccess","errorMessage":"invalid field access: field {field_index} of {field_count}","messagePattern":"invalid field access: field (.+?) of (.+?)","errorType":"error_code","errorClass":"VmPanic","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":39,"sourceCode":"/// `ThrowIfPanic` instruction filters which panics are caught vs rethrown.\n#[derive(Debug, Error, PartialEq, Clone)]\npub enum VmPanic {\n    #[error(\"division by zero: {left:?} / {right:?}\")]\n    DivisionByZero { left: Value, right: Value },\n\n    /// An `int` (i63) arithmetic operation overflowed the representable\n    /// range `[INT_MIN, INT_MAX]`. Carries a human-readable description of\n    /// the operation (e.g. `\"4611686018427387903 + 1\"`); built only on the\n    /// cold overflow path, so the `String` alloc never touches hot code.\n    #[error(\"integer overflow: {message}\")]\n    IntegerOverflow { message: String },\n\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\")]","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L21-L57","documentation":"Raised when a VM field access targets a field index that does not exist on the value being accessed. The error reports the field index requested and the number of fields the record actually has. It indicates the accessor and the record shape are out of sync.","triggerScenarios":"Accessing a struct/record field by index where the index exceeds the record's field_count, typically from compiled field access instructions against a record of a different/older layout (e.g. after a schema change or accessing a field on the wrong type).","commonSituations":"Running cached/compiled artifacts after the schema changed (fields added/removed/reordered), reflection over records with stale type info, accessing a field on a record type that was never validated at compile time.","solutions":["Recompile the BAML program so field indices match the current record layout.","Verify the value being accessed actually is the intended record type before field access.","Catch `baml.panics.InvalidFieldAccess` if dynamic/reflection-style access is intentional."],"exampleFix":"// before\n// compiled bytecode for old schema: field index 5 no longer exists\n// after\n// regenerate bytecode from the updated .baml schema, then re-run","handlingStrategy":"type-guard","validationCode":"// BAML: confirm the value has the expected field count before access\nif (record.field_count() <= FIELD_IDX) { return err(\"stale schema?\"); }","typeGuard":"// BAML: narrow before field access\nif (is_my_record(v)) { return v.expected_field; }","tryCatchPattern":"// BAML\ntry {\n  let f = record.field(FIELD_IDX);\n} catch (e: baml.panics.InvalidFieldAccess) {\n  return err(\"record layout mismatch\");\n}","preventionTips":["Recompile after every schema change; never mix old bytecode with new records","Validate record types at boundaries before field access","Avoid reaching into reflection internals directly"],"tags":["vm","record","field-access","panic","baml"],"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"}