{"record":{"id":"b253a89f25b4d584","repo":"BoundaryML/baml","slug":"continue-statement-not-in-loop-context","errorCode":null,"errorMessage":"continue statement not in loop context","messagePattern":"continue statement not in loop context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1418,"sourceCode":") -> 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() {\n        scope.variables.insert(name.to_string(), cell);","sourceCodeStart":1400,"sourceCodeEnd":1436,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1400-L1436","documentation":"Raised by evaluate_block when a `continue` statement's ControlFlow::Continue signal escapes to a non-loop context. `continue` is only valid inside the body of a for/while loop in BAML; if it executes in a block with no enclosing loop (such as a filter function body or a plain expression block), the interpreter bails with this message.","triggerScenarios":"Placing `continue` inside a BAML block that is not a loop body — e.g. a lambda/filter function, an if-else branch outside any loop, or a function body — so the Continue control-flow flag propagates uncaught to evaluate_block.","commonSituations":"Using `continue` to skip elements inside a map/filter callback (wrong tool — use the predicate or an if inside the loop body instead); copy-pasted loop code moved out of its loop; misunderstanding that `continue` works in any block.","solutions":["Ensure `continue` appears only inside a for/while loop body; remove it elsewhere.","In filter/map callbacks, express skipping via the predicate or an if condition rather than continue.","Inside a loop body but hitting this anyway: check that the continue isn't inside a nested lambda whose body has no loop.","Upgrade BAML if you believe continue is valid in this context — this may be a scoping bug worth reporting."],"exampleFix":"// before (BAML)\nfor (let n in nums) { if (n % 2 != 0) { continue; } }\n// (valid in a loop; invalid in a filter lambda)\nlet evens = nums.filter(|n| n % 2 == 0);","handlingStrategy":"validation","validationCode":"// Static guard: `continue` must appear inside a loop body; never in lambda bodies.\nif (/\\bcontinue\\b/.test(lambdaBodySrc)) {\n  throw new Error(\"continue is not allowed in non-loop blocks; filter first or use an if inside the loop\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await bamlFn(ctx, args);\n} catch (e) {\n  if (String(e).includes(\"continue statement not in loop context\")) {\n    throw new Error(\"BAML source error: move `continue` inside a for loop body\");\n  }\n  throw e;\n}","preventionTips":["Use `continue` only directly inside for/while loop bodies.","To skip items in map/filter, use the callback predicate or an if in the loop body.","Lint BAML sources for continue outside loop contexts.","Watch for continue inside nested lambdas — the innermost block has no loop."],"tags":["baml","control-flow","continue","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"}