BoundaryML/baml · error · anyhow::Error

Could not unify Int with {:?}

Error message

Could not unify Int with {:?}

What it means

Same unification path as error 738 for integers: distribute_type_with_meta fails with 'Could not unify Int with {field_type:?}' when an Int value's literal/primitive type is not a subtype of the expected field type. The schema expected something other than an int at this position.

Source

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

            }
            BamlValueWithMeta::Int(i, meta)
                if self.is_subtype(
                    &TypeIR::Literal(LiteralValue::Int(i), Default::default()),
                    &field_type,
                ) =>
            {
                Ok(BamlValueWithMeta::Int(i, (meta, field_type)))
            }
            BamlValueWithMeta::Int(i, meta)
                if self.is_subtype(
                    &TypeIR::Primitive(TypeValue::Int, Default::default()),
                    &field_type,
                ) =>
            {
                Ok(BamlValueWithMeta::Int(i, (meta, field_type)))
            }
            BamlValueWithMeta::Int(_i, _meta) => {
                anyhow::bail!("Could not unify Int with {:?}", field_type)
            }

            BamlValueWithMeta::Float(f, meta)
                if self.is_subtype(
                    &TypeIR::Primitive(TypeValue::Float, Default::default()),
                    &field_type,
                ) =>
            {
                Ok(BamlValueWithMeta::Float(f, (meta, field_type)))
            }
            BamlValueWithMeta::Float(_, _) => {
                anyhow::bail!("Could not unify Float with {:?}", field_type)
            }

            BamlValueWithMeta::Bool(b, meta) => {
                let literal_type = TypeIR::Literal(LiteralValue::Bool(b), Default::default());
                let primitive_type = TypeIR::Primitive(TypeValue::Bool, Default::default());

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Align the .baml field type with the actual int value (declare it int)
  2. Convert the value (e.g. ToString) before distribution if the field is a string
  3. Fix enum fields to map literals correctly or declare the field as int
  4. Regenerate client code after type changes

Example fix

// before (.baml)
class A { name int }  // value: 5 (int) expected string
// after
class A { name string }
// or: class A { name int }
Defensive patterns

Strategy: try-catch

Validate before calling

fn is_int_compatible(v: i64, t: &TypeIR) -> bool {
  matches!(t, TypeIR::Primitive(TypeValue::Int, _))
    || matches!(t, TypeIR::Literal(LiteralValue::Int(i), _) if *i == v)
}

Type guard

fn as_int_field(v: &BamlValue, t: &TypeIR) -> Option<i64> {
  match v {
    BamlValue::Int(i) => Some(*i),
    _ => None,
  }
}

Try / catch

match distribute_type(&value, &field_type) {
  Ok(v) => v,
  Err(e) if e.to_string().contains("Could not unify Int") => {
    // coerce or report: value is numeric but schema disagrees
    return Err(e.context("schema expects non-int field for numeric value"));
  }
  Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Distributing a BamlValueWithMeta::Int into a field whose TypeIR is not int and not a matching int literal — e.g. an int value assigned to a string, float (if not subtype), or class field.

Common situations: Model returned a number where the schema declares a string or enum; changed a field's type in .baml without updating consumers; enum-typed fields receiving raw ints.

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/3181e3957eeb6809. Report an issue: GitHub.