{"record":{"id":"28583df6dd17cb96","repo":"BoundaryML/baml","slug":"expected-a-err-msg-got","errorCode":null,"errorMessage":"Expected a {err_msg}, got: {}","messagePattern":"Expected a (.+?), got: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/ir_type/mod.rs","lineNumber":101,"sourceCode":"            TypeGeneric::Arrow(_, _) => \"function\",\n            TypeGeneric::Union(_, _) => \"union\",\n        }\n    }\n}\n\nmacro_rules! impl_as_variant {\n    ($method_name:ident, $variant:pat, $err_msg:literal) => {\n        pub fn $method_name<U: TypeLookupsMeta<T>>(\n            self,\n            lookup: &U,\n        ) -> anyhow::Result<TypeGeneric<T>> {\n            match self {\n                $variant => Ok(self),\n                TypeGeneric::RecursiveTypeAlias { name, .. } => {\n                    let expanded_type = TypeLookupsMeta::<T>::expand_recursive_type(lookup, &name)?;\n                    expanded_type.$method_name::<U>(lookup)\n                }\n                _ => anyhow::bail!(concat!(\"Expected a \", $err_msg, \", got: {}\"), self),\n            }\n        }\n    };\n}\n\nimpl<T: MetaSuffix> TypeGeneric<T> {\n    impl_as_variant!(resolve_map, TypeGeneric::Map(..), \"map type\");\n    impl_as_variant!(resolve_list, TypeGeneric::List(..), \"list type\");\n    impl_as_variant!(resolve_enum, TypeGeneric::Enum { .. }, \"enum type\");\n    impl_as_variant!(resolve_class, TypeGeneric::Class { .. }, \"class type\");\n}\n\n#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, strum::Display)]\npub enum StreamingMode {\n    NonStreaming,\n    Streaming,\n}\n","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/ir_type/mod.rs#L83-L119","documentation":"This error comes from a macro that implements type-narrowing methods on TypeGeneric<T>. When a method that only makes sense for certain type variants (e.g. primitives, classes) is called on a variant that does not support it (and it is not a recursive type alias that can be expanded), the macro bails with \"Expected a <what>, got: <actual>\". It signals that the caller assumed the field type had a shape it does not have.","triggerScenarios":"Calling a TypeGeneric narrowing/inspection method (generated by this macro) on a type whose variant does not match what the method expects, e.g. treating a union, list, or map as a named class, or inspecting a field type before recursive aliases are expanded.","commonSituations":"BAML schema code or generated clients traversing IR types and assuming a field is e.g. a class or primitive when the schema actually declares a union/list; schema changes that changed a field's type while downstream code assumed the old shape.","solutions":["Inspect the actual type in the error message and match on the correct TypeGeneric variant before calling the narrowing method.","Expand recursive type aliases first (expand_recursive_type) so the method can recurse instead of hitting the fallback bail.","If you control the schema, align the field's declared type with what the calling code expects.","Use pattern matching (match on TypeGeneric variants) instead of assuming a single variant."],"exampleFix":"// before\nlet cls = field_type.as_class()?; // panics/bails if field is a union\n// after\nmatch field_type {\n    TypeGeneric::Class(_) => { /* handle class */ }\n    TypeGeneric::Union(_) => { /* handle union explicitly */ }\n    _ => anyhow::bail!(\"unexpected field type\"),\n}","handlingStrategy":"type-guard","validationCode":"if !matches!(field_type, TypeGeneric::Class(_) | TypeGeneric::Primitive(_)) { return Err(anyhow!(\"unexpected variant\")); }","typeGuard":"fn is_named_type(t: &TypeGeneric<Meta>) -> bool { matches!(t, TypeGeneric::Class(_) | TypeGeneric::Enum(_)) }","tryCatchPattern":"match field_type.as_class_if_supported() { Ok(c) => handle(c), Err(e) => log::warn!(\"type shape mismatch: {}\", e) }","preventionTips":["Match on TypeGeneric variants explicitly instead of assuming one shape","Expand recursive type aliases before inspection","Re-run type checks after schema edits"],"tags":["rust","type-system","ir"],"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"}