BoundaryML/baml · error · anyhow::Error

Could not unify Enum {} with {:?}

Error message

Could not unify Enum {} with {:?}

What it means

An Enum value's variant name could not be unified with the expected TypeIR: `is_subtype(enum_type, field_type)` failed in distribute_type_with_meta. The declared type must be the same enum (or an alias/union containing it), otherwise the value cannot be typed and the pass bails.

Source

Thrown at engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs:347

            {
                Ok(BamlValueWithMeta::Media(m, (meta, field_type)))
            }
            BamlValueWithMeta::Media(_, _) => {
                anyhow::bail!("Could not unify Media with {:?}", field_type)
            }

            BamlValueWithMeta::Enum(name, val, meta) => {
                if self.is_subtype(
                    &TypeIR::Enum {
                        name: name.clone(),
                        dynamic: false,
                        meta: Default::default(),
                    },
                    &field_type,
                ) {
                    Ok(BamlValueWithMeta::Enum(name, val, (meta, field_type)))
                } else {
                    anyhow::bail!("Could not unify Enum {} with {:?}", name, field_type)
                }
            }

            BamlValueWithMeta::Class(name, fields, meta) => {
                if !self.is_subtype(&TypeIR::class(name.as_str()), &field_type) {
                    anyhow::bail!("Could not unify Class {} with {:?}", name, field_type);
                } else {
                    let class_fields = self.class_fields(&name)?;
                    let mapped_fields = fields
                        .into_iter()
                        .map(|(k, v)| {
                            let field_type = match class_fields.get(k.as_str()) {
                                Some(ft) => ft.clone(),
                                None => infer_type_with_meta(&v).unwrap_or(UNIT_TYPE.clone()),
                            };
                            let mapped_field = self.distribute_type_with_meta(v, field_type)?;
                            Ok((k, mapped_field))
                        })

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Type the parameter as the correct enum in the .baml file (the one whose name appears in the error)
  2. If multiple enums are valid, declare a union `EnumA | EnumB`
  3. Convert the enum value to a string on the caller side if the schema expects string
  4. Check enum name spelling/case in both the .baml schema and the caller payload

Example fix

// before (.baml)
function Pick(choice: Color) -> string { ... }
// caller sends an Animal enum value

// after (.baml)
function Pick(choice: Color | Animal) -> string { ... }
Defensive patterns

Strategy: validation

Validate before calling

function validateEnumArg(enumName: string, declaredType: string): boolean {
  return declaredType === enumName || declaredType.split('|').map(s => s.trim()).includes(enumName);
}

Type guard

const isEnumValue = <T extends Record<string, string>>(e: T, v: string): v is T[keyof T] =>
  Object.values(e).includes(v);

Prevention

When it happens

Trigger: distribute_type over BamlValueWithMeta::Enum(name, ..) where field_type is a different enum, a string, or a union that excludes this enum — typically while validating function arguments against the .baml schema.

Common situations: Two enums with similar names being mixed up after a rename; sending an enum value where a string-literal union is expected; dynamic enums disabled while the value came from dynamic input.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/4514f513e47b9fa3. Report an issue: GitHub.