{"record":{"id":"60fa3b8ed8cf2867","repo":"BoundaryML/baml","slug":"array-assignment-requires-a-non-negative-integer-index-at","errorCode":null,"errorMessage":"array assignment requires a non-negative integer index at {:?}","messagePattern":"array assignment requires a non-negative integer index at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1527,"sourceCode":"                        function_name,\n                    )\n                    .await?,\n                )?;\n                let index_value = expect_value(\n                    evaluate_expr(\n                        index,\n                        scopes,\n                        thir,\n                        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            }","sourceCodeStart":1509,"sourceCodeEnd":1545,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1509-L1545","documentation":"For array-element assignment targets `arr[i] = v`, the interpreter evaluates the index expression and requires it to be a non-negative integer (`Int(i)` with `i >= 0`). Any other value type (string, float, negative int) causes this bail, which includes the expression's source span via `meta.0`.","triggerScenarios":"`arr[i] = v` where `i` evaluates to a string, float, bool, or a negative integer.","commonSituations":"Using a string key on an array (map-style indexing); computing the index with a float-producing expression; off-by-one producing a negative index from a subtraction.","solutions":["Ensure the index expression evaluates to a non-negative integer","Cast/round float results to integers before indexing","Use map syntax with string keys if you need key-based assignment","Add a guard comparing the computed index against 0 before assignment"],"exampleFix":"// before\nlet i = -1;\narr[i] = 5; // requires non-negative integer index\n// after\nlet i = 0;\nif (i >= 0) { arr[i] = 5; }","handlingStrategy":"validation","validationCode":"// before arr[i] = v, ensure i is a non-negative integer\nassert i >= 0;","typeGuard":"fn is_valid_index(v: &BamlValue) -> bool {\n    matches!(v, BamlValue::Int(i) if *i >= 0)\n}","tryCatchPattern":null,"preventionTips":["Compute indices with integer-only expressions","Clamp or check indices before array writes","Use maps for non-integer keys"],"tags":["baml","interpreter","assignment","index"],"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-23T08:17:48.524Z"}