BoundaryML/baml · error

trim() method only available on strings at

Error message

trim() method only available on strings at {:?}

What it means

The interpreter requires that trim() be called only on string values; a non-string receiver triggers this bail. trim() removes leading and trailing whitespace and returns a new string, so the operation is undefined for other BAML types.

Solutions

  1. Coerce the value to a string first (string interpolation or an explicit string conversion) before calling trim().
  2. Fix the declaration/producer of the value so it is actually a string.
  3. Guard with a type/null check before invoking trim() when the value is optional.
  4. Use the reported span in the error to find the offending receiver expression.

Example fix

// before
let clean = record.id.trim();
// after
let clean = "{{ record.id }}".trim();  // or make id a string field
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value !== 'string') {
  throw new Error('trim() receiver must be a string, got: ' + typeof value);
}

Type guard

const isStr = (v) => typeof v === 'string';

Try / catch

try {
  const res = await b.Function(args);
} catch (e) {
  if (String(e.message).includes('trim() method only available on strings')) {
    // stringify the offending field and retry
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `x.trim()` where `x` evaluates to a non-string at runtime — an int, float, bool, null, map, array, or an enum/media value — in a BAML expression.

Common situations: Trimming user input fetched from a map/JSON payload that arrives as a number; applying trim to the result of a function whose return type changed to non-string; forgetting that null values are not strings in BAML.

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

Appendix: source

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

                bail!("toLowerCase() method takes no arguments at {:?}", meta.0);
            }
            Ok(BamlValueWithMeta::String(s.to_lowercase(), meta.clone()))
        }
        "toUpperCase" => {
            let BamlValueWithMeta::String(s, _) = receiver else {
                bail!(
                    "toUpperCase() method only available on strings at {:?}",
                    meta.0
                );
            };
            if !args.is_empty() {
                bail!("toUpperCase() method takes no arguments at {:?}", meta.0);
            }
            Ok(BamlValueWithMeta::String(s.to_uppercase(), meta.clone()))
        }
        "trim" => {
            let BamlValueWithMeta::String(s, _) = receiver else {
                bail!("trim() method only available on strings at {:?}", meta.0);
            };
            if !args.is_empty() {
                bail!("trim() method takes no arguments at {:?}", meta.0);
            }
            Ok(BamlValueWithMeta::String(
                s.trim().to_string(),
                meta.clone(),
            ))
        }
        "includes" => {
            let BamlValueWithMeta::String(s, _) = receiver else {
                bail!(
                    "includes() method only available on strings at {:?}",
                    meta.0
                );
            };
            if args.len() != 1 {
                bail!("includes() method takes exactly 1 argument at {:?}", meta.0);

View on GitHub (pinned to bd85ce9dee)