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
- Align the .baml field type with the actual int value (declare it int)
- Convert the value (e.g. ToString) before distribution if the field is a string
- Fix enum fields to map literals correctly or declare the field as int
- 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
- Ensure enum/number fields declare int when the model emits numbers
- Convert numeric strings to ints before distribution if the field is int
- Keep .baml type definitions and downstream parsing in sync
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
- Could not unify String with {:?}
- Unification error
- type `{ty}` can't be passed through auto-CLI; deliver it via
- baml.json.deserialize failed: {e:?}
- baml.json.serialize returned non-string value: {other:?}
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/3181e3957eeb6809.
Report an issue: GitHub.