{"record":{"id":"d9eac88d2e97be61","repo":"BoundaryML/baml","slug":"undefined-variant-enum-name-variant","errorCode":null,"errorMessage":"undefined variant: {enum_name}.{variant}","messagePattern":"undefined variant: (.+?)\\.(.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"baml_language/crates/baml_compiler2_emit/src/emit.rs","lineNumber":3567,"sourceCode":"    fn init_field(&mut self, field_idx: usize, name: &str) -> Result<(), Self::Error> {\n        let idx = self.emit(Instruction::InitField(field_idx));\n        self.set_operand(idx, OperandMeta::Field(name.to_string()));\n        Ok(())\n    }\n\n    fn alloc_enum_variant(&mut self, enum_name: &str, variant: &str) -> Result<(), Self::Error> {\n        let enum_obj_idx = self\n            .enum_object_indices\n            .get(enum_name)\n            .copied()\n            .unwrap_or_else(|| panic!(\"undefined enum: {enum_name}\"));\n\n        let variant_idx = self\n            .enum_variants\n            .get(enum_name)\n            .and_then(|variants| variants.get(variant))\n            .copied()\n            .unwrap_or_else(|| panic!(\"undefined variant: {enum_name}.{variant}\"));\n\n        #[allow(clippy::cast_possible_wrap)]\n        let idx = self.add_constant(ConstValue::Int(variant_idx as i64));\n        let lc_inst = self.emit(Instruction::LoadConst(idx));\n        self.set_operand(\n            lc_inst,\n            OperandMeta::Const(format!(\"{enum_name}.{variant}\")),\n        );\n        let inst = self.emit(Instruction::AllocVariant(ObjectIndex::from_raw(\n            enum_obj_idx,\n        )));\n        self.set_operand(inst, OperandMeta::Object(enum_name.to_string()));\n        Ok(())\n    }\n\n    fn discriminant(&mut self) -> Result<(), Self::Error> {\n        self.emit(Instruction::Discriminant);\n        Ok(())","sourceCodeStart":3549,"sourceCodeEnd":3585,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler2_emit/src/emit.rs#L3549-L3585","documentation":"Panic in alloc_enum_variant: the enum exists in enum_object_indices but the specific variant name is missing from enum_variants[enum_name]. The compiler allocated the enum object yet has no variant index for the requested variant, so the variant load cannot be lowered. This is a name-resolution failure between the referenced variant and the compiler's variant table.","triggerScenarios":"Referencing a variant that does not exist on the enum (typo, removed variant, or variant added in a newer schema version while generated/older code still references the old set).","commonSituations":"Typos in variant names, refactoring enum variants without updating all references, version drift between BAML clients/server schema where the variant list differs.","solutions":["Fix the variant name to match the enum's declared variants.","Regenerate client code after the enum's variant set changes.","Add the missing variant to the enum definition if it was removed by mistake.","Align BAML schema versions across client and server so both sides know the variant."],"exampleFix":"// before: variant that no longer exists\nlet s = Status.Pendng;\n// after: corrected spelling\nlet s = Status.Pending;","handlingStrategy":"validation","validationCode":"// verify the variant exists on the enum before use\nlet variants: Vec<&str> = project.enum_variants(\"Status\");\nassert!(variants.contains(&\"Pending\"), \"Status has no variant Pending\");","typeGuard":"fn variant_exists(project: &Project, enum_name: &str, variant: &str) -> bool {\n    project.enum_variants(enum_name).contains(variant)\n}","tryCatchPattern":"// compiler panic — validate names at generation/lint time\nif !variant_exists(project, \"Status\", \"Pending\") {\n    regenerate_client_code();\n}","preventionTips":["Check enum variant spelling against the schema definition","Regenerate clients whenever enum variants change","Keep BAML schema versions synchronized between client and server","Add a lint that validates all enum variant references at build time"],"tags":["compiler","enum","panic","name-resolution"],"backgroundTag":"invalid-enum-value","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"}