{"record":{"id":"4d97b843ac9c5a1a","repo":"BoundaryML/baml","slug":"field-assignment-on-non-map-class","errorCode":null,"errorMessage":"field assignment on non-map/class","messagePattern":"field assignment on non-map/class","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1495,"sourceCode":"                        function_name,\n                    )\n                    .await?,\n                )?;\n\n                match &mut base_value {\n                    BamlValueWithMeta::Class(_, fields, _) => {\n                        let entry = fields\n                            .get_mut(field)\n                            .with_context(|| format!(\"field `{field}` not found for assignment\"))?;\n                        *entry = value_to_assign.clone();\n                    }\n                    BamlValueWithMeta::Map(fields, _) => {\n                        let entry = fields\n                            .get_mut(field)\n                            .with_context(|| format!(\"field `{field}` not found for assignment\"))?;\n                        *entry = value_to_assign.clone();\n                    }\n                    _ => bail!(\"field assignment on non-map/class\"),\n                }\n\n                value_to_assign = base_value;\n                current_expr = base.as_ref();\n            }\n            Expr::ArrayAccess { base, index, meta } => {\n                let mut base_value = expect_value(\n                    evaluate_expr(\n                        base,\n                        scopes,\n                        thir,\n                        run_llm_function,\n                        watch_handler,\n                        function_name,\n                    )\n                    .await?,\n                )?;\n                let index_value = expect_value(","sourceCodeStart":1477,"sourceCodeEnd":1513,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1477-L1513","documentation":"When assigning to a field access target like `obj.field = value`, the interpreter only supports bases that evaluate to a `Map` (or class-like map value). If the base value is any other type (string, int, list, etc.), it bails with `field assignment on non-map/class`. Note it also errors if the named field does not yet exist on the map.","triggerScenarios":"`x.f = v` where `x` evaluates to a non-map BAML value, or where the map `x` has no key `f` (the inner `field \\`f\\` not found for assignment` context is attached).","commonSituations":"Treating a string/number result as an object; assuming class instances support direct field mutation when the runtime models them as maps without that key; JSON input missing the field you try to overwrite.","solutions":["Verify the base value is actually a map/class with the target field present before assigning","Add the field at map construction time instead of assigning to a missing key","Inspect the value's runtime type (print/log it) to confirm it is a Map","If mutating other types is needed, rebuild the value with the updated field rather than in-place assignment"],"exampleFix":"// before\nlet cfg = {};\ncfg.name = \"x\"; // field `name` not found for assignment\n// after\nlet cfg = { name: \"x\" };\ncfg.name = \"y\"; // ok: key exists on a map","handlingStrategy":"type-guard","validationCode":"// verify the base is a map containing the field before assigning\n// (conceptual host-side check)\nassert matches!(value, BamlValue::Map(_));","typeGuard":"fn is_map_with_field(v: &BamlValue, field: &str) -> bool {\n    match v { BamlValue::Map(m) => m.contains_key(field), _ => false }\n}","tryCatchPattern":"// host-side\nmatch interp_result {\n    Err(e) if e.to_string().contains(\"field assignment on non-map\") => {\n        // rebuild value as a map with the field instead of mutating\n    }\n    other => other?,\n}","preventionTips":["Only assign fields on values you constructed as maps","Prefer rebuilding values over in-place field mutation","Validate JSON inputs have the keys you intend to overwrite"],"tags":["baml","interpreter","assignment","type-mismatch"],"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"}