{"record":{"id":"dca5d9ebcf723f97","repo":"BoundaryML/baml","slug":"failed-to-find-type-to-find-in-union","errorCode":null,"errorMessage":"Failed to find {type_to_find} in union","messagePattern":"Failed to find (.+?) in union","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/ir_type/mod.rs","lineNumber":490,"sourceCode":"    pub fn selected_type_index(\n        &self,\n        type_to_find: &TypeGeneric<T>,\n        lookup: &impl TypeLookupsMeta<T>,\n    ) -> Result<SelectedTypeIndexResult<'_, T>, anyhow::Error> {\n        let options = self.iter_include_null();\n\n        for (i, t) in options.iter().enumerate() {\n            if match t {\n                TypeGeneric::RecursiveTypeAlias { name, .. } => {\n                    &TypeLookupsMeta::expand_recursive_type(lookup, name.as_str())? == type_to_find\n                }\n                _ => *t == type_to_find,\n            } {\n                return Ok(SelectedTypeIndexResult { index: i, options });\n            }\n        }\n\n        Err(anyhow::anyhow!(\"Failed to find {type_to_find} in union\"))\n    }\n}\n\npub trait HasType<T> {\n    fn field_type(&self) -> &TypeGeneric<T>;\n}\n\nimpl HasType<type_meta::IR> for TypeIR {\n    fn field_type(&self) -> &TypeIR {\n        self\n    }\n}\n\nimpl HasType<type_meta::NonStreaming> for TypeNonStreaming {\n    fn field_type(&self) -> &TypeGeneric<type_meta::NonStreaming> {\n        self\n    }\n}","sourceCodeStart":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/ir_type/mod.rs#L472-L508","documentation":"selected_type_index scans the options of a union TypeGeneric looking for an entry equal to type_to_find. If none of the union's options match, it returns this error. It means a lookup assumed a given type is one of the union's members but it is not.","triggerScenarios":"Calling selected_type_index with a type_to_find that is not one of the union's variants (e.g. after a schema edit removed/changed that variant, or the types differ by generic metadata parameters).","commonSituations":"Streaming/partial-response code mapping a runtime-selected type back to its union index after the BAML schema was changed; comparing types that differ by alias or modifiers so equality fails.","solutions":["Print the union's options and confirm type_to_find exactly matches one of them (including modifiers/metadata).","Regenerate/refresh code after schema changes so lookups use current variants.","Handle the Err case by falling back to a search by name or by reporting a schema mismatch instead of assuming membership."],"exampleFix":"// before\nlet idx = union.selected_type_index(&expected)?;\n// after\nlet idx = match union.selected_type_index(&expected) {\n    Ok(r) => r.index,\n    Err(_) => anyhow::bail!(\"type {:?} is not a member of union {:?}\", expected, union),\n};","handlingStrategy":"validation","validationCode":"if !union.options().iter().any(|t| *t == type_to_find) { return Err(anyhow!(\"type not a union member\")); }","typeGuard":"fn in_union(union: &TypeGeneric<Meta>, t: &TypeGeneric<Meta>) -> bool { union.options().iter().any(|o| *o == *t) }","tryCatchPattern":"match union.selected_type_index(&expected) { Ok(r) => r.index, Err(_) => fallback_lookup(&expected) }","preventionTips":["Regenerate clients after schema changes","Compare types including modifiers/metadata","Handle union membership failure explicitly rather than unwrapping"],"tags":["rust","union","lookup"],"backgroundTag":"resource-not-found","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"}