{"record":{"id":"9684c0fe355cc59b","repo":"BoundaryML/baml","slug":"error-evaluating-constraint","errorCode":null,"errorMessage":"Error evaluating constraint: {:?}","messagePattern":"Error evaluating constraint: (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-core/src/ir/ir_helpers/to_baml_arg.rs","lineNumber":651,"sourceCode":"/// Search a potentially deeply-nested `BamlValue` for any failing asserts,\n/// returning the first one encountered.\nfn first_failing_assert_nested<'a>(\n    ir: &'a IntermediateRepr,\n    baml_value: &BamlValue,\n    field_type: &'a TypeIR,\n) -> anyhow::Result<Option<Constraint>> {\n    let value_with_types = ir.distribute_type(baml_value.clone(), field_type.clone())?;\n    let first_failure = value_with_types\n        .iter()\n        .map(|value_node| {\n            let constraints = value_node.meta().meta().constraints.clone();\n            constraints\n                .into_iter()\n                .filter_map(|c| {\n                    let constraint = c.clone();\n                    let baml_value: BamlValue = value_node.into();\n                    let result = evaluate_predicate(&baml_value, &c.expression).map_err(|e| {\n                        anyhow::anyhow!(format!(\"Error evaluating constraint: {:?}\", e))\n                    });\n                    match result {\n                        Ok(false) => {\n                            if c.level == ConstraintLevel::Assert {\n                                Some(Ok(constraint))\n                            } else {\n                                None\n                            }\n                        }\n                        Ok(true) => None,\n                        Err(e) => Some(Err(e)),\n                    }\n                })\n                .collect::<Vec<_>>()\n        })\n        .flat_map(|x| x.into_iter())\n        .next();\n    first_failure.transpose()","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-core/src/ir/ir_helpers/to_baml_arg.rs#L633-L669","documentation":"BAML evaluates @assert/@check constraint predicates against a value using Jinja expressions. When evaluate_predicate itself errors (e.g. the predicate did not render to a boolean), the error is wrapped as 'Error evaluating constraint: {:?}'. This surfaces during argument coercion when checking nested constraints.","triggerScenarios":"coerce_arg -> first_failing_assert_nested encounters a constraint whose Jinja expression fails to evaluate: undefined 'this' fields referenced in the predicate, runtime errors in the expression, or a non-boolean result.","commonSituations":"An @assert predicate like {{ this.len > 5 }} applied to a value without field 'len'; predicate returning a string/number instead of a boolean; predicates written against the wrong field name after a schema refactor.","solutions":["Inspect the inner debug payload after 'Error evaluating constraint:' for the root cause.","Make sure the predicate references only fields that exist on the value being checked.","Ensure the predicate expression evaluates strictly to true/false (e.g. {{ this.len > 5 }}, not {{ this.len }}).","Test the constraint expression standalone with a sample value."],"exampleFix":"// before\n@assert({{ this.items }})\n// after\n@assert({{ this.items|length > 0 }})","handlingStrategy":"validation","validationCode":"// validate predicate fields before coercion\nfn predicate_fields_exist(predicate: &str, value: &serde_json::Value) -> bool {\n    // ensure 'this.<field>' references exist on the value\n    predicate.split(\"this.\").skip(1).all(|rest| {\n        let field: String = rest.chars().take_while(|c| c.is_alphanumeric() || *c == '_').collect();\n        field.is_empty() || value.get(&field).is_some()\n    })\n}","typeGuard":null,"tryCatchPattern":"match evaluate_predicate(&value, &expr) {\n    Ok(true) => { /* pass */ }\n    Ok(false) => { /* constraint failed */ }\n    Err(e) => eprintln!(\"constraint eval error: {e:?}\"), // includes field-level cause\n}","preventionTips":["Reference only fields that exist on the checked value.","Ensure predicates compare to produce true/false.","Test each @assert expression against sample values."],"tags":["validation","assert","constraint","baml"],"backgroundTag":"constraint-evaluation-failed","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"}