{"record":{"id":"d092ef9d9ea7eb72","repo":"BoundaryML/baml","slug":"could-not-unify-map-with-field-type","errorCode":null,"errorMessage":"Could not unify map with {field_type:?}","messagePattern":"Could not unify map with (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs","lineNumber":297,"sourceCode":"\n            BamlValueWithMeta::Bool(b, meta) => {\n                let literal_type = TypeIR::Literal(LiteralValue::Bool(b), Default::default());\n                let primitive_type = TypeIR::Primitive(TypeValue::Bool, Default::default());\n\n                if self.is_subtype(&literal_type, &field_type)\n                    || self.is_subtype(&primitive_type, &field_type)\n                {\n                    Ok(BamlValueWithMeta::Bool(b, (meta, field_type)))\n                } else {\n                    anyhow::bail!(\"Could not unify Bool with {:?}\", field_type)\n                }\n            }\n\n            BamlValueWithMeta::Null(meta) => Ok(BamlValueWithMeta::Null((meta, field_type))),\n\n            BamlValueWithMeta::Map(pairs, meta) => {\n                let (annotation_key_type, annotation_value_type) = map_types(self, &field_type)\n                    .ok_or(anyhow::anyhow!(\"Could not unify map with {field_type:?}\"))?;\n\n                let mapped_fields: BamlMap<String, BamlValueWithMeta<(T, TypeIR)>> = pairs\n                    .into_iter()\n                    .map(|(key, val)| {\n                        let sub_value =\n                            self.distribute_type_with_meta(val, annotation_value_type.clone())?;\n\n                        Ok((key, sub_value))\n                    })\n                    .collect::<anyhow::Result<BamlMap<String, BamlValueWithMeta<(T, TypeIR)>>>>()?;\n\n                Ok(BamlValueWithMeta::Map(mapped_fields, (meta, field_type)))\n            }\n\n            BamlValueWithMeta::List(items, meta) => {\n                let new_items = items\n                    .into_iter()\n                    .map(|i| {","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs#L279-L315","documentation":"While distributing types over a Map value, BAML calls `map_types` to extract the key/value types of the expected TypeIR. If the expected type is not a `map<K, V>` (e.g. a primitive, class, or list), no key/value pair can be extracted and this error is thrown.","triggerScenarios":"distribute_type_with_meta on BamlValueWithMeta::Map when field_type is not a map type — e.g. declaring a parameter as `string` but the caller passes an object; or an alias resolving to a non-map type.","commonSituations":"Sending a JSON object for a parameter declared as a scalar; a class instance passed where a free-form `map<string, X>` was intended (or vice versa); type-alias refactors that changed a map into a class.","solutions":["Declare the parameter/field as `map<string, T>` (with the element type you actually pass) in the .baml schema","If the value is structured, define a class in .baml and pass it where a class type is expected, instead of a raw map","Unwrap or replace optionals/unions so that map_types can resolve — `map<string,int>?` should still resolve, but a bare `string` will not","Inspect the `field_type` debug-printed in the message to see which declared type was expected and align the payload"],"exampleFix":"// before (.baml)\nfunction Upsert(data: string) -> bool { ... }\n// caller sends data: {\"a\": 1}\n\n// after (.baml)\nfunction Upsert(data: map<string, int>) -> bool { ... }","handlingStrategy":"validation","validationCode":"function validateMapArg(value: object, declaredType: string): boolean {\n  return /^map\\s*<\\s*string/.test(declaredType) && value !== null && !Array.isArray(value);\n}","typeGuard":"const isPlainObject = (v: unknown): v is Record<string, unknown> =>\n  typeof v === 'object' && v !== null && !Array.isArray(v);","tryCatchPattern":null,"preventionTips":["Use `map<string, T>` in .baml for free-form objects; classes for structured ones","Distinguish objects from arrays before passing","Avoid refactoring map types into classes without updating call sites","Validate payloads with the generated client types"],"tags":["rust","type-system","baml","map-type"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}