{"record":{"id":"94db035b77bbf6cc","repo":"BoundaryML/baml","slug":"for-loop-requires-iterable-list","errorCode":null,"errorMessage":"for loop requires iterable (list)","messagePattern":"for loop requires iterable \\(list\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":825,"sourceCode":"                                            scopes.pop();\n                                            return Ok(Some(ControlFlow::Break));\n                                        }\n                                        ControlFlow::Continue => {\n                                            scopes.pop();\n                                            continue;\n                                        }\n                                        ControlFlow::Normal(_) => {\n                                            scopes.pop();\n                                        }\n                                        ControlFlow::Return(val) => {\n                                            scopes.pop();\n                                            scopes.pop();\n                                            return Ok(Some(ControlFlow::Return(val)));\n                                        }\n                                    }\n                                }\n                            }\n                            _ => bail!(\"for loop requires iterable (list)\"),\n                        }\n                    }\n                    Statement::AssignOp {\n                        left,\n                        value,\n                        assign_op,\n                        ..\n                    } => {\n                        use crate::hir::AssignOp;\n\n                        let current_val = expect_value(\n                            evaluate_expr(\n                                left,\n                                scopes,\n                                thir,\n                                run_llm_function,\n                                watch_handler,\n                                function_name,","sourceCodeStart":807,"sourceCodeEnd":843,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L807-L843","documentation":"The BAML interpreter's for-loop handler only supports iterating over a list value. When the loop expression evaluates to any other type (int, string, map, null, etc.), handle_statement bails with this error. It is a runtime type restriction of the for-in construct in the interpreted language.","triggerScenarios":"Executing `for x in <expr> { ... }` where <expr> evaluates to a non-list BamlValue (e.g. an int, string, map, or null) at runtime.","commonSituations":"Iterating over a map instead of map.keys()/values, forgetting a function returns an optional/null, or assuming a string is iterable character-by-character.","solutions":["Ensure the loop expression evaluates to a list (e.g. wrap a single value in [value]).","If iterating a map, iterate over Map.keys() or entries instead.","Check for null/optional returns from functions called in the loop expression and handle them before looping."],"exampleFix":"// before\nfor k in myMap {\n  ...\n}\n// after\nfor k in myMap.keys() {\n  ...\n}","handlingStrategy":"type-guard","validationCode":"if !isList(loopExpr) { throw new Error(\"for loop expects a list, got: \" + typeName(loopExpr)); }","typeGuard":"function isList(v) { return Array.isArray(v); }","tryCatchPattern":"try { evaluateBlock(block, scopes); } catch (e) { if (e.message.includes('for loop requires iterable')) { /* handle non-iterable loop target */ } else { throw e; } }","preventionTips":["Only use for-in over expressions statically typed as lists.","Unwrap optional values before looping.","Iterate map keys/values explicitly rather than the map itself."],"tags":["runtime","type-error","for-loop","interpreter"],"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"}