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
- Prefix the function name with `--function`
- Put any positional/target arguments after `--`
- 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
- Never put bare positional tokens before `--`
- Prefix function names with `--function`
- Lint script bodies against the documented grammar
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- are mutually exclusive dispatch modes — pick one.
- script body has `--function` without a value
- unknown run-verb flag
- {bail_context}
- Cannot generate HIR/bytecode due to validation 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 moreView on GitHub (pinned to bd85ce9dee)