{"record":{"id":"9c370bdfe934ef90","repo":"BoundaryML/baml","slug":"property-already-exists-in-class","errorCode":null,"errorMessage":"Property already exists: {} in class {}","messagePattern":"Property already exists: (.+?) in class (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_cffi/src/raw_ptr_wrapper/type_builder/objects.rs","lineNumber":328,"sourceCode":"            .get_meta(\"description\")\n            .and_then(|value| value.as_str().map(|s| s.to_string()))\n            .or_else(ast_description);\n        Ok(result)\n    }\n\n    pub fn add_property(\n        &self,\n        rt: &BamlRuntime,\n        name: &str,\n        field_type: TypeIR,\n    ) -> anyhow::Result<ClassPropertyBuilder> {\n        self.mode.at_least(NodeRW::ReadWrite)?;\n        let cls = self.cls(rt)?;\n\n        // if the IR already has the property, then its not valid to add it again\n        if let Ok(cls) = rt.ir.find_class(self.class_name.as_str()) {\n            if cls.find_field(name).is_some() {\n                anyhow::bail!(\n                    \"Property already exists: {} in class {}\",\n                    name,\n                    self.class_name\n                );\n            }\n        }\n\n        let builder = cls.lock().unwrap();\n        let prop = builder.upsert_property(name);\n        prop.lock().unwrap().set_type(field_type);\n        Ok(self.create_property(name, rt))\n    }\n\n    pub fn property(&self, rt: &BamlRuntime, name: &str) -> anyhow::Result<ClassPropertyBuilder> {\n        self.mode.at_least(NodeRW::ReadOnly)?;\n        let cls = self.cls(rt)?;\n\n        let builder = cls.lock().unwrap();","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_cffi/src/raw_ptr_wrapper/type_builder/objects.rs#L310-L346","documentation":"ClassBuilder::add_property refuses to add a property that already exists in the IR's class definition. BAML requires unique property names within a class, so re-adding an existing field bails instead of overwriting it.","triggerScenarios":"Calling cb.add_property(rt, \"name\", ...) when the class already defines a field 'name' in the BAML source (cls.find_field succeeds).","commonSituations":"Dynamically adding a property that is already declared in the .baml class; re-running non-idempotent builder code that re-adds the same property.","solutions":["Use cb.property(rt, name) to fetch/modify the existing property instead of add_property","Check for existing fields before calling add_property (e.g. via list_properties)","Rename the new property if you truly need an additional field"],"exampleFix":"// before\ncb.add_property(rt, \"name\", tb.string_type())?; // fails if 'name' exists\n// after\nif !cb.list_properties(rt)?.contains_key(\"name\") {\n    cb.add_property(rt, \"name\", tb.string_type())?;\n}","handlingStrategy":"validation","validationCode":"# check before add\nprops = cb.list_properties(rt)\nif \"name\" not in props:\n    cb.add_property(rt, \"name\", tb.string())","typeGuard":null,"tryCatchPattern":"try:\n    cb.add_property(rt, name, ty)\nexcept Exception as e:\n    if \"already exists\" in str(e):\n        p = cb.property(rt, name)\n    else:\n        raise","preventionTips":["List existing properties before dynamically adding fields","Make property registration idempotent (upsert-style get-or-add)","Align dynamic property names with those declared in .baml class definitions"],"tags":["type-builder","duplicate","property","class"],"backgroundTag":"file-already-exists","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}