{"record":{"id":"6a44cc08d7e28f97","repo":"BoundaryML/baml","slug":"type-error","errorCode":null,"errorMessage":"Type error: {}","messagePattern":"Type error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/cli/repl.rs","lineNumber":514,"sourceCode":"\n        if type_diagnostics.has_errors() {\n            eprintln!(\"Warning: Type errors in loaded BAML sources\");\n        }\n\n        let input_expr_ast = parse_standalone_expression(input, &mut type_diagnostics)?;\n        let input_expr_hir = hir::Expression::from_ast(&input_expr_ast);\n\n        let input_expr_thir =\n            typecheck_expression(&input_expr_hir, &type_context, &mut type_diagnostics);\n\n        // Check for type errors in the user's expression\n        if type_diagnostics.has_errors() {\n            let error_messages: Vec<String> = type_diagnostics\n                .errors()\n                .iter()\n                .map(|e| e.message().to_string())\n                .collect();\n            return Err(anyhow!(\"Type error: {}\", error_messages.join(\"; \")));\n        }\n\n        // let variables: IndexMap<String, BamlValueWithMeta<TypeGeneric<TypeIR>>> = self\n\n        let variables: IndexMap<String, BamlValueWithMeta<ExprMetadata>> = self\n            .variables\n            .iter()\n            .map(|(k, v)| (k.clone(), v.clone()))\n            .collect();\n\n        let fn_params = self.function_parameters()?.clone();\n\n        let runtime_clone = self.runtime.clone();\n        let env_vars = self.env_vars.clone();\n        let run_id = self.current_run_id.unwrap_or(0);\n        let handle_llm_function = move |function_name: String,\n                                        args: Vec<BamlValue>,\n                                        _watch_context: Option<","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/cli/repl.rs#L496-L532","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the reported type error in the expression (read the joined messages after 'Type error:')","Run :type <expression> first to check the inferred type before evaluating","Use explicit casts or correct literal types in the expression","Re-check variable types with :env after reloading BAML sources"],"exampleFix":"// before (REPL input)\nMyFn(\"hello\")  // where MyFn expects int\n// after\nMyFn(42)","handlingStrategy":"validation","validationCode":"// Before evaluating, type-check the expression at the REPL\n:type MyFn(arg1, arg2)\n// In embedding code: match on the Result and inspect the anyhow message\nif let Err(e) = repl.parse_and_evaluate_baml_expression(expr) {\n    if e.to_string().starts_with(\"Type error:\") { /* show diagnostics to user */ }\n}","typeGuard":null,"tryCatchPattern":"match result { Err(e) if e.to_string().starts_with(\"Type error:\") => println!(\"Fix types: {e}\"), Ok(v) => println!(\"{v:?}\"), Err(e) => eprintln!(\"{e:#}\") }","preventionTips":["Run :type on expressions before evaluating them","Keep variable types visible with :env after each reload","Match literal types to function signatures (int vs string vs float)","After :load, re-verify calls whose signatures changed"],"tags":["repl","type-checking","baml"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}