BoundaryML/baml · error

Type error

Error message

Type error: {}

What it means

The BAML REPL type-checks each typed expression before evaluation; if the compiler's type diagnostics contain errors, evaluation is aborted and all type-error messages are joined into one anyhow error. This guarantees expressions only run once they pass type inference.

Solutions

  1. Fix the reported type error in the expression (read the joined messages after 'Type error:')
  2. Run :type <expression> first to check the inferred type before evaluating
  3. Use explicit casts or correct literal types in the expression
  4. Re-check variable types with :env after reloading BAML sources

Example fix

// before (REPL input)
MyFn("hello")  // where MyFn expects int
// after
MyFn(42)
Defensive patterns

Strategy: validation

Validate before calling

// Before evaluating, type-check the expression at the REPL
:type MyFn(arg1, arg2)
// In embedding code: match on the Result and inspect the anyhow message
if let Err(e) = repl.parse_and_evaluate_baml_expression(expr) {
    if e.to_string().starts_with("Type error:") { /* show diagnostics to user */ }
}

Try / catch

match result { Err(e) if e.to_string().starts_with("Type error:") => println!("Fix types: {e}"), Ok(v) => println!("{v:?}"), Err(e) => eprintln!("{e:#}") }

Prevention

When it happens

Trigger: Calling parse_and_evaluate_baml_expression_with_status (directly or via evaluate_simple_expression_with_status / parse_and_evaluate_baml_expression) with an expression whose BAML type checking fails, e.g. passing a string to a function expecting an int.

Common situations: Typing an expression with a type mismatch at the REPL prompt, calling an LLM function with wrongly-typed positional arguments, using a variable of the wrong type after :load reloads sources with changed signatures.

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

Appendix: source

Thrown at engine/baml-runtime/src/cli/repl.rs:514

        if type_diagnostics.has_errors() {
            eprintln!("Warning: Type errors in loaded BAML sources");
        }

        let input_expr_ast = parse_standalone_expression(input, &mut type_diagnostics)?;
        let input_expr_hir = hir::Expression::from_ast(&input_expr_ast);

        let input_expr_thir =
            typecheck_expression(&input_expr_hir, &type_context, &mut type_diagnostics);

        // Check for type errors in the user's expression
        if type_diagnostics.has_errors() {
            let error_messages: Vec<String> = type_diagnostics
                .errors()
                .iter()
                .map(|e| e.message().to_string())
                .collect();
            return Err(anyhow!("Type error: {}", error_messages.join("; ")));
        }

        // let variables: IndexMap<String, BamlValueWithMeta<TypeGeneric<TypeIR>>> = self

        let variables: IndexMap<String, BamlValueWithMeta<ExprMetadata>> = self
            .variables
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();

        let fn_params = self.function_parameters()?.clone();

        let runtime_clone = self.runtime.clone();
        let env_vars = self.env_vars.clone();
        let run_id = self.current_run_id.unwrap_or(0);
        let handle_llm_function = move |function_name: String,
                                        args: Vec<BamlValue>,
                                        _watch_context: Option<

View on GitHub (pinned to bd85ce9dee)