{"record":{"id":"e4452b09e970a86f","repo":"BoundaryML/baml","slug":"attempting-to-finish-a-call-without-first-starting-one-mod","errorCode":null,"errorMessage":"Attempting to finish a call without first starting one","messagePattern":"Attempting to finish a call without first starting one","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/tracing/mod.rs","lineNumber":637,"sourceCode":"                call.function_type,\n            )\n        };\n\n        BAML_TRACER.lock().unwrap().put(Arc::new(event));\n\n        Ok(call_id)\n    }\n\n    #[cfg(target_arch = \"wasm32\")]\n    pub(crate) async fn finish_baml_call(\n        &self,\n        call: TracingCall,\n        ctx: &RuntimeContextManager,\n        response: &Result<FunctionResult>,\n    ) -> Result<(uuid::Uuid, Vec<baml_ids::FunctionCallId>)> {\n        let guard = self.trace_stats.guard();\n        let Some((call_id, event_chain, tags)) = ctx.exit() else {\n            anyhow::bail!(\"Attempting to finish a call without first starting one\");\n        };\n\n        if call.call_id != call_id {\n            anyhow::bail!(\"Call ID mismatch: {} != {}\", call.call_id, call_id);\n        }\n\n        if let Ok(response) = &response {\n            let name = event_chain.last().map(|s| s.name.as_str());\n            let is_ok = response\n                .result_with_constraints()\n                .as_ref()\n                .is_some_and(|r| r.is_ok());\n            if is_ok {\n                baml_log::info!(\n                    \"{}{}\",\n                    name.map(|s| format!(\"Function {s}:\\n\"))\n                        .unwrap_or_default()\n                        .purple(),","sourceCodeStart":619,"sourceCodeEnd":655,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/tracing/mod.rs#L619-L655","documentation":"In `finish_baml_call` (tracing/mod.rs:637), `ctx.exit()` returned None: no call was started in this context before attempting to finish the BAML function call. The runtime cannot close a trace that was never opened.","triggerScenarios":"Calling `finish_baml_call` when `start_baml_call` (or equivalent) was not run on the same RuntimeContextManager, the context was already exited, or the wrong context instance was passed.","commonSituations":"Custom runtime integrations (Python/Ruby/FFI hosts) that finish the call on an error path before starting it; context created per-request but reused across requests after exit; double-finish.","solutions":["Ensure start_baml_call runs before finish_baml_call on the same context manager.","Guard error paths so finish is only invoked when a start succeeded (track a started flag).","Pass the same RuntimeContextManager instance used at start, not a new one.","Call finish exactly once per started call."],"exampleFix":"// before\nlet result = run_function().await; // may fail before tracing started\nruntime.finish_baml_call(call, &ctx, &result);\n// after\nruntime.start_baml_call(&mut call, &ctx, ...)?;\nlet result = run_function().await;\nruntime.finish_baml_call(call, &ctx, &result);","handlingStrategy":"try-catch","validationCode":"// track started state explicitly\nlet mut started = false;\nruntime.start_baml_call(&mut call, &ctx, ...)?;\nstarted = true;\nif started { runtime.finish_baml_call(call, &ctx, &result)?; }","typeGuard":null,"tryCatchPattern":"if let Err(e) = runtime.finish_baml_call(call, &ctx, &result) {\n    if e.to_string().contains(\"without first starting one\") {\n        log::debug!(\"no active BAML call; nothing to finish\");\n    } else {\n        return Err(e);\n    }\n}","preventionTips":["Guard finish with a started flag set only after a successful start.","Reuse the same RuntimeContextManager from start to finish.","Call finish exactly once per invocation.","Test integration error paths where start may be skipped."],"tags":["tracing","state","lifecycle"],"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-14T11:17:12.474Z"}