{"record":{"id":"8e32b59c953c90cc","repo":"BoundaryML/baml","slug":"expected-value-found-function","errorCode":null,"errorMessage":"expected value, found function","messagePattern":"expected value, found function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2337,"sourceCode":"                evaluate_expr(\n                    inner,\n                    scopes,\n                    thir,\n                    run_llm_function,\n                    watch_handler,\n                    function_name,\n                )\n                .await?\n            }\n        })\n    })\n}\n\nfn expect_value(v: EvalValue) -> Result<BamlValueWithMeta<ExprMetadata>> {\n    match v {\n        EvalValue::Value(v) => Ok(v),\n        EvalValue::Reference(cell) => Ok(cell.lock().unwrap().clone()),\n        EvalValue::Function(_, _, _) => bail!(\"expected value, found function\"),\n    }\n}\n\nfn evaluate_binary_op(\n    operator: &crate::hir::BinaryOperator,\n    left_val: &BamlValueWithMeta<ExprMetadata>,\n    right_val: &BamlValueWithMeta<ExprMetadata>,\n    meta: &ExprMetadata,\n) -> Result<BamlValueWithMeta<ExprMetadata>> {\n    use crate::hir::BinaryOperator;\n    Ok(match operator {\n        // Arithmetic operations\n        BinaryOperator::Add => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                BamlValueWithMeta::Int(a + b, meta.clone())\n            }\n            (BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Float(b, _)) => {\n                BamlValueWithMeta::Float(a + b, meta.clone())","sourceCodeStart":2319,"sourceCodeEnd":2355,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2319-L2355","documentation":"`expect_value` converts an evaluation result into a plain value, but function values (first-class function/closure references from `EvalValue::Function`) are not usable as values. When an expression that must produce a data value instead evaluates to a function, this error is thrown.","triggerScenarios":"Using a function name where a value is expected: assigning a function to a variable read as data, passing a function as an argument to something expecting a value, using a function in a binary op, array, or return-value position; calling `expect_value` on the result of an expression like `myFn` without invoking it.","commonSituations":"Forgetting `()` to invoke a function (`let x = compute;` instead of `compute()`); shadowing a variable with a function name; attempting to store/print a function reference.","solutions":["Add parentheses to invoke the function: `myFn()` instead of `myFn`","Check that the identifier at that position refers to a value, not a function declaration","If first-class functions are needed, they are not supported as values in this interpreter — restructure the code"],"exampleFix":"// before\nlet result = compute;      // function, not value\n// after\nlet result = compute();","handlingStrategy":"type-guard","validationCode":"if (typeof candidate === 'function') { throw new Error('expected a value, got a function — did you forget ()?'); }","typeGuard":"function isValueNotFunction(v) { return typeof v !== 'function'; }","tryCatchPattern":"try { let v = expectValue(expr); } catch (e) { if (String(e).includes('expected value, found function')) { /* add () to invoke */ } else { throw e; } }","preventionTips":["Always invoke functions with () when a value is needed","Avoid name shadowing between functions and variables","Remember this interpreter does not treat functions as first-class values"],"tags":["runtime","functions","interpreter","type-check"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}