BoundaryML/baml · error

substring() method takes exactly 2 arguments at

Error message

substring() method takes exactly 2 arguments at {:?}

What it means

The substring method has a fixed arity of two arguments (start and end indices). If a call passes any other number of arguments, the interpreter bails with this message. The library enforces the exact signature substring(start, end).

Solutions

  1. Supply both arguments: s.substring(start, end).
  2. To slice to the end of the string, pass the string's length (or a large number — end is clamped with .min(s.len())) as the second argument.
  3. Remove any extra arguments at the failing call site.

Example fix

// before
let tail = s.substring(2);

// after
let tail = s.substring(2, s.len());
Defensive patterns

Strategy: validation

Validate before calling

// substring takes exactly 2 int arguments (start, end)
let args_ok = args.len() == 2;

Try / catch

match result {
  Err(e) if e.to_string().contains("takes exactly 2 arguments") => log_missing_arg("substring"),
  other => other,
}

Prevention

When it happens

Trigger: Calling s.substring(0) with one argument (expecting a Python-like end-omitted slice), or s.substring(0, 5, 2) with three arguments, in BAML expression code.

Common situations: Porting Python/JS slicing habits (`s[0:]` or `substring(0)`) into BAML; passing an optional step argument; forgetting the end index.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

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

            }
            let BamlValueWithMeta::String(delimiter, _) = &args[0] else {
                bail!("split() argument must be a string at {:?}", meta.0);
            };
            let parts: Vec<BamlValueWithMeta<ExprMetadata>> = s
                .split(delimiter.as_str())
                .map(|part| BamlValueWithMeta::String(part.to_string(), meta.clone()))
                .collect();
            Ok(BamlValueWithMeta::List(parts, meta.clone()))
        }
        "substring" => {
            let BamlValueWithMeta::String(s, _) = receiver else {
                bail!(
                    "substring() method only available on strings at {:?}",
                    meta.0
                );
            };
            if args.len() != 2 {
                bail!(
                    "substring() method takes exactly 2 arguments at {:?}",
                    meta.0
                );
            }
            let BamlValueWithMeta::Int(start, _) = &args[0] else {
                bail!("substring() start argument must be an int at {:?}", meta.0);
            };
            let BamlValueWithMeta::Int(end, _) = &args[1] else {
                bail!("substring() end argument must be an int at {:?}", meta.0);
            };

            let start = (*start as usize).min(s.len());
            let end = (*end as usize).min(s.len()).max(start);

            Ok(BamlValueWithMeta::String(
                s[start..end].to_string(),
                meta.clone(),
            ))

View on GitHub (pinned to bd85ce9dee)