{"record":{"id":"45c11c2bac761a25","repo":"BoundaryML/baml","slug":"break-statement-not-in-loop-context","errorCode":null,"errorMessage":"break statement not in loop context","messagePattern":"break statement not in loop context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1417,"sourceCode":"    function_name: &str,\n) -> Result<BamlValueWithMeta<ExprMetadata>>\nwhere\n    F: LlmHandler<Fut>,\n    Fut: LlmFuture,\n{\n    match evaluate_block_with_control_flow(\n        block,\n        scopes,\n        thir,\n        run_llm_function,\n        watch_handler,\n        function_name,\n    )\n    .await?\n    {\n        ControlFlow::Normal(val) => Ok(val),\n        ControlFlow::Return(val) => Ok(val),\n        ControlFlow::Break => bail!(\"break statement not in loop context\"),\n        ControlFlow::Continue => bail!(\"continue statement not in loop context\"),\n    }\n}\n\nfn declare(scopes: &mut [Scope], name: &str, value: BamlValueWithMeta<ExprMetadata>) {\n    if let Some(scope) = scopes.last_mut() {\n        scope\n            .variables\n            .insert(name.to_string(), Arc::new(Mutex::new(value)));\n    }\n}\n\nfn declare_with_cell(\n    scopes: &mut [Scope],\n    name: &str,\n    cell: Arc<Mutex<BamlValueWithMeta<ExprMetadata>>>,\n) {\n    if let Some(scope) = scopes.last_mut() {","sourceCodeStart":1399,"sourceCodeEnd":1435,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1399-L1435","documentation":"Raised by evaluate_block, the wrapper that evaluates a BAML block where break/continue are not meaningful. If the block (or a nested block) executes a `break` and the ControlFlow::Break value propagates all the way out to a context that is not a loop (e.g. a filter/lambda function body or a plain expression evaluation), the interpreter rejects it with this error.","triggerScenarios":"Using `break` inside a BAML block that is not a loop body — e.g. inside a filter/lambda function, an if-else block at the top level of an expression, or a function body — where no loop is on the control-flow stack.","commonSituations":"Copy-pasting loop code containing `break` into a map/filter callback or an if branch; thinking `break` exits an if block; BAML version where break support exists only in for/while loops.","solutions":["Remove the `break` or ensure it is placed directly inside a for/while loop body.","If inside a lambda/filter function, use `return` (or the block's trailing expression) instead of `break` to stop processing.","Restructure the logic with an if condition around the loop-body work instead of breaking out of a non-loop block.","Upgrade BAML if you expect break to be valid in this context (it may be a scoping bug worth reporting)."],"exampleFix":"// before (BAML)\nlet evens = nums.filter(|n| { if (n % 2 != 0) { break; }; n * 2 });\n// after\nlet evens = nums.filter(|n| n % 2 == 0).map(|n| n * 2);","handlingStrategy":"validation","validationCode":"// Static guard: `break` must appear inside a loop body in your BAML sources.\n// In a filter/lambda callback, reject any break/continue entirely:\nif (/\\bbreak\\b/.test(lambdaBodySrc)) {\n  throw new Error(\"break is not allowed in non-loop blocks; use the predicate or return instead\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await bamlFn(ctx, args);\n} catch (e) {\n  if (String(e).includes(\"break statement not in loop context\")) {\n    throw new Error(\"BAML source error: move `break` inside a for loop body\");\n  }\n  throw e;\n}","preventionTips":["Use `break` only directly inside for/while loop bodies.","In map/filter callbacks, filter first or use the predicate — never break.","Lint BAML sources for break/continue outside loop bodies.","Remember `break` exits loops, not if-blocks or function bodies."],"tags":["baml","control-flow","break","loops"],"backgroundTag":"invalid-state-transition","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"}