{"record":{"id":"94764394a3e77948","repo":"BoundaryML/baml","slug":"expression-functions-must-have-return-type","errorCode":null,"errorMessage":"Expression functions must have return type.","messagePattern":"Expression functions must have return type\\.","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-core/src/ir/repr.rs","lineNumber":290,"sourceCode":"        Ok(expr_fn)\n    }\n}\n\nimpl WithRepr<Function> for ExprFnWalker<'_> {\n    fn repr(&self, db: &ParserDatabase) -> Result<Function> {\n        let body = convert_function_body(self.expr_fn().body.to_owned(), db)?;\n        let args = self\n            .expr_fn()\n            .args\n            .args\n            .iter()\n            .map(|(arg_name, arg_type)| Ok((arg_name.to_string(), arg_type.field_type.repr(db)?)))\n            .collect::<Result<_>>()?;\n        let return_type = self\n            .expr_fn()\n            .return_type\n            .as_ref()\n            .ok_or(anyhow::anyhow!(\n                \"Expression functions must have return type.\"\n            ))?\n            .repr(db)?;\n        let function = Function {\n            name: self.expr_fn().name.to_string(),\n            inputs: args,\n            output: return_type,\n            configs: vec![],\n            default_config: \"\".to_string(),\n            tests: vec![],\n        };\n        Ok(function)\n    }\n}\n\n/// Convert a function body to an expression.\n///\n/// The function body is a list of statements, which are let bindings.","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-core/src/ir/repr.rs#L272-L308","documentation":"While building the IR Function struct from an AST expression function, the return type annotation must be present. BAML raises this error when expr_fn().return_type is None while converting parameters and constructing the function's representation. Note the slightly different wording ('return type.') distinguishes it from the Arrow-type path at line 251.","triggerScenarios":"repr(db) on a top-level expression function definition missing `-> ReturnType`; triggered during IR construction for functions used in client codegen.","commonSituations":"Expression functions declared without return annotations; migrated/copied BAML functions where the return type line was dropped; mixed BAML versions across team members.","solutions":["Add an explicit `-> Type` return annotation to the offending fn definition.","Regenerate the client (baml generate) after fixing the annotation.","Synchronize BAML schema versions across your team and CI."],"exampleFix":"// before\nfn ExtractName(text string) {\n  llm ExtractNamePrompt(text)\n}\n// after\nfn ExtractName(text string) -> string {\n  llm ExtractNamePrompt(text)\n}","handlingStrategy":"validation","validationCode":"# ensure every top-level fn has an explicit return annotation\nimport re\nfor m in re.finditer(r'^\\s*fn\\s+(\\w+)\\s*\\([^)]*\\)\\s*(->\\s*[\\w<>\\[\\]]+)?', src, re.M):\n    if not m.group(2):\n        print(f\"fn {m.group(1)} is missing a return type\")","typeGuard":null,"tryCatchPattern":"match field_repr(db) {\n    Ok(f) => f,\n    Err(e) if e.to_string().contains(\"must have return type\") => {\n        eprintln!(\"fix the fn lacking `-> ReturnType` in the .baml schema\");\n        return Err(e);\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Require explicit return types in code review of .baml changes.","Run `baml generate` as a pre-commit/CI gate.","Pin one BAML version across the team."],"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"}