BoundaryML/baml · error

length() method not available on type {:?} at {:?}

Error message

length() method not available on type {:?} at {:?}

What it means

THIR interpreter runtime error: the `length()` method was invoked on a receiver type that has no length — not a list, string, or map (e.g. an int, bool, null, or class instance). The unsupported receiver type is echoed in the message along with the location metadata.

Source

Thrown at engine/baml-compiler/src/thir/interpret.rs:2828

                    }
                    Ok(BamlValueWithMeta::Int(items.len() as i64, meta.clone()))
                }
                BamlValueWithMeta::String(s, _) => {
                    if !args.is_empty() {
                        bail!("length() method takes no arguments at {:?}", meta.0);
                    }
                    Ok(BamlValueWithMeta::Int(
                        s.chars().count() as i64,
                        meta.clone(),
                    ))
                }
                BamlValueWithMeta::Map(map, _) => {
                    if !args.is_empty() {
                        bail!("length() method takes no arguments at {:?}", meta.0);
                    }
                    Ok(BamlValueWithMeta::Int(map.len() as i64, meta.clone()))
                }
                _ => bail!(
                    "length() method not available on type {:?} at {:?}",
                    receiver,
                    meta.0
                ),
            }
        }
        "toLowerCase" => {
            let BamlValueWithMeta::String(s, _) = receiver else {
                bail!(
                    "toLowerCase() method only available on strings at {:?}",
                    meta.0
                );
            };
            if !args.is_empty() {
                bail!("toLowerCase() method takes no arguments at {:?}", meta.0);
            }
            Ok(BamlValueWithMeta::String(s.to_lowercase(), meta.clone()))
        }

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check the receiver's actual runtime type at the reported span
  2. Guard or coerce: only call length() on values known to be string/list/map
  3. If the value may be null or scalar, handle that case before calling length()

Example fix

// before
let n = count.length()
// after
let n = items.length()  // items is a list
Defensive patterns

Strategy: type-guard

Validate before calling

fn supports_length(v: &BamlValueWithMeta<ExprMetadata>) -> bool {
    matches!(v, BamlValueWithMeta::List(_, _) | BamlValueWithMeta::String(_, _) | BamlValueWithMeta::Map(_, _))
}

Type guard

fn supports_length(v: &BamlValueWithMeta<ExprMetadata>) -> bool {
    matches!(v, BamlValueWithMeta::List(_, _) | BamlValueWithMeta::String(_, _) | BamlValueWithMeta::Map(_, _))
}

Prevention

When it happens

Trigger: Calling x.length() where x evaluates to a non-collection/non-string BamlValue, e.g. an integer field or a null.

Common situations: Assuming a value is a list or string when the upstream JSON shape differs (e.g. a single object instead of an array); calling length() on a number.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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