{"record":{"id":"4fa6502fdb3dfcc6","repo":"BoundaryML/baml","slug":"undefined-function-baml-deep-copy","errorCode":null,"errorMessage":"undefined function: baml.deep_copy","messagePattern":"undefined function: baml\\.deep_copy","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"baml_language/crates/baml_compiler2_emit/src/emit.rs","lineNumber":3489,"sourceCode":"\n    fn alloc_uint8array(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {\n        use std::fmt::Write;\n        // Store the byte data as a compile-time constant template, then deep-copy\n        // it to produce a mutable TLAB allocation (matching array literal semantics).\n        let mut display = String::from(\"b\\\"\");\n        for b in bytes {\n            write!(display, \"\\\\x{b:02x}\").unwrap();\n        }\n        display.push('\"');\n        let obj_idx = self.mint_object(Object::Uint8Array(bytes.to_vec().into()));\n        let idx = self.add_constant(ConstValue::Object(ObjectIndex::from_raw(obj_idx)));\n        let inst = self.emit(Instruction::LoadConst(idx));\n        self.set_operand(inst, OperandMeta::Const(display));\n        let deep_copy_idx = self\n            .globals\n            .get(\"baml.deep_copy\")\n            .copied()\n            .unwrap_or_else(|| panic!(\"undefined function: baml.deep_copy\"));\n        let inst = self.emit(Instruction::Call {\n            callee: GlobalIndex::from_raw(deep_copy_idx),\n            ntypeargs: 0,\n        });\n        self.set_operand(inst, OperandMeta::Callable(\"baml.deep_copy\".to_string()));\n        Ok(())\n    }\n\n    fn alloc_map(\n        &mut self,\n        key_ty: &TyTemplate,\n        value_ty: &TyTemplate,\n        len: usize,\n    ) -> Result<(), Self::Error> {\n        // Push key then value type on top of the entries; the VM's `AllocMap`\n        // pops value then key before processing the pairs.\n        self.load_type(key_ty)?;\n        self.load_type(value_ty)?;","sourceCodeStart":3471,"sourceCodeEnd":3507,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler2_emit/src/emit.rs#L3471-L3507","documentation":"Panic when emitting a deep-copy operation: the emitter unconditionally looks up the builtin global \"baml.deep_copy\" in the globals map and panics if it is absent. The compiler assumes the runtime pre-registers this builtin; if the builtin was never registered (or renamed), deep-copy call sites cannot be lowered.","triggerScenarios":"Compiling code that performs a deep copy (e.g. deep-cloning a class/dynamic value) while the globals table lacks the \"baml.deep_copy\" builtin — typically because the runtime prelude wasn't loaded or the builtin was renamed across versions.","commonSituations":"Version mismatch between the compiler and runtime prelude, embedding the BAML runtime without registering builtins, or a renamed builtin after an upgrade.","solutions":["Ensure the runtime prelude (which registers baml.deep_copy) is loaded before compiling/running.","Upgrade or align compiler and runtime versions so the builtin name matches.","Check for typos/renames of the builtin in a custom prelude.","Report the missing-builtin scenario to the BAML maintainers if the prelude is stock."],"exampleFix":"// before: embedding the VM without the builtin prelude\nlet mut vm = Vm::new();\n// after: register builtins first\nlet mut vm = Vm::new();\nvm.register_builtins(); // includes baml.deep_copy","handlingStrategy":"validation","validationCode":"// check the builtin is registered before compiling deep-copy sites\nif vm.globals().get(\"baml.deep_copy\").is_none() {\n    vm.register_builtins();\n}","typeGuard":"fn has_deep_copy_builtin(globals: &HashMap<String, usize>) -> bool {\n    globals.contains_key(\"baml.deep_copy\")\n}","tryCatchPattern":"// panics abort the process; validate environment beforehand\nassert!(globals.contains_key(\"baml.deep_copy\"), \"runtime prelude not loaded\");","preventionTips":["Always initialize the runtime with the standard prelude/builtins","Keep baml-cli and the embedded runtime on matching versions","Grep for builtin renames when upgrading across versions","Smoke-test deep-copy code paths after any runtime embedding change"],"tags":["compiler","builtins","panic","prelude"],"backgroundTag":"internal-invariant-violation","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"}