{"record":{"id":"0f8de47639314e88","repo":"BoundaryML/baml","slug":"index-not-int-at","errorCode":null,"errorMessage":"index not int at {:?}","messagePattern":"index not int at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2029,"sourceCode":"                )?;\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                    )\n                    .await?,\n                )?;\n                match b.clone() {\n                    BamlValueWithMeta::Map(m, _) => {","sourceCodeStart":2011,"sourceCodeEnd":2047,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2011-L2047","documentation":"When indexing a list, the interpreter requires the index expression to evaluate to BamlValueWithMeta::Int. A non-integer index (string, float, bool) throws \"index not int at {:?}\". List indices must be integer expressions.","triggerScenarios":"`list[idx]` where idx evaluates to a string (e.g. from user/LLM output), a float, or any non-Int value.","commonSituations":"Passing a string index that came from parsed text or an LLM response without converting it; using a float computed by division; using map-style string keys on a list.","solutions":["Convert the index to an integer before indexing (parse the string, truncate/round the float)","If using string keys, use a map instead of a list","Trace where the index value originates (likely an untyped LLM/string output) and add an explicit int conversion","Validate the index is an integer at authoring time"],"exampleFix":"// before\nlet item = items[idx_str] // idx_str is \"2\"\n// after\nlet item = items[idx_str.parse_int()]","handlingStrategy":"validation","validationCode":"// Coerce/validate the index to an integer before list access\nfunction toIntIndex(v: unknown): number {\n  const n = typeof v === \"string\" ? parseInt(v, 10) : v;\n  if (typeof n !== \"number\" || !Number.isInteger(n)) throw new Error(\"List index must be an int\");\n  return n;\n}","typeGuard":"function isIntIndex(v: unknown): v is number {\n  return typeof v === \"number\" && Number.isInteger(v);\n}","tryCatchPattern":"try {\n  return evalBaml(indexExpr);\n} catch (e) {\n  if (String(e).includes(\"index not int\")) {\n    throw new Error(\"List index is not an integer — parse/convert it before indexing\");\n  }\n  throw e;\n}","preventionTips":["Convert string values from LLM output to ints before using them as indices","Avoid float arithmetic for index computations; round/truncate explicitly","Use maps with string keys instead of lists when keys are not numeric","Type-annotate index expressions so the checker catches non-int indices"],"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"}