BoundaryML/baml · error

unexpected token ` ` in script body. Script bodies have the…

Error message

unexpected token `{token}` in script body. Script bodies have the shape `[--function <name>] [-- <target-args>...]`.

What it means

Parse guard for script bodies in baml.toml [scripts]: a bare token appeared where only `--function <name>` or the `--` separator is allowed. This usually means target arguments were written without the required `--` separator in front of them.

Solutions

  1. Prefix the function name with `--function`
  2. Put any positional/target arguments after `--`
  3. Remove stray tokens that don't fit the `[--function <name>] [-- <args>...]` shape

Example fix

// before
["MyFunction", "arg"]
// after
["--function", "MyFunction", "--", "arg"]
Defensive patterns

Strategy: validation

Validate before calling

function validateBodyShape(tokens) {
  const sep = tokens.indexOf('--');
  const head = sep === -1 ? tokens : tokens.slice(0, sep);
  const ok = head.length === 0 || (head[0] === '--function' && head.length <= 2);
  if (!ok) throw new Error('body must be [--function <name>] [-- <target-args>...]');
}

Try / catch

try { await baml.run(tokens); } catch (e) { if (String(e).includes('unexpected token')) { console.error('Correct shape: [--function <name>] [-- <target-args>...]'); } else throw e; }

Prevention

When it happens

Trigger: `parse_script_body` hits a token that is neither `--function`, nor a `--`-prefixed flag, nor the `--` separator — e.g. `["MyFunc"]` or `["--function", "F", "extra"]`.

Common situations: Omitting `--function` before the function name; forgetting `--` before target args; positional arguments accidentally left in the body.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at baml_language/crates/baml_cli/src/run_command.rs:123

            i += 1;
            if i < tokens.len() {
                function = Some(tokens[i].clone());
                i += 1;
            } else {
                anyhow::bail!("script body has `--function` without a value");
            }
            continue;
        }

        if let Some(stripped) = token.strip_prefix("--") {
            anyhow::bail!(
                "unknown run-verb flag `--{stripped}` in script body. \
                 Only `--function <name>` is recognized before `--`; \
                 put target arguments after `--`."
            );
        }

        anyhow::bail!(
            "unexpected token `{token}` in script body. \
             Script bodies have the shape `[--function <name>] [-- <target-args>...]`."
        );
    }

    Ok(ScriptExpansion {
        function,
        extra_args,
    })
}

// ============================================================================
// CLI Args
// ============================================================================

/// Run a BAML function, script, or expression.
///
/// Choose one execution mode: a positional function or script, one or more

View on GitHub (pinned to bd85ce9dee)