{"record":{"id":"abe99f56ce39bf6a","repo":"BoundaryML/baml","slug":"could-not-unify-class-with","errorCode":null,"errorMessage":"Could not unify Class {} with {:?}","messagePattern":"Could not unify Class (.+?) with (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs","lineNumber":353,"sourceCode":"\n            BamlValueWithMeta::Enum(name, val, meta) => {\n                if self.is_subtype(\n                    &TypeIR::Enum {\n                        name: name.clone(),\n                        dynamic: false,\n                        meta: Default::default(),\n                    },\n                    &field_type,\n                ) {\n                    Ok(BamlValueWithMeta::Enum(name, val, (meta, field_type)))\n                } else {\n                    anyhow::bail!(\"Could not unify Enum {} with {:?}\", name, field_type)\n                }\n            }\n\n            BamlValueWithMeta::Class(name, fields, meta) => {\n                if !self.is_subtype(&TypeIR::class(name.as_str()), &field_type) {\n                    anyhow::bail!(\"Could not unify Class {} with {:?}\", name, field_type);\n                } else {\n                    let class_fields = self.class_fields(&name)?;\n                    let mapped_fields = fields\n                        .into_iter()\n                        .map(|(k, v)| {\n                            let field_type = match class_fields.get(k.as_str()) {\n                                Some(ft) => ft.clone(),\n                                None => infer_type_with_meta(&v).unwrap_or(UNIT_TYPE.clone()),\n                            };\n                            let mapped_field = self.distribute_type_with_meta(v, field_type)?;\n                            Ok((k, mapped_field))\n                        })\n                        .collect::<anyhow::Result<BamlMap<String, BamlValueWithMeta<(T, TypeIR)>>>>(\n                        )?;\n                    Ok(BamlValueWithMeta::Class(\n                        name,\n                        mapped_fields,\n                        (meta, field_type),","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs#L335-L371","documentation":"A Class value's type could not be unified with the expected TypeIR: `is_subtype(class_type, field_type)` returned false in distribute_type_with_meta, so the pass refuses to attach the type. The expected type must be the same class (or a supertype/alias/union that admits it).","triggerScenarios":"distribute_type over BamlValueWithMeta::Class(name, ..) when field_type is a different class, a primitive, a map, or a union excluding this class — usually during argument checking against .baml function signatures.","commonSituations":"Passing an instance of class A where class B is declared after a refactor; sending a raw object/dict where a class parameter is declared (or the reverse); unions that don't include the concrete class being sent.","solutions":["Declare the parameter as the class named in the error, or add it to the union: `ClassA | ClassB`","Verify the caller constructs the correct class type (matching field names/types) as declared in .baml","If structural typing was expected, ensure the class actually is a subtype in the IR (same name and compatible fields)","Regenerate client types after .baml changes so stale SDK classes are not sent"],"exampleFix":"// before (.baml)\nclass Address { city string }\nfunction Ship(dest: Person) -> bool { ... }\n// caller sends an Address\n\n// after (.baml)\nfunction Ship(dest: Address) -> bool { ... }","handlingStrategy":"validation","validationCode":"function validateClassArg(className: string, declaredType: string): boolean {\n  return declaredType === className || declaredType.split('|').map(s => s.trim()).includes(className);\n}","typeGuard":"const isInstanceOf = <T extends object>(v: unknown, ctor: new (...a: never[]) => T): v is T => v instanceof ctor;","tryCatchPattern":null,"preventionTips":["Construct arguments with generated client class types","Regenerate SDKs after class renames/refactors","Ensure unions in .baml include every concrete class you may send","Add integration tests that call each function with realistic arguments"],"tags":["rust","type-system","baml","class"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}