{"record":{"id":"4bb833b687e9d4be","repo":"BoundaryML/baml","slug":"array-access-on-non-list-at","errorCode":null,"errorMessage":"array access on non-list at {:?}","messagePattern":"array access on non-list at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2025,"sourceCode":"                        watch_handler,\n                        function_name,\n                    )\n                    .await?,\n                )?;\n                let i = 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                let arr = match b.clone() {\n                    BamlValueWithMeta::List(v, _) => v,\n                    _ => bail!(\"array access on non-list at {:?}\", meta),\n                };\n                let idx = match i {\n                    BamlValueWithMeta::Int(ii, _) => ii as usize,\n                    _ => bail!(\"index not int at {:?}\", meta),\n                };\n                let v = arr.get(idx).cloned().context(\"index out of bounds\")?;\n                EvalValue::Value(v.clone())\n            }\n            Expr::FieldAccess { base, field, meta } => {\n                let b = expect_value(\n                    evaluate_expr(\n                        base,\n                        scopes,\n                        thir,\n                        run_llm_function,\n                        watch_handler,\n                        function_name,\n                    )","sourceCodeStart":2007,"sourceCodeEnd":2043,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2007-L2043","documentation":"For an index expression `expr[i]`, the interpreter requires the base to evaluate to a BamlValueWithMeta::List. Anything else (map, class, string, scalar) throws \"array access on non-list at {:?}\". Bracket indexing in BAML only works on lists.","triggerScenarios":"Using `value[index]` where value evaluates to a map, class instance, or scalar rather than a list, during array-access handling in evaluate_expr_with_context.","commonSituations":"Indexing into a map with bracket syntax instead of key access (`m[\"key\"]` vs field access); assuming a string is indexable; a variable that was expected to be a list but is null or a single value after an LLM/parse step.","solutions":["Confirm the base expression actually produces a list (e.g. from a map or split call)","For maps/classes use key/field access (`m.key` or `m[\"key\"]` semantics supported by your construct) instead of integer indexing","If the value may be a single item, wrap it in a list before indexing or handle both cases","Check upstream parsing/LLM output shape — the value may have arrived as a non-list"],"exampleFix":"// before\nlet first = my_map[0] // array access on non-list\n// after\nlet first = my_map.first_key // or use a list: let items = [1,2]; items[0]","handlingStrategy":"type-guard","validationCode":"// Confirm the value is an array before indexing\nfunction assertList(v: unknown): unknown[] {\n  if (!Array.isArray(v)) throw new Error(\"Expected a list before [index] access\");\n  return v;\n}","typeGuard":"function isList<T>(v: unknown): v is T[] {\n  return Array.isArray(v);\n}","tryCatchPattern":"try {\n  return evalBaml(indexExpr);\n} catch (e) {\n  if (String(e).includes(\"array access on non-list\")) {\n    throw new Error(\"Use field/key access for maps and classes; [i] works only on lists\");\n  }\n  throw e;\n}","preventionTips":["Know the shape of values returned by LLM outputs/parsers before indexing","Use dot/field access for maps and classes, brackets only for lists","Wrap possibly-single values into arrays before indexing","Add schema validation on parsed outputs to guarantee list types"],"tags":["interpreter","indexing","type-mismatch"],"backgroundTag":"type-mismatch","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"}