BoundaryML/baml · error · anyhow::Error

Could not unify Float with {:?}

Error message

Could not unify Float with {:?}

What it means

In BAML's IR type-distribution pass, a Float value could not be matched against the declared field type: `is_subtype(float, field_type)` failed, so `distribute_type_with_meta` bails instead of annotating the value with the type. This happens when a runtime float is checked against a type that is not `float` (nor an alias/union member resolving to float).

Source

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

                    &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());

                if self.is_subtype(&literal_type, &field_type)
                    || self.is_subtype(&primitive_type, &field_type)
                {
                    Ok(BamlValueWithMeta::Bool(b, (meta, field_type)))
                } else {
                    anyhow::bail!("Could not unify Bool with {:?}", field_type)
                }
            }

            BamlValueWithMeta::Null(meta) => Ok(BamlValueWithMeta::Null((meta, field_type))),

            BamlValueWithMeta::Map(pairs, meta) => {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Change the BAML function/field type to `float` (or `int | float`) so the value's type is accepted
  2. Coerce the value to the expected type on the caller side (e.g. round/cast 1.5 to int before calling the function)
  3. If the value should accept both, make the field optional or a union like `float | int` in the .baml schema
  4. Check which parameter/field the error mentions and fix the client's argument payload to match the .baml definition

Example fix

// before (.baml)
function GetScore(rating: int) -> float { ... }
// caller sends rating: 4.5

// after (.baml)
function GetScore(rating: float) -> float { ... }
Defensive patterns

Strategy: validation

Validate before calling

function validateFloatMatchesSchema(value: number, declaredType: string): boolean {
  return declaredType === 'float' || declaredType === 'float | int' || declaredType === 'int | float';
}
// call before invoking: if (!validateFloatMatchesSchema(v, paramType)) throw new Error('fix .baml type or coerce value');

Type guard

const isFloat = (v: unknown): v is number => typeof v === 'number' && !Number.isInteger(v);

Prevention

When it happens

Trigger: Calling IRHelper::distribute_type (directly or via distribute_type_with_meta) with a BamlValueWithMeta::Float while the expected TypeIR is e.g. `int`, `string`, a class, or an unrelated enum/alias; typically during function-argument checking or testfile value typing where the declared parameter type doesn't accept floats.

Common situations: Declaring a BAML function parameter as `int` or `string` but the client sends 1.5; a type alias changed from float to int; JSON payloads (always float-like for decimals) being validated against int-typed fields.

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/46fa5527770a7690. Report an issue: GitHub.