{"record":{"id":"849da5e342c986be","repo":"BoundaryML/baml","slug":"spread-operator-can-only-be-used-on-classes-at","errorCode":null,"errorMessage":"spread operator can only be used on classes at {:?}","messagePattern":"spread operator can only be used on classes at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2105,"sourceCode":"                                    run_llm_function,\n                                    watch_handler,\n                                    function_name,\n                                )\n                                .await?,\n                            )?;\n                            match spread_val.clone() {\n                                BamlValueWithMeta::Class(_, spread_fields, _) => {\n                                    for (k, v) in spread_fields.iter() {\n                                        field_map.insert(k.clone(), v.clone());\n                                    }\n                                }\n                                // // TODO: Allow maps to be spread?\n                                // BamlValueWithMeta::Map(spread_fields) => {\n                                //     for (k, v) in spread_fields.iter() {\n                                //         field_map.insert(k.clone(), v.clone());\n                                //     }\n                                // }\n                                _ => bail!(\n                                    \"spread operator can only be used on classes at {:?}\",\n                                    meta.0\n                                ),\n                            }\n                        }\n                    }\n                }\n\n                EvalValue::Value(BamlValueWithMeta::Class(\n                    name.clone(),\n                    field_map,\n                    meta.clone(),\n                ))\n            }\n            Expr::Builtin(builtin, meta) => {\n                use crate::thir::Builtin;\n                match builtin {\n                    Builtin::FetchValue => {","sourceCodeStart":2087,"sourceCodeEnd":2123,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2087-L2123","documentation":"In a ClassConstructor expression, the `..spread` syntax requires the spread operand to be a class instance. The interpreter's map-spread support is commented out (TODO), so spreading a map or any non-class value throws \"spread operator can only be used on classes at {:?}\".","triggerScenarios":"Writing `ClassName { ..other_value }` where other_value evaluates to a map, list, or scalar rather than an instance of a class.","commonSituations":"Trying to merge a map of fields into a class constructor (a common pattern in other languages) that BAML's interpreter does not support; passing the result of a parse that yielded a map instead of a class.","solutions":["Spread only actual class instances (e.g. another ClassName { ... } value)","List fields explicitly instead of spreading a map: write out each field assignment","If the source is a map, access its entries and assign them to fields individually","Upgrade BAML and check whether map spreading has since been implemented"],"exampleFix":"// before\nlet c = User { ..user_map } // spread on map unsupported\n// after\nlet c = User { name: user_map.name, age: user_map.age }","handlingStrategy":"validation","validationCode":"// Only allow class instances as spread sources\nfunction assertSpreadable(v: unknown): void {\n  if (v === null || typeof v !== \"object\" || Array.isArray(v) || !isBamlClassInstance(v))\n    throw new Error(\"Spread requires a class instance\");\n}","typeGuard":"function isBamlClassInstance(v: unknown): boolean {\n  return v !== null && typeof v === \"object\" && !Array.isArray(v) && (v as any).__baml_class === true;\n}","tryCatchPattern":"try {\n  return evalBaml(ctorExpr);\n} catch (e) {\n  if (String(e).includes(\"spread operator can only be used on classes\")) {\n    throw new Error(\"Expand map fields explicitly — spread supports class instances only\");\n  }\n  throw e;\n}","preventionTips":["Write fields explicitly when the source is a map","Remember map spreading is unimplemented (TODO) in the interpreter","Construct intermediate class instances if you need to merge structured data","Check release notes before relying on newly proposed spread semantics"],"tags":["interpreter","spread","class-constructor"],"backgroundTag":"unsupported-operation","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"}