{"record":{"id":"19e8e6d4f1f316ec","repo":"BoundaryML/baml","slug":"array-assignment-index-out-of-bounds","errorCode":null,"errorMessage":"array assignment index out of bounds","messagePattern":"array assignment index out of bounds","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1536,"sourceCode":"                        run_llm_function,\n                        watch_handler,\n                        function_name,\n                    )\n                    .await?,\n                )?;\n\n                let idx = match index_value {\n                    BamlValueWithMeta::Int(i, _) if i >= 0 => i as usize,\n                    _ => bail!(\n                        \"array assignment requires a non-negative integer index at {:?}\",\n                        meta.0\n                    ),\n                };\n\n                match &mut base_value {\n                    BamlValueWithMeta::List(items, _) => {\n                        if idx >= items.len() {\n                            bail!(\"array assignment index out of bounds\");\n                        }\n                        items[idx] = value_to_assign.clone();\n                    }\n                    _ => bail!(\"array assignment on non-list value at {:?}\", meta.0),\n                }\n\n                value_to_assign = base_value;\n                current_expr = base.as_ref();\n            }\n            _ => return Err(anyhow!(\"Complex assignment targets not yet supported\")),\n        }\n    }\n}\n\nfn lookup(scopes: &[Scope], name: &str) -> Option<BamlValueWithMeta<ExprMetadata>> {\n    for s in scopes.iter().rev() {\n        if let Some(cell) = s.variables.get(name) {\n            return Some(cell.lock().unwrap().clone());","sourceCodeStart":1518,"sourceCodeEnd":1554,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1518-L1554","documentation":"After confirming the index is a valid non-negative integer, the interpreter checks that the target list actually has an element at that position. BAML's array assignment does not auto-extend lists, so `idx >= items.len()` bails with `array assignment index out of bounds`.","triggerScenarios":"`arr[i] = v` where `i` equals or exceeds the current length of the list stored in `arr`.","commonSituations":"Appending by assigning at `len(arr)` (Python-style `arr[len] = x`); sparse index writes; loops whose upper bound is computed incorrectly.","solutions":["Extend the list before assigning (e.g. rebind `arr = arr + [value]` to append)","Assign only to indices `0..len(arr)-1`","Fix loop bounds so the index stays within the current length","Pre-populate the list to the required size before positional writes"],"exampleFix":"// before\nlet arr = [1, 2];\narr[2] = 3; // out of bounds\n// after\nlet arr = [1, 2];\nlet arr = arr + [3]; // append","handlingStrategy":"validation","validationCode":"// ensure the index is within the current list length\nassert i < arr.len();","typeGuard":"fn in_bounds(len: usize, idx: usize) -> bool { idx < len }","tryCatchPattern":"// host-side\nmatch result {\n    Err(e) if e.to_string().contains(\"index out of bounds\") => {\n        // fall back to append (arr = arr + [v]) or extend first\n    }\n    other => other?,\n}","preventionTips":["Append with concatenation instead of assigning past the end","Pre-size lists before positional writes","Double-check loop bounds against list length"],"tags":["baml","interpreter","assignment","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}