{"record":{"id":"7714683cdb475aca","repo":"BoundaryML/baml","slug":"expression-functions-must-have-a-return-type","errorCode":null,"errorMessage":"Expression functions must have a return type","messagePattern":"Expression functions must have a return type","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-core/src/ir/repr.rs","lineNumber":251,"sourceCode":"                name,\n            )\n        });\n        let tests = self\n            .walk_tests()\n            .map(|e| e.node(db))\n            .collect::<Result<Vec<_>>>()?;\n        let arg_types = args\n            .iter()\n            .map(|(_, arg_type)| arg_type.clone())\n            .collect::<Vec<_>>();\n        let arity = arg_types.len();\n        let return_type = self\n            .expr_fn()\n            .return_type\n            .clone()\n            .map(|ret| ret.repr(db))\n            .transpose()?\n            .ok_or(anyhow::anyhow!(\n                \"Expression functions must have a return type\"\n            ))?;\n        let lambda_type = TypeIR::Arrow(\n            Box::new(ArrowGeneric {\n                param_types: arg_types,\n                return_type: return_type.clone(),\n            }),\n            Default::default(),\n        );\n        let expr_fn = ExprFunction {\n            name: self.expr_fn().name.to_string(),\n            inputs: args,\n            output: return_type,\n            expr: Expr::Lambda(\n                arity,\n                Arc::new(closed_body),\n                (self.expr_fn().span.clone(), Some(lambda_type)),\n            ),","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-core/src/ir/repr.rs#L233-L269","documentation":"When converting an AST expression-function (fn defined as an expression/lambda) into IR type representation, the function must declare a return type. If expr_fn().return_type is None, BAML raises this error because it cannot construct the Arrow (function) type without a return type.","triggerScenarios":"Calling repr(db) on a function definition node whose expr_fn lacks a return type annotation — i.e. an expression-style `fn name(...) { ... }` written without `-> ReturnType`.","commonSituations":"Writing BAML expression functions without a return type annotation; older BAML syntax that allowed inferred return types being compiled by a newer stricter compiler.","solutions":["Add an explicit return type annotation to the expression function.","Check the .baml function definition named in surrounding compiler output.","Upgrade or align BAML CLI and runtime versions so syntax expectations match."],"exampleFix":"// before\nfn GetTopic(input string) {\n  \"pick a topic for {{ input }}\"\n}\n// after\nfn GetTopic(input string) -> string {\n  \"pick a topic for {{ input }}\"\n}","handlingStrategy":"validation","validationCode":"# check expression fns declare a return type before compiling\nimport re\nfor m in re.finditer(r'^\\s*fn\\s+\\w+\\s*\\([^)]*\\)\\s*(->\\s*\\w+)?\\s*\\{', src, re.M):\n    if not m.group(1):\n        print(f\"missing return type: {m.group(0)!r}\")","typeGuard":null,"tryCatchPattern":"// add explicit return types; if this still fires, it's a compiler bug:\nErr(e) if e.to_string().contains(\"must have a return type\") => {\n    eprintln!(\"add `-> Type` to the expression fn named in the error\");\n    return Err(e);\n}","preventionTips":["Annotate a return type on every fn in .baml files.","Run baml CLI generate in CI to catch missing annotations early.","Keep BAML schema syntax aligned with your compiler version."],"tags":["baml","types","function","schema"],"backgroundTag":"missing-required-argument","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"}